Skip to content

Commit 3eb0d45

Browse files
simplify version inference logic tree
1 parent 882ef63 commit 3eb0d45

File tree

2 files changed

+34
-52
lines changed

2 files changed

+34
-52
lines changed

src/setuptools_scm/_integration/version_inference.py

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -114,62 +114,49 @@ def get_version_inference_config(
114114
Returns:
115115
VersionInferenceResult with the decision and configuration
116116
"""
117+
# Normalize name from project metadata when not provided
117118
if dist_name is None:
118119
dist_name = pyproject_data.project_name
119120

120-
# Handle version already set
121+
# If a version is already present, decide based on context (infer_version vs version_keyword)
121122
if current_version is not None:
122-
if was_set_by_infer:
123-
if overrides is not None and overrides:
124-
# Clear version and proceed with actual overrides (non-empty dict)
125-
return VersionInferenceConfig(
126-
dist_name=dist_name,
127-
pyproject_data=pyproject_data,
128-
overrides=overrides,
129-
)
130-
else:
131-
# Keep existing version from infer_version (no overrides or empty overrides)
132-
# But allow re-inferring if this is another infer_version call
133-
if overrides is None:
134-
# This is another infer_version call, allow it to proceed
135-
return VersionInferenceConfig(
136-
dist_name=dist_name,
137-
pyproject_data=pyproject_data,
138-
overrides=overrides,
139-
)
140-
else:
141-
# This is version_keyword with empty overrides, keep existing version
142-
return VersionInferenceNoOp()
143-
else:
144-
# Version set by something else
123+
# infer_version call (overrides is None) should be a no-op if version already exists
124+
if overrides is None:
125+
return VersionInferenceNoOp()
126+
127+
if not was_set_by_infer:
145128
return VersionInferenceError(
146-
f"version of {dist_name} already set", should_warn=True
129+
f"version of {dist_name} already set",
130+
should_warn=pyproject_data.should_infer(),
147131
)
148132

149-
# Handle setuptools-scm package
133+
# Version was set by infer_version previously
134+
if overrides:
135+
# Non-empty overrides from version_keyword → re-infer with overrides
136+
return VersionInferenceConfig(
137+
dist_name=dist_name, pyproject_data=pyproject_data, overrides=overrides
138+
)
139+
# Empty overrides dict from version_keyword → keep existing version
140+
return VersionInferenceNoOp()
141+
142+
# Do not infer a version for setuptools-scm itself
150143
if dist_name == "setuptools-scm":
151144
return VersionInferenceNoOp()
152145

153-
# version_keyword (with overrides) always tries to infer
146+
# version_keyword path: any overrides (empty or not) mean we should infer
154147
if overrides is not None:
155148
return VersionInferenceConfig(
156-
dist_name=dist_name,
157-
pyproject_data=pyproject_data,
158-
overrides=overrides,
149+
dist_name=dist_name, pyproject_data=pyproject_data, overrides=overrides
159150
)
160151

161-
# infer_version (no overrides) uses pyproject configuration to decide
152+
# infer_version path: decide based on pyproject configuration only
162153
try:
163-
should_proceed = pyproject_data.should_infer()
154+
if pyproject_data.should_infer():
155+
return VersionInferenceConfig(
156+
dist_name=dist_name, pyproject_data=pyproject_data, overrides=None
157+
)
164158
except ValueError:
165-
# For infer_version, silently skip on configuration issues (auto-activation shouldn't error)
159+
# Auto-activation should not error in infer_version context → skip silently
166160
return VersionInferenceNoOp()
167161

168-
if should_proceed:
169-
return VersionInferenceConfig(
170-
dist_name=dist_name,
171-
pyproject_data=pyproject_data,
172-
overrides=overrides,
173-
)
174-
else:
175-
return VersionInferenceNoOp()
162+
return VersionInferenceNoOp()

testing/test_version_inference.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_version_already_set_by_infer_with_overrides(self) -> None:
2626
assert result.overrides == {"key": "value"}
2727

2828
def test_version_already_set_by_infer_no_overrides(self) -> None:
29-
"""Test that we allow re-inferring when version was set by infer_version and overrides=None (another infer_version call)."""
29+
"""infer_version call with existing version should be a no-op."""
3030
result = get_version_inference_config(
3131
dist_name="test_package",
3232
current_version="1.0.0",
@@ -35,9 +35,7 @@ def test_version_already_set_by_infer_no_overrides(self) -> None:
3535
was_set_by_infer=True,
3636
)
3737

38-
assert isinstance(result, VersionInferenceConfig)
39-
assert result.dist_name == "test_package"
40-
assert result.overrides is None
38+
assert isinstance(result, VersionInferenceNoOp)
4139

4240
def test_version_already_set_by_infer_empty_overrides(self) -> None:
4341
"""Test that we don't re-infer when version was set by infer_version with empty overrides (version_keyword call)."""
@@ -52,7 +50,7 @@ def test_version_already_set_by_infer_empty_overrides(self) -> None:
5250
assert isinstance(result, VersionInferenceNoOp)
5351

5452
def test_version_already_set_by_something_else(self) -> None:
55-
"""Test that we return error when version was set by something else."""
53+
"""infer_version call with existing version set by something else should be a no-op."""
5654
result = get_version_inference_config(
5755
dist_name="test_package",
5856
current_version="1.0.0",
@@ -61,9 +59,7 @@ def test_version_already_set_by_something_else(self) -> None:
6159
was_set_by_infer=False,
6260
)
6361

64-
assert isinstance(result, VersionInferenceError)
65-
assert result.message == "version of test_package already set"
66-
assert result.should_warn is True
62+
assert isinstance(result, VersionInferenceNoOp)
6763

6864
def test_setuptools_scm_package(self) -> None:
6965
"""Test that we don't infer for setuptools-scm package itself."""
@@ -190,7 +186,7 @@ def test_none_dist_name(self) -> None:
190186
assert result.dist_name is None
191187

192188
def test_version_already_set_none_dist_name(self) -> None:
193-
"""Test that we handle None dist_name in error case."""
189+
"""infer_version call with None dist_name and existing version should be a no-op."""
194190
result = get_version_inference_config(
195191
dist_name=None,
196192
current_version="1.0.0",
@@ -199,8 +195,7 @@ def test_version_already_set_none_dist_name(self) -> None:
199195
was_set_by_infer=False,
200196
)
201197

202-
assert isinstance(result, VersionInferenceError)
203-
assert result.message == "version of None already set"
198+
assert isinstance(result, VersionInferenceNoOp)
204199

205200
def test_overrides_passed_through(self) -> None:
206201
"""Test that overrides are passed through to the config."""

0 commit comments

Comments
 (0)