Skip to content

Commit e41490b

Browse files
Merge pull request #1185 from RonnyPfannschmidt/fix-1181-version-keyword-doesnt-need-config
fix 1181 version keyword doesnt need config
2 parents 7281159 + 4ffcbfc commit e41490b

File tree

5 files changed

+49
-3
lines changed

5 files changed

+49
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
### Fixed
77

88
- fix #1180: ensure version dumping works when no scm_version is given (problems in downstreams)
9-
9+
- fix #1181: config - reintroduce controll over when we expect a section to be present
10+
as it turns out theres valid use cases where setuptools_scm is not direct part of the dependencies
1011

1112
## v9.0.0
1213

src/setuptools_scm/_config.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ def from_file(
271271
name: str | os.PathLike[str] = "pyproject.toml",
272272
dist_name: str | None = None,
273273
missing_file_ok: bool = False,
274+
missing_section_ok: bool = False,
274275
**kwargs: Any,
275276
) -> Configuration:
276277
"""
@@ -279,10 +280,20 @@ def from_file(
279280
not installed or the file has invalid format or does
280281
not contain setuptools_scm configuration (either via
281282
_ [tool.setuptools_scm] section or build-system.requires).
283+
284+
Parameters:
285+
- name: path to pyproject.toml
286+
- dist_name: name of the distribution
287+
- missing_file_ok: if True, do not raise an error if the file is not found
288+
- missing_section_ok: if True, do not raise an error if the section is not found
289+
(workaround for not walking the dependency graph when figuring out if setuptools_scm is a dependency)
290+
- **kwargs: additional keyword arguments to pass to the Configuration constructor
282291
"""
283292

284293
try:
285-
pyproject_data = _read_pyproject(Path(name))
294+
pyproject_data = _read_pyproject(
295+
Path(name), missing_section_ok=missing_section_ok
296+
)
286297
except FileNotFoundError:
287298
if missing_file_ok:
288299
log.warning("File %s not found, using empty configuration", name)

src/setuptools_scm/_integration/pyproject_reading.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def read_pyproject(
4747
path: Path = Path("pyproject.toml"),
4848
tool_name: str = "setuptools_scm",
4949
build_package_names: Sequence[str] = ("setuptools_scm", "setuptools-scm"),
50+
missing_section_ok: bool = False,
5051
) -> PyProjectData:
5152
defn = read_toml_content(path)
5253
requires: list[str] = defn.get("build-system", {}).get("requires", [])
@@ -55,7 +56,7 @@ def read_pyproject(
5556
try:
5657
section = defn.get("tool", {})[tool_name]
5758
except LookupError as e:
58-
if not is_required:
59+
if not is_required and not missing_section_ok:
5960
# Enhanced error message that mentions both configuration options
6061
error = (
6162
f"{path} does not contain a tool.{tool_name} section. "

src/setuptools_scm/_integration/setuptools.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def version_keyword(
130130
config = _config.Configuration.from_file(
131131
dist_name=dist_name,
132132
missing_file_ok=True,
133+
missing_section_ok=True,
133134
**overrides,
134135
)
135136
_assign_version(dist, config)

testing/test_integration.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,3 +921,35 @@ def test_integration_function_call_order(
921921
assert final_version == expected_final_version, (
922922
f"Expected version '{expected_final_version}' but got '{final_version}'"
923923
)
924+
925+
926+
def test_version_keyword_no_scm_dependency_works(
927+
wd: WorkDir, monkeypatch: pytest.MonkeyPatch
928+
) -> None:
929+
# Set up a git repository with a tag
930+
wd.commit_testfile("test")
931+
wd("git tag 1.0.0")
932+
monkeypatch.chdir(wd.cwd)
933+
934+
# Create a pyproject.toml file WITHOUT setuptools_scm in build-system.requires
935+
# and WITHOUT [tool.setuptools_scm] section
936+
pyproject_content = """
937+
[build-system]
938+
requires = ["setuptools>=80"]
939+
build-backend = "setuptools.build_meta"
940+
941+
[project]
942+
name = "test-package-no-scm"
943+
dynamic = ["version"]
944+
"""
945+
wd.write("pyproject.toml", pyproject_content)
946+
947+
import setuptools
948+
949+
from setuptools_scm._integration.setuptools import version_keyword
950+
951+
# Create distribution
952+
dist = setuptools.Distribution({"name": "test-package-no-scm"})
953+
954+
version_keyword(dist, "use_scm_version", True)
955+
assert dist.metadata.version == "1.0.0"

0 commit comments

Comments
 (0)