Skip to content

Commit 0f7c71d

Browse files
in case infer-version finds neither the config section, nor the requirement - skip action
closes #1185
1 parent d44633e commit 0f7c71d

File tree

5 files changed

+152
-5
lines changed

5 files changed

+152
-5
lines changed

CHANGELOG.md

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

3+
## v9.0.2
4+
5+
### Fixed
6+
7+
- fix #1184: in case setuptools-scm is a indirect dependency and no pyproject.toml section exists - don't infer the version
8+
39

410
## v9.0.1
511

src/setuptools_scm/_config.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ def from_file(
272272
dist_name: str | None = None,
273273
missing_file_ok: bool = False,
274274
missing_section_ok: bool = False,
275+
pyproject_data: PyProjectData | None = None,
275276
**kwargs: Any,
276277
) -> Configuration:
277278
"""
@@ -291,9 +292,10 @@ def from_file(
291292
"""
292293

293294
try:
294-
pyproject_data = _read_pyproject(
295-
Path(name), missing_section_ok=missing_section_ok
296-
)
295+
if pyproject_data is None:
296+
pyproject_data = _read_pyproject(
297+
Path(name), missing_section_ok=missing_section_ok
298+
)
297299
except FileNotFoundError:
298300
if missing_file_ok:
299301
log.warning("File %s not found, using empty configuration", name)
@@ -303,6 +305,7 @@ def from_file(
303305
project={},
304306
section={},
305307
is_required=False,
308+
section_present=False,
306309
)
307310
else:
308311
raise

src/setuptools_scm/_integration/pyproject_reading.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class PyProjectData(NamedTuple):
2222
project: TOML_RESULT
2323
section: TOML_RESULT
2424
is_required: bool
25+
section_present: bool
2526

