From f2882511f48df861163be9dd7e7d50fe39f1cac3 Mon Sep 17 00:00:00 2001 From: Torben Nehmer Date: Wed, 2 Dec 2026 21:49:48 +0100 Subject: [PATCH] Forbid layer imports in the root package The layers contract does not check chemenu/__init__.py itself, so an import there would load a layer for every importer without any check failing. Co-Authored-By: Claude Opus 5.5 --- docs/architecture.md | 2 +- pyproject.toml | 9 +++++++++ tests/test_import_contracts.py | 10 ++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/docs/architecture.md b/docs/architecture.md index 164d1a5..d6cca36 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -10,7 +10,7 @@ Bis dahin ist [#249](https://gitea.nehmer.net/torben/chemenu/issues/249) maßgeb * `application`: Anwendungsfälle, einer pro Job. Rechteprüfung und Transaktionsgrenzen liegen hier. Hängt nur von Domäne und Ports ab. * `adapters`: Implementierungen der Ports (Git, Dateisystem, Datenbank, LLM, HTTP). Instanz und Nutzer kommen als expliziter Kontext, nie als Modul-Global. -`import-linter` erzwingt die Importrichtung in pre-commit und CI; der Vertrag steht in `pyproject.toml`. +`import-linter` erzwingt die Importrichtung in pre-commit und CI; die Verträge stehen in `pyproject.toml`. `chemenu/__init__.py` importiert keine Schicht. Neben den Schichten steht ein Werkzeug: diff --git a/pyproject.toml b/pyproject.toml index 447275b..aef15cd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -72,3 +72,12 @@ layers = [ "domain", ] exhaustive = true + +# The root package is the container of the layers and not checked as one of them. +[[tool.importlinter.contracts]] +id = "root" +name = "Root package imports no layer" +type = "forbidden" +source_modules = ["chemenu"] +forbidden_modules = ["chemenu.**"] +as_packages = false diff --git a/tests/test_import_contracts.py b/tests/test_import_contracts.py index 15b506a..6602426 100644 --- a/tests/test_import_contracts.py +++ b/tests/test_import_contracts.py @@ -97,6 +97,16 @@ def test_type_checking_import_fails(project: Path) -> None: assert "chemenu.domain is not allowed to import chemenu.adapters:" in result.stdout +@pytest.mark.parametrize("imported", [*PACKAGES, "evals.model"]) +def test_root_package_import_fails(project: Path, imported: str) -> None: + (project / "chemenu" / "__init__.py").write_text( + f"import chemenu.{imported}\n", encoding="utf-8" + ) + result = run_lint(project) + assert result.returncode == 1, result.stdout + result.stderr + assert f"chemenu is not allowed to import chemenu.{imported}:" in result.stdout + + def test_undeclared_package_fails(project: Path) -> None: steps = project / "chemenu" / "steps" steps.mkdir()