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
11 changes: 11 additions & 0 deletions src/usethis/_integrations/project/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ def _get_module_layered_architecture(
if m not in layered:
excluded.add(m)

# _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.
# 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]

return LayeredArchitecture(
layers=list(reversed(layers)),
excluded=excluded,
Expand Down
43 changes: 43 additions & 0 deletions tests/usethis/_core/test_core_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,49 @@ def test_cyclic_excluded(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
"""
)

@pytest.mark.usefixtures("_vary_network_conn")
def test_version_excluded(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
"""_version.py is auto-excluded into exhaustive_ignores.

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

monkeypatch.syspath_prepend(str(tmp_path))

# Act
with change_cwd(tmp_path), files_manager():
use_import_linter()

# Assert
contents = (tmp_path / ".importlinter").read_text()
assert contents == (
"""\
[importlinter]
root_packages =
a

[importlinter:contract:0]
name = a
type = layers
layers =
b | c
containers =
a
exhaustive = True
exhaustive_ignores =
_version
"""
)

@pytest.mark.usefixtures("_vary_network_conn")
def test_existing_ini_match(self, tmp_path: Path):
# Arrange
Expand Down
81 changes: 81 additions & 0 deletions tests/usethis/_integrations/project/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,87 @@ def test_bottom_heavy(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
assert arch.layers == [{"f", "g"}, {"d", "e"}, {"a", "b", "c"}]
assert arch.excluded == set()

def test_version_excluded_with_others(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
"""_version is excluded even when sharing a layer with other modules.

Ref: https://github.com/usethis-python/usethis-python/issues/1423
"""
# Arrange
(tmp_path / "salut").mkdir()
(tmp_path / "salut" / "__init__.py").touch()
(tmp_path / "salut" / "a.py").touch()
(tmp_path / "salut" / "b.py").touch()
(tmp_path / "salut" / "_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", graph=graph)

# Assert
assert arch.layers == [{"a", "b"}]
assert arch.excluded == {"_version"}

def test_version_excluded_sole_in_layer(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
"""_version is excluded even when it is the sole module in its layer.

The empty layer should be cleaned up.

Ref: https://github.com/usethis-python/usethis-python/issues/1423
"""
# Arrange
(tmp_path / "salut").mkdir()
(tmp_path / "salut" / "__init__.py").touch()
(tmp_path / "salut" / "a.py").touch()
(tmp_path / "salut" / "b.py").write_text("""\
import salut.a
""")
(tmp_path / "salut" / "_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", graph=graph)

# Assert
assert arch.layers == [{"b"}, {"a"}]
assert arch.excluded == {"_version"}

def test_version_excluded_with_deps(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
"""_version is excluded even when other modules import it.

Ref: https://github.com/usethis-python/usethis-python/issues/1423
"""
# Arrange
(tmp_path / "salut").mkdir()
(tmp_path / "salut" / "__init__.py").touch()
(tmp_path / "salut" / "a.py").write_text("""\
import salut._version
""")
(tmp_path / "salut" / "b.py").touch()
(tmp_path / "salut" / "_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", graph=graph)

# Assert
assert arch.layers == [{"a"}, {"b"}]
assert arch.excluded == {"_version"}


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