Skip to content

Commit 5a6c4b3

Browse files
refactor: reorganize type definitions and simplify imports
Move type definitions closer to their usage and remove unnecessary import aliases across both projects for better module cohesion. Type movements: - GivenPyProjectResult: vcs-versioning/_types.py → _pyproject_reading.py (moved to where it's actually used in read_pyproject) - VersionInferenceApplicable, GetVersionInferenceConfig: vcs-versioning/_types.py → setuptools-scm/_integration/version_inference.py (setuptools-scm specific protocols moved to their implementation module) Import simplifications: - setuptools-scm/_integration/pyproject_reading.py: - Import GivenPyProjectResult directly (not via _types) - Import get_args_for_pyproject directly (not as alias) - Remove wrapper function, just re-export from vcs-versioning - setuptools-scm/_integration/setuptools.py: - Import GivenPyProjectResult from _pyproject_reading - Import GetVersionInferenceConfig from version_inference - Remove _types module import (no longer needed) - vcs-versioning/_config.py: - Import get_args_for_pyproject, read_pyproject directly - Remove unnecessary _ prefixes from imports - vcs-versioning/_pyproject_reading.py: - Add GivenPyProjectResult TypeAlias definition - Remove _types module import, use direct import - vcs-versioning/_types.py: - Remove GivenPyProjectResult (moved to _pyproject_reading) - Remove version inference protocols (moved to setuptools-scm) - Remove Protocol from imports (no longer needed) - Remove unused TYPE_CHECKING imports Benefits: - Better module cohesion - types live with their usage - Cleaner imports without unnecessary aliases - vcs-versioning/_types.py now only contains core VCS types - Version inference types properly scoped to setuptools-scm - No functional changes All tests pass (65 tests: pyproject reading, integration, config, version inference).
1 parent 6e3bc2d commit 5a6c4b3

File tree

6 files changed

+50
-58
lines changed

6 files changed

+50
-58
lines changed

setuptools-scm/src/setuptools_scm/_integration/pyproject_reading.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
from collections.abc import Sequence
66
from pathlib import Path
77

8-
from vcs_versioning import _types as _t
98
from vcs_versioning._pyproject_reading import DEFAULT_PYPROJECT_PATH
9+
from vcs_versioning._pyproject_reading import GivenPyProjectResult
1010
from vcs_versioning._pyproject_reading import PyProjectData
11-
from vcs_versioning._pyproject_reading import (
12-
get_args_for_pyproject as _vcs_get_args_for_pyproject,
13-
)
11+
from vcs_versioning._pyproject_reading import get_args_for_pyproject
1412
from vcs_versioning._pyproject_reading import read_pyproject as _vcs_read_pyproject
1513
from vcs_versioning._requirement_cls import Requirement
1614
from vcs_versioning._requirement_cls import extract_package_name
@@ -121,7 +119,7 @@ def read_pyproject(
121119
path: Path = DEFAULT_PYPROJECT_PATH,
122120
tool_name: str = "setuptools_scm",
123121
canonical_build_package_name: str = "setuptools-scm",
124-
_given_result: _t.GivenPyProjectResult = None,
122+
_given_result: GivenPyProjectResult = None,
125123
_given_definition: TOML_RESULT | None = None,
126124
) -> PyProjectData:
127125
"""Read and parse pyproject configuration with setuptools-specific extensions.
@@ -147,12 +145,3 @@ def read_pyproject(
147145
_check_setuptools_dynamic_version_conflict(path, pyproject_data)
148146

149147
return pyproject_data
150-
151-
152-
def get_args_for_pyproject(
153-
pyproject: PyProjectData,
154-
dist_name: str | None,
155-
kwargs: TOML_RESULT,
156-
) -> TOML_RESULT:
157-
"""Delegate to vcs_versioning's implementation"""
158-
return _vcs_get_args_for_pyproject(pyproject, dist_name, kwargs)

setuptools-scm/src/setuptools_scm/_integration/setuptools.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import setuptools
1010

11-
from vcs_versioning import _types as _t
11+
from vcs_versioning._pyproject_reading import GivenPyProjectResult
1212
from vcs_versioning._toml import InvalidTomlError
1313
from vcs_versioning.overrides import GlobalOverrides
1414
from vcs_versioning.overrides import ensure_context
@@ -17,6 +17,7 @@
1717
from .pyproject_reading import read_pyproject
1818
from .setup_cfg import SetuptoolsBasicData
1919
from .setup_cfg import extract_from_legacy
20+
from .version_inference import GetVersionInferenceConfig
2021
from .version_inference import get_version_inference_config
2122

2223
log = logging.getLogger(__name__)
@@ -74,9 +75,9 @@ def version_keyword(
7475
keyword: str,
7576
value: bool | dict[str, Any] | Callable[[], dict[str, Any]],
7677
*,
77-
_given_pyproject_data: _t.GivenPyProjectResult = None,
78+
_given_pyproject_data: GivenPyProjectResult = None,
7879
_given_legacy_data: SetuptoolsBasicData | None = None,
79-
_get_version_inference_config: _t.GetVersionInferenceConfig = get_version_inference_config,
80+
_get_version_inference_config: GetVersionInferenceConfig = get_version_inference_config,
8081
) -> None:
8182
"""apply version infernce when setup(use_scm_version=...) is used
8283
this takes priority over the finalize_options based version
@@ -131,9 +132,9 @@ def version_keyword(
131132
def infer_version(
132133
dist: setuptools.Distribution,
133134
*,
134-
_given_pyproject_data: _t.GivenPyProjectResult = None,
135+
_given_pyproject_data: GivenPyProjectResult = None,
135136
_given_legacy_data: SetuptoolsBasicData | None = None,
136-
_get_version_inference_config: _t.GetVersionInferenceConfig = get_version_inference_config,
137+
_get_version_inference_config: GetVersionInferenceConfig = get_version_inference_config,
137138
) -> None:
138139
"""apply version inference from the finalize_options hook
139140
this is the default for pyproject.toml based projects that don't use the use_scm_version keyword
@@ -162,8 +163,8 @@ def _infer_version_impl(
162163
*,
163164
dist_name: str | None,
164165
legacy_data: SetuptoolsBasicData,
165-
_given_pyproject_data: _t.GivenPyProjectResult = None,
166-
_get_version_inference_config: _t.GetVersionInferenceConfig = get_version_inference_config,
166+
_given_pyproject_data: GivenPyProjectResult = None,
167+
_get_version_inference_config: GetVersionInferenceConfig = get_version_inference_config,
167168
) -> None:
168169
"""Internal implementation of infer_version."""
169170
try:

setuptools-scm/src/setuptools_scm/_integration/version_inference.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,42 @@
33
import logging
44

55
from dataclasses import dataclass
6+
from typing import TYPE_CHECKING
67
from typing import Any
8+
from typing import Protocol
79
from typing import TypeAlias
810

911
from setuptools import Distribution
1012
from vcs_versioning._pyproject_reading import PyProjectData
1113

1214
from .pyproject_reading import should_infer
1315

16+
if TYPE_CHECKING:
17+
pass # Concrete implementations defined below
18+
1419
log = logging.getLogger(__name__)
1520

1621

22+
class VersionInferenceApplicable(Protocol):
23+
"""A result object from version inference decision that can be applied to a dist."""
24+
25+
def apply(self, dist: Distribution) -> None: # pragma: no cover - structural type
26+
...
27+
28+
29+
class GetVersionInferenceConfig(Protocol):
30+
"""Callable protocol for the decision function used by integration points."""
31+
32+
def __call__(
33+
self,
34+
dist_name: str | None,
35+
current_version: str | None,
36+
pyproject_data: PyProjectData,
37+
overrides: dict[str, object] | None = None,
38+
) -> VersionInferenceApplicable: # pragma: no cover - structural type
39+
...
40+
41+
1742
@dataclass
1843
class VersionInferenceConfig:
1944
"""Configuration for version inference."""

vcs-versioning/src/vcs_versioning/_config.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616

1717
from . import _types as _t
1818
from ._overrides import read_toml_overrides
19-
from ._pyproject_reading import PyProjectData
20-
from ._pyproject_reading import get_args_for_pyproject as _get_args_for_pyproject
21-
from ._pyproject_reading import read_pyproject as _read_pyproject
19+
from ._pyproject_reading import PyProjectData, get_args_for_pyproject, read_pyproject
2220
from ._version_cls import Version as _Version
2321
from ._version_cls import _validate_version_cls
2422
from ._version_cls import _Version as _VersionAlias
@@ -282,8 +280,8 @@ def from_file(
282280
"""
283281

284282
if pyproject_data is None:
285-
pyproject_data = _read_pyproject(Path(name))
286-
args = _get_args_for_pyproject(pyproject_data, dist_name, kwargs)
283+
pyproject_data = read_pyproject(Path(name))
284+
args = get_args_for_pyproject(pyproject_data, dist_name, kwargs)
287285

288286
args.update(read_toml_overrides(args["dist_name"]))
289287
relative_to = args.pop("relative_to", name)

vcs-versioning/src/vcs_versioning/_pyproject_reading.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,31 @@
99
from collections.abc import Sequence
1010
from dataclasses import dataclass
1111
from pathlib import Path
12+
from typing import TYPE_CHECKING, TypeAlias, Union
1213

1314
if sys.version_info >= (3, 11):
1415
from typing import Self
1516
else:
1617
from typing_extensions import Self
1718

18-
from . import _types as _t
1919
from ._requirement_cls import extract_package_name
2020
from ._toml import TOML_RESULT, InvalidTomlError, read_toml_content
2121

22+
if TYPE_CHECKING:
23+
pass # PyProjectData is defined below
24+
2225
log = logging.getLogger(__name__)
2326

2427
_ROOT = "root"
2528

2629

2730
DEFAULT_PYPROJECT_PATH = Path("pyproject.toml")
2831

32+
# Testing injection type for configuration reading
33+
GivenPyProjectResult: TypeAlias = Union[
34+
"PyProjectData", InvalidTomlError, FileNotFoundError, None
35+
]
36+
2937

3038
@dataclass
3139
class PyProjectData:
@@ -170,7 +178,7 @@ def has_build_package(
170178
def read_pyproject(
171179
path: Path = DEFAULT_PYPROJECT_PATH,
172180
canonical_build_package_name: str = "setuptools-scm",
173-
_given_result: _t.GivenPyProjectResult = None,
181+
_given_result: GivenPyProjectResult = None,
174182
_given_definition: TOML_RESULT | None = None,
175183
tool_names: list[str] | None = None,
176184
) -> PyProjectData:

vcs-versioning/src/vcs_versioning/_types.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22

33
import os
44
from collections.abc import Callable, Sequence
5-
from typing import TYPE_CHECKING, Protocol, TypeAlias, Union
5+
from typing import TYPE_CHECKING, TypeAlias, Union
66

77
if TYPE_CHECKING:
8-
from setuptools import Distribution
9-
108
from . import _version_schemes as version
11-
from ._pyproject_reading import PyProjectData
12-
from ._toml import InvalidTomlError
139

1410
PathT: TypeAlias = Union["os.PathLike[str]", str]
1511

@@ -21,28 +17,3 @@
2117

2218
# Git pre-parse function types
2319
GIT_PRE_PARSE: TypeAlias = str | None
24-
25-
# Testing injection types for configuration reading
26-
GivenPyProjectResult: TypeAlias = Union[
27-
"PyProjectData", "InvalidTomlError", FileNotFoundError, None
28-
]
29-
30-
31-
class VersionInferenceApplicable(Protocol):
32-
"""A result object from version inference decision that can be applied to a dist."""
33-
34-
def apply(self, dist: Distribution) -> None: # pragma: no cover - structural type
35-
...
36-
37-
38-
class GetVersionInferenceConfig(Protocol):
39-
"""Callable protocol for the decision function used by integration points."""
40-
41-
def __call__(
42-
self,
43-
dist_name: str | None,
44-
current_version: str | None,
45-
pyproject_data: PyProjectData,
46-
overrides: dict[str, object] | None = None,
47-
) -> VersionInferenceApplicable: # pragma: no cover - structural type
48-
...

0 commit comments

Comments
 (0)