Skip to content

Commit ec99afe

Browse files
use a actually user friendly deprecation for the setuptools dynamic antipattern
1 parent 34b72e1 commit ec99afe

File tree

6 files changed

+52
-12
lines changed

6 files changed

+52
-12
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import warnings
2+
3+
from pathlib import Path
4+
5+
6+
def warn_dynamic_version(path: Path, section: str, expression: str) -> None:
7+
warnings.warn(
8+
f"{path}: at [{section}]\n"
9+
f"{expression} forcing setuptools to override the version setuptools-scm sets\n"
10+
"When using setuptools-scm its invalid to use setuptools dynamic version as well, please removeit.\n"
11+
"Setuptools-scm is responsible for setting the version, forcing setuptools to override creates errors."
12+
)
13+
14+
15+
def warn_pyproject_setuptools_dynamic_version(path: Path) -> None:
16+
warn_dynamic_version(path, "tool.setuptools.dynamic", "version = {attr = ...}")
17+
18+
19+
def warn_setup_cfg_dynamic_version(path: Path) -> None:
20+
warn_dynamic_version(path, "metadata", "version = attr: ...}")

src/setuptools_scm/_integration/pyproject_reading.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,9 @@ def read_pyproject(
235235
.get("version", None)
236236
)
237237
if setuptools_dynamic_version is not None:
238-
warnings.warn(
239-
f"{path}: at [tool.setuptools.dynamic]\n"
240-
"version = {attr = ...} is sabotaging setuptools-scm"
241-
)
238+
from .deprecation import warn_pyproject_setuptools_dynamic_version
239+
240+
warn_pyproject_setuptools_dynamic_version(path)
242241

243242
return pyproject_data
244243

src/setuptools_scm/_integration/setup_cfg.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import os
4-
import warnings
54

65
from dataclasses import dataclass
76
from pathlib import Path
@@ -27,10 +26,9 @@ def read_setup_cfg(input: str | os.PathLike[str] = "setup.cfg") -> SetuptoolsBas
2726
name = parser.get("metadata", "name", fallback=None)
2827
version = parser.get("metadata", "version", fallback=None)
2928
if version is not None and "attr" in version:
30-
warnings.warn(
31-
"setup.cfg: ignoring invalid dynamic version - version = attr: ..."
32-
" is sabotaging setuptools-scm"
33-
)
29+
from .deprecation import warn_setup_cfg_dynamic_version
30+
31+
warn_setup_cfg_dynamic_version(path)
3432
version = None
3533
return SetuptoolsBasicData(path=path, name=name, version=version)
3634

testing/test_deprecation.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""Test deprecation warnings and their exact text."""
2+
3+
from pathlib import Path
4+
5+
import pytest
6+
7+
from setuptools_scm._integration.deprecation import warn_dynamic_version
8+
9+
10+
def test_warn_dynamic_version_full_text() -> None:
11+
"""Test the complete warning text for warn_dynamic_version function."""
12+
test_path = Path("test_file.toml")
13+
expected_warning = (
14+
f"{test_path}: at [test.section]\n"
15+
"test_expression forcing setuptools to override the version setuptools-scm sets\n"
16+
"When using setuptools-scm its invalid to use setuptools dynamic version as well, please removeit.\n"
17+
"Setuptools-scm is responsible for setting the version, forcing setuptools to override creates errors."
18+
)
19+
20+
with pytest.warns(UserWarning) as warning_info: # noqa: PT030
21+
warn_dynamic_version(test_path, "test.section", "test_expression")
22+
23+
assert len(warning_info) == 1
24+
assert str(warning_info[0].message) == expected_warning

testing/test_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ def test_setup_cfg_dynamic_version_warns_and_ignores(tmp_path: Path) -> None:
474474

475475
with pytest.warns(
476476
UserWarning,
477-
match="setup.cfg: ignoring invalid dynamic version - version = attr: ... is sabotaging setuptools-scm",
477+
match=r"setup\.cfg: at \[metadata\]",
478478
):
479479
legacy_data = read_setup_cfg(cfg)
480480

testing/test_pyproject_reading.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ def test_read_pyproject_with_given_definition(monkeypatch: pytest.MonkeyPatch) -
131131
def test_read_pyproject_with_setuptools_dynamic_version_warns() -> None:
132132
with pytest.warns(
133133
UserWarning,
134-
match=r"pyproject.toml: at \[tool\.setuptools\.dynamic\]\n"
135-
r"version = {attr = \.\.\.} is sabotaging setuptools-scm",
134+
match=r"pyproject\.toml: at \[tool\.setuptools\.dynamic\]",
136135
):
137136
pyproject_data = read_pyproject(
138137
_given_definition={

0 commit comments

Comments
 (0)