2627
@property
2728
def project_name(self) -> str | None:
@@ -55,6 +56,7 @@ def read_pyproject(
5556

5657
try:
5758
section = defn.get("tool", {})[tool_name]
59+
section_present = True
5860
except LookupError as e:
5961
if not is_required and not missing_section_ok:
6062
# Enhanced error message that mentions both configuration options
@@ -69,9 +71,12 @@ def read_pyproject(
6971
error = f"{path} does not contain a tool.{tool_name} section"
7072
log.warning("toml section missing %r", error, exc_info=True)
7173
section = {}
74+
section_present = False
7275

7376
project = defn.get("project", {})
74-
return PyProjectData(path, tool_name, project, section, is_required)
77+
return PyProjectData(
78+
path, tool_name, project, section, is_required, section_present
79+
)
7580

7681

7782
def get_args_for_pyproject(

src/setuptools_scm/_integration/setuptools.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import warnings
66

7+
from pathlib import Path
78
from typing import Any
89
from typing import Callable
910

@@ -149,8 +150,21 @@ def infer_version(dist: setuptools.Distribution) -> None:
149150
if dist_name == "setuptools-scm":
150151
return
151152

153+
# Check if setuptools-scm is configured before proceeding
152154
try:
153-
config = _config.Configuration.from_file(dist_name=dist_name)
155+
from .pyproject_reading import read_pyproject
156+
157+
pyproject_data = read_pyproject(Path("pyproject.toml"), missing_section_ok=True)
158+
# Only proceed if setuptools-scm is either in build_requires or has a tool section
159+
if not pyproject_data.is_required and not pyproject_data.section_present:
160+
return # No setuptools-scm configuration, silently return
161+
except (FileNotFoundError, LookupError):
162+
return # No pyproject.toml or other issues, silently return
163+
164+
try:
165+
config = _config.Configuration.from_file(
166+
dist_name=dist_name, pyproject_data=pyproject_data
167+
)
154168
except LookupError as e:
155169
log.info(e, exc_info=True)
156170
else:

testing/test_integration.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,125 @@ def test_integration_function_call_order(
923923
)
924924

925925

926+
def test_infer_version_with_build_requires_no_tool_section(
927+
wd: WorkDir, monkeypatch: pytest.MonkeyPatch
928+
) -> None:
929+
"""Test that infer_version works when setuptools-scm is in build_requires but no [tool.setuptools_scm] section"""
930+
if sys.version_info < (3, 11):
931+
pytest.importorskip("tomli")
932+
933+
# Set up a git repository with a tag
934+
wd.commit_testfile("test")
935+
wd("git tag 1.0.0")
936+
monkeypatch.chdir(wd.cwd)
937+
938+
# Create a pyproject.toml file with setuptools_scm in build-system.requires but NO [tool.setuptools_scm] section
939+
pyproject_content = """
940+
[build-system]
941+
requires = ["setuptools>=80", "setuptools_scm>=8"]
942+
build-backend = "setuptools.build_meta"
943+
944+
[project]
945+
name = "test-package-infer-version"
946+
dynamic = ["version"]
947+
"""
948+
wd.write("pyproject.toml", pyproject_content)
949+
950+
import setuptools
951+
952+
from setuptools_scm._integration.setuptools import infer_version
953+
954+
# Create distribution
955+
dist = setuptools.Distribution({"name": "test-package-infer-version"})
956+
957+
# Call infer_version - this should work because setuptools_scm is in build-system.requires
958+
infer_version(dist)
959+
960+
# Verify that version was set
961+
assert dist.metadata.version is not None
962+
assert dist.metadata.version == "1.0.0"
963+
964+
# Verify that the marker was set
965+
assert getattr(dist, "_setuptools_scm_version_set_by_infer", False) is True
966+
967+
968+
def test_infer_version_with_build_requires_dash_variant_no_tool_section(
969+
wd: WorkDir, monkeypatch: pytest.MonkeyPatch
970+
) -> None:
971+
"""Test that infer_version works when setuptools-scm (dash variant) is in build_requires but no [tool.setuptools_scm] section"""
972+
if sys.version_info < (3, 11):
973+
pytest.importorskip("tomli")
974+
975+
# Set up a git repository with a tag
976+
wd.commit_testfile("test")
977+
wd("git tag 1.0.0")
978+
monkeypatch.chdir(wd.cwd)
979+
980+
# Create a pyproject.toml file with setuptools-scm (dash variant) in build-system.requires but NO [tool.setuptools_scm] section
981+
pyproject_content = """
982+
[build-system]
983+
requires = ["setuptools>=80", "setuptools-scm>=8"]
984+
build-backend = "setuptools.build_meta"
985+
986+
[project]
987+
name = "test-package-infer-version-dash"
988+
dynamic = ["version"]
989+
"""
990+
wd.write("pyproject.toml", pyproject_content)
991+
992+
import setuptools
993+
994+
from setuptools_scm._integration.setuptools import infer_version
995+
996+
# Create distribution
997+
dist = setuptools.Distribution({"name": "test-package-infer-version-dash"})
998+
999+
# Call infer_version - this should work because setuptools-scm is in build-system.requires
1000+
infer_version(dist)
1001+
1002+
# Verify that version was set
1003+
assert dist.metadata.version is not None
1004+
assert dist.metadata.version == "1.0.0"
1005+
1006+
# Verify that the marker was set
1007+
assert getattr(dist, "_setuptools_scm_version_set_by_infer", False) is True
1008+
1009+
1010+
def test_infer_version_without_build_requires_no_tool_section_silently_returns(
1011+
wd: WorkDir, monkeypatch: pytest.MonkeyPatch
1012+
) -> None:
1013+
"""Test that infer_version silently returns when setuptools-scm is NOT in build_requires and no [tool.setuptools_scm] section"""
1014+
if sys.version_info < (3, 11):
1015+
pytest.importorskip("tomli")
1016+
1017+
# Set up a git repository with a tag
1018+
wd.commit_testfile("test")
1019+
wd("git tag 1.0.0")
1020+
monkeypatch.chdir(wd.cwd)
1021+
1022+
# Create a pyproject.toml file WITHOUT setuptools_scm in build-system.requires and NO [tool.setuptools_scm] section
1023+
pyproject_content = """
1024+
[build-system]
1025+
requires = ["setuptools>=80", "wheel"]
1026+
build-backend = "setuptools.build_meta"
1027+
1028+
[project]
1029+
name = "test-package-no-scm"
1030+
dynamic = ["version"]
1031+
"""
1032+
wd.write("pyproject.toml", pyproject_content)
1033+
1034+
import setuptools
1035+
1036+
from setuptools_scm._integration.setuptools import infer_version
1037+
1038+
# Create distribution
1039+
dist = setuptools.Distribution({"name": "test-package-no-scm"})
1040+
1041+
infer_version(dist)
1042+
assert dist.metadata.version is None
1043+
1044+
9261045
def test_version_keyword_no_scm_dependency_works(
9271046
wd: WorkDir, monkeypatch: pytest.MonkeyPatch
9281047
) -> None:

0 commit comments

Comments
 (0)