Skip to content

Commit 7281159

Browse files
Merge pull request #1183 from RonnyPfannschmidt/fix-1180-provide-dummy-scmversion-for-dump
fix 1180 provide dummy scmversion for dump
2 parents 5c7a701 + a921c02 commit 7281159

File tree

3 files changed

+89
-8
lines changed

3 files changed

+89
-8
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
4+
## v9.0.1
5+
6+
### Fixed
7+
8+
- fix #1180: ensure version dumping works when no scm_version is given (problems in downstreams)
9+
10+
311
## v9.0.0
412

513
### Breaking

src/setuptools_scm/_integration/dump_version.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,34 @@ def _validate_template(target: Path, template: str | None) -> str:
9595
return template
9696

9797

98+
class DummyScmVersion:
99+
@property
100+
def short_node(self) -> str | None:
101+
return None
102+
103+
98104
def write_version_to_path(
99-
target: Path, template: str | None, version: str, scm_version: ScmVersion | None
105+
target: Path,
106+
template: str | None,
107+
version: str,
108+
scm_version: ScmVersion | None = None,
100109
) -> None:
101110
final_template = _validate_template(target, template)
102111
log.debug("dump %s into %s", version, target)
103112
version_tuple = _version_as_tuple(version)
104-
if scm_version is not None:
105-
content = final_template.format(
106-
version=version,
107-
version_tuple=version_tuple,
108-
scm_version=scm_version,
113+
if scm_version is None:
114+
warnings.warn(
115+
"write_version_to_path called without scm_version parameter. "
116+
"This will be required in a future version. "
117+
"Pass scm_version=None explicitly to suppress this warning.",
118+
DeprecationWarning,
119+
stacklevel=2,
109120
)
110-
else:
111-
content = final_template.format(version=version, version_tuple=version_tuple)
121+
122+
content = final_template.format(
123+
version=version,
124+
version_tuple=version_tuple,
125+
scm_version=scm_version or DummyScmVersion(),
126+
)
112127

113128
target.write_text(content, encoding="utf-8")

testing/test_functions.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,61 @@ def test_has_command_logs_stderr(caplog: pytest.LogCaptureFixture) -> None:
293293
def test_tag_to_version(tag: str, expected_version: str) -> None:
294294
version = str(tag_to_version(tag, c))
295295
assert version == expected_version
296+
297+
298+
def test_write_version_to_path_deprecation_warning_none(tmp_path: Path) -> None:
299+
"""Test that write_version_to_path warns when scm_version=None is passed."""
300+
from setuptools_scm._integration.dump_version import write_version_to_path
301+
302+
target_file = tmp_path / "version.py"
303+
304+
# This should raise a deprecation warning when scm_version=None is explicitly passed
305+
with pytest.warns(
306+
DeprecationWarning, match="write_version_to_path called without scm_version"
307+
):
308+
write_version_to_path(
309+
target=target_file,
310+
template=None, # Use default template
311+
version="1.2.3",
312+
scm_version=None, # Explicitly passing None should warn
313+
)
314+
315+
# Verify the file was created and contains the expected content
316+
assert target_file.exists()
317+
content = target_file.read_text(encoding="utf-8")
318+
319+
# Check that the version is correctly formatted
320+
assert "__version__ = version = '1.2.3'" in content
321+
assert "__version_tuple__ = version_tuple = (1, 2, 3)" in content
322+
323+
# Check that commit_id is set to None when scm_version is None
324+
assert "__commit_id__ = commit_id = None" in content
325+
326+
327+
def test_write_version_to_path_deprecation_warning_missing(tmp_path: Path) -> None:
328+
"""Test that write_version_to_path warns when scm_version parameter is not provided."""
329+
from setuptools_scm._integration.dump_version import write_version_to_path
330+
331+
target_file = tmp_path / "version.py"
332+
333+
# This should raise a deprecation warning when scm_version is not provided
334+
with pytest.warns(
335+
DeprecationWarning, match="write_version_to_path called without scm_version"
336+
):
337+
write_version_to_path(
338+
target=target_file,
339+
template=None, # Use default template
340+
version="1.2.3",
341+
# scm_version not provided - should warn
342+
)
343+
344+
# Verify the file was created and contains the expected content
345+
assert target_file.exists()
346+
content = target_file.read_text(encoding="utf-8")
347+
348+
# Check that the version is correctly formatted
349+
assert "__version__ = version = '1.2.3'" in content
350+
assert "__version_tuple__ = version_tuple = (1, 2, 3)" in content
351+
352+
# Check that commit_id is set to None when scm_version is None
353+
assert "__commit_id__ = commit_id = None" in content

0 commit comments

Comments
 (0)