Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/usethis/_integrations/project/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,16 @@ def _get_module_layered_architecture(
# _version is typically auto-generated by VCS plugins (e.g. setuptools-scm,
# hatch-vcs) and should not participate in the layered architecture.
# Instead, it is excluded so it appears in exhaustive_ignores.
# Only exclude _version at the root package level, not in subpackages.
# Ref: https://github.com/usethis-python/usethis-python/issues/1423
_VERSION_MODULE = "_version"
for layer in layers:
if _VERSION_MODULE in layer:
layer.discard(_VERSION_MODULE)
excluded.add(_VERSION_MODULE)
layers = [layer for layer in layers if layer]
# Ref: https://github.com/usethis-python/usethis-python/issues/1432
if "." not in module:
_VERSION_MODULE = "_version"
for layer in layers:
if _VERSION_MODULE in layer:
layer.discard(_VERSION_MODULE)
excluded.add(_VERSION_MODULE)
layers = [layer for layer in layers if layer]

return LayeredArchitecture(
layers=list(reversed(layers)),
Expand Down
30 changes: 30 additions & 0 deletions tests/usethis/_integrations/project/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,36 @@ def test_version_excluded_with_deps(
assert arch.layers == [{"a"}, {"b"}]
assert arch.excluded == {"_version"}

def test_version_not_excluded_in_submodule(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
"""_version is not excluded from submodule layer contracts.

Only top-level packages should exclude _version, since VCS plugins
(e.g. setuptools-scm, hatch-vcs) only auto-generate it at the root.

Ref: https://github.com/usethis-python/usethis-python/issues/1432
"""
# Arrange
(tmp_path / "salut").mkdir()
(tmp_path / "salut" / "__init__.py").touch()
(tmp_path / "salut" / "b").mkdir()
(tmp_path / "salut" / "b" / "__init__.py").touch()
(tmp_path / "salut" / "b" / "c.py").touch()
(tmp_path / "salut" / "b" / "d.py").touch()
(tmp_path / "salut" / "b" / "_version.py").touch()

monkeypatch.syspath_prepend(str(tmp_path))

# Act
with change_cwd(tmp_path):
graph = _get_graph("salut")
arch = _get_module_layered_architecture("salut.b", graph=graph)

# Assert
assert arch.layers == [{"_version", "c", "d"}]
assert arch.excluded == set()


class TestGetChildDependencies:
def test_three(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
Expand Down
Loading