diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b28221d..1b952ade 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog + +## v9.0.1 + +### Fixed + +- fix #1180: ensure version dumping works when no scm_version is given (problems in downstreams) + + ## v9.0.0 ### Breaking diff --git a/src/setuptools_scm/_integration/dump_version.py b/src/setuptools_scm/_integration/dump_version.py index 62827a1b..06081c9f 100644 --- a/src/setuptools_scm/_integration/dump_version.py +++ b/src/setuptools_scm/_integration/dump_version.py @@ -95,19 +95,34 @@ def _validate_template(target: Path, template: str | None) -> str: return template +class DummyScmVersion: + @property + def short_node(self) -> str | None: + return None + + def write_version_to_path( - target: Path, template: str | None, version: str, scm_version: ScmVersion | None + target: Path, + template: str | None, + version: str, + scm_version: ScmVersion | None = None, ) -> None: final_template = _validate_template(target, template) log.debug("dump %s into %s", version, target) version_tuple = _version_as_tuple(version) - if scm_version is not None: - content = final_template.format( - version=version, - version_tuple=version_tuple, - scm_version=scm_version, + if scm_version is None: + warnings.warn( + "write_version_to_path called without scm_version parameter. " + "This will be required in a future version. " + "Pass scm_version=None explicitly to suppress this warning.", + DeprecationWarning, + stacklevel=2, ) - else: - content = final_template.format(version=version, version_tuple=version_tuple) + + content = final_template.format( + version=version, + version_tuple=version_tuple, + scm_version=scm_version or DummyScmVersion(), + ) target.write_text(content, encoding="utf-8") diff --git a/testing/test_functions.py b/testing/test_functions.py index c0cb5166..b6b8a59e 100644 --- a/testing/test_functions.py +++ b/testing/test_functions.py @@ -293,3 +293,61 @@ def test_has_command_logs_stderr(caplog: pytest.LogCaptureFixture) -> None: def test_tag_to_version(tag: str, expected_version: str) -> None: version = str(tag_to_version(tag, c)) assert version == expected_version + + +def test_write_version_to_path_deprecation_warning_none(tmp_path: Path) -> None: + """Test that write_version_to_path warns when scm_version=None is passed.""" + from setuptools_scm._integration.dump_version import write_version_to_path + + target_file = tmp_path / "version.py" + + # This should raise a deprecation warning when scm_version=None is explicitly passed + with pytest.warns( + DeprecationWarning, match="write_version_to_path called without scm_version" + ): + write_version_to_path( + target=target_file, + template=None, # Use default template + version="1.2.3", + scm_version=None, # Explicitly passing None should warn + ) + + # Verify the file was created and contains the expected content + assert target_file.exists() + content = target_file.read_text(encoding="utf-8") + + # Check that the version is correctly formatted + assert "__version__ = version = '1.2.3'" in content + assert "__version_tuple__ = version_tuple = (1, 2, 3)" in content + + # Check that commit_id is set to None when scm_version is None + assert "__commit_id__ = commit_id = None" in content + + +def test_write_version_to_path_deprecation_warning_missing(tmp_path: Path) -> None: + """Test that write_version_to_path warns when scm_version parameter is not provided.""" + from setuptools_scm._integration.dump_version import write_version_to_path + + target_file = tmp_path / "version.py" + + # This should raise a deprecation warning when scm_version is not provided + with pytest.warns( + DeprecationWarning, match="write_version_to_path called without scm_version" + ): + write_version_to_path( + target=target_file, + template=None, # Use default template + version="1.2.3", + # scm_version not provided - should warn + ) + + # Verify the file was created and contains the expected content + assert target_file.exists() + content = target_file.read_text(encoding="utf-8") + + # Check that the version is correctly formatted + assert "__version__ = version = '1.2.3'" in content + assert "__version_tuple__ = version_tuple = (1, 2, 3)" in content + + # Check that commit_id is set to None when scm_version is None + assert "__commit_id__ = commit_id = None" in content