Skip to content

Commit 7570f68

Browse files
ignore dynamic version in setup.cfg
partial solution to #1216
1 parent a0920fd commit 7570f68

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

CHANGELOG.md

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

3+
4+
## v9.2.0
5+
6+
### fixed
7+
8+
- fix #1216: accept and create a warning for usages of `version = attr:` in setuptools config.
9+
unfortunately dozens of projects cargo-culted that antipattern
10+
11+
312
## v9.2.0
413

514
### Added

src/setuptools_scm/_integration/setup_cfg.py

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

33
import os
4+
import warnings
45

56
from dataclasses import dataclass
67
from pathlib import Path
@@ -25,6 +26,12 @@ def read_setup_cfg(input: str | os.PathLike[str] = "setup.cfg") -> SetuptoolsBas
2526

2627
name = parser.get("metadata", "name", fallback=None)
2728
version = parser.get("metadata", "version", fallback=None)
29+
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+
)
34+
version = None
2835
return SetuptoolsBasicData(path=path, name=name, version=version)
2936

3037

testing/test_integration.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from setuptools_scm._integration import setuptools as setuptools_integration
1919
from setuptools_scm._integration.pyproject_reading import PyProjectData
2020
from setuptools_scm._integration.setup_cfg import SetuptoolsBasicData
21+
from setuptools_scm._integration.setup_cfg import read_setup_cfg
2122
from setuptools_scm._requirement_cls import extract_package_name
2223

2324
if TYPE_CHECKING:
@@ -457,6 +458,29 @@ def test_unicode_in_setup_cfg(tmp_path: Path) -> None:
457458
assert data.version == "1.2.3"
458459

459460

461+
@pytest.mark.issue(1216)
462+
def test_setup_cfg_dynamic_version_warns_and_ignores(tmp_path: Path) -> None:
463+
cfg = tmp_path / "setup.cfg"
464+
cfg.write_text(
465+
textwrap.dedent(
466+
"""
467+
[metadata]
468+
name = example-broken
469+
version = attr: example_broken.__version__
470+
"""
471+
),
472+
encoding="utf-8",
473+
)
474+
475+
with pytest.warns(
476+
UserWarning,
477+
match="setup.cfg: ignoring invalid dynamic version - version = attr: ... is sabotaging setuptools-scm",
478+
):
479+
legacy_data = read_setup_cfg(cfg)
480+
481+
assert legacy_data.version is None
482+
483+
460484
def test_setup_cfg_version_prevents_inference_version_keyword(
461485
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
462486
) -> None:

0 commit comments

Comments
 (0)