Skip to content

Commit 56a7a00

Browse files
split version inference into the part that needs a distribution object and the part that doesnt
1 parent 206742a commit 56a7a00

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

src/setuptools_scm/_integration/version_inference.py

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,13 @@ class VersionInferenceConfig:
2525

2626
def apply(self, dist: Distribution) -> None:
2727
"""Apply version inference to the distribution."""
28-
from .. import _config as _config_module
29-
from .._get_version_impl import _get_version
30-
from .._get_version_impl import _version_missing
31-
32-
config = _config_module.Configuration.from_file(
33-
dist_name=self.dist_name,
34-
pyproject_data=self.pyproject_data,
35-
**(self.overrides or {}),
28+
version_string = infer_version_string(
29+
self.dist_name,
30+
self.pyproject_data, # type: ignore[arg-type]
31+
self.overrides,
32+
force_write_version_files=True,
3633
)
37-
38-
# Get and assign version
39-
maybe_version = _get_version(config, force_write_version_files=True)
40-
if maybe_version is None:
41-
_version_missing(config)
42-
else:
43-
dist.metadata.version = maybe_version
34+
dist.metadata.version = version_string
4435

4536
# Mark that this version was set by infer_version if overrides is None (infer_version context)
4637
if self.overrides is None:
@@ -75,6 +66,43 @@ def apply(self, dist: Distribution) -> None:
7566
]
7667

7768

69+
def infer_version_string(
70+
dist_name: str | None,
71+
pyproject_data: PyProjectData,
72+
overrides: dict[str, Any] | None = None,
73+
*,
74+
force_write_version_files: bool = False,
75+
) -> str:
76+
"""
77+
Compute the inferred version string from the given inputs without requiring a
78+
setuptools Distribution instance. This is a pure helper that simplifies
79+
integration tests by avoiding file I/O and side effects on a Distribution.
80+
81+
Parameters:
82+
dist_name: Optional distribution name (used for overrides and env scoping)
83+
pyproject_data: Parsed PyProjectData (may be constructed via for_testing())
84+
overrides: Optional override configuration (same keys as [tool.setuptools_scm])
85+
force_write_version_files: When True, apply write_to/version_file effects
86+
87+
Returns:
88+
The computed version string.
89+
"""
90+
from .. import _config as _config_module
91+
from .._get_version_impl import _get_version
92+
from .._get_version_impl import _version_missing
93+
94+
config = _config_module.Configuration.from_file(
95+
dist_name=dist_name, pyproject_data=pyproject_data, **(overrides or {})
96+
)
97+
98+
maybe_version = _get_version(
99+
config, force_write_version_files=force_write_version_files
100+
)
101+
if maybe_version is None:
102+
_version_missing(config)
103+
return maybe_version
104+
105+
78106
def get_version_inference_config(
79107
dist_name: str | None,
80108
current_version: str | None,

0 commit comments

Comments
 (0)