Skip to content

Commit 78eff52

Browse files
fix: only exclude _version from top-level Import Linter contracts, not submodule contracts
Closes #1432 Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com> Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/385f0827-605c-428f-8947-960907d3c5a9
1 parent ac8d8a5 commit 78eff52

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

src/usethis/_integrations/project/imports.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,16 @@ def _get_module_layered_architecture(
9191
# _version is typically auto-generated by VCS plugins (e.g. setuptools-scm,
9292
# hatch-vcs) and should not participate in the layered architecture.
9393
# Instead, it is excluded so it appears in exhaustive_ignores.
94+
# Only exclude _version at the root package level, not in subpackages.
9495
# Ref: https://github.com/usethis-python/usethis-python/issues/1423
95-
_VERSION_MODULE = "_version"
96-
for layer in layers:
97-
if _VERSION_MODULE in layer:
98-
layer.discard(_VERSION_MODULE)
99-
excluded.add(_VERSION_MODULE)
100-
layers = [layer for layer in layers if layer]
96+
# Ref: https://github.com/usethis-python/usethis-python/issues/1432
97+
if "." not in module:
98+
_VERSION_MODULE = "_version"
99+
for layer in layers:
100+
if _VERSION_MODULE in layer:
101+
layer.discard(_VERSION_MODULE)
102+
excluded.add(_VERSION_MODULE)
103+
layers = [layer for layer in layers if layer]
101104

102105
return LayeredArchitecture(
103106
layers=list(reversed(layers)),

tests/usethis/_integrations/project/test_imports.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,36 @@ def test_version_excluded_with_deps(
285285
assert arch.layers == [{"a"}, {"b"}]
286286
assert arch.excluded == {"_version"}
287287

288+
def test_version_not_excluded_in_submodule(
289+
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
290+
):
291+
"""_version is not excluded from submodule layer contracts.
292+
293+
Only top-level packages should exclude _version, since VCS plugins
294+
(e.g. setuptools-scm, hatch-vcs) only auto-generate it at the root.
295+
296+
Ref: https://github.com/usethis-python/usethis-python/issues/1432
297+
"""
298+
# Arrange
299+
(tmp_path / "salut").mkdir()
300+
(tmp_path / "salut" / "__init__.py").touch()
301+
(tmp_path / "salut" / "b").mkdir()
302+
(tmp_path / "salut" / "b" / "__init__.py").touch()
303+
(tmp_path / "salut" / "b" / "c.py").touch()
304+
(tmp_path / "salut" / "b" / "d.py").touch()
305+
(tmp_path / "salut" / "b" / "_version.py").touch()
306+
307+
monkeypatch.syspath_prepend(str(tmp_path))
308+
309+
# Act
310+
with change_cwd(tmp_path):
311+
graph = _get_graph("salut")
312+
arch = _get_module_layered_architecture("salut.b", graph=graph)
313+
314+
# Assert
315+
assert arch.layers == [{"_version", "c", "d"}]
316+
assert arch.excluded == set()
317+
288318

289319
class TestGetChildDependencies:
290320
def test_three(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch):

0 commit comments

Comments
 (0)