From 8397389a553f6ce21001c67bf175e1855f6fbffd Mon Sep 17 00:00:00 2001 From: Branimir Georgiev <66906831+braboj@users.noreply.github.com> Date: Wed, 4 Nov 2026 13:37:40 +0200 Subject: [PATCH 1/6] fix(efficacy): the layering probe reads what import tariff loads The probe classed each module by what it imports, then looked for domain modules importing Flask or a database driver, which by that definition there never are. It now walks the import closure of the package's __init__, relative and absolute imports alike, and reports every module in it that imports Flask, Werkzeug, Jinja or a database driver. Without a package entry the reading is missing, not zero. Closes #125 Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01DpT5xPGzmuB6WLaUppMRKY --- docs/architecture/08-crosscutting-concepts.md | 2 +- docs/coverage.md | 2 +- tests/efficacy/probes.py | 100 ++++++++++++++++-- tests/efficacy/report.py | 4 +- 4 files changed, 93 insertions(+), 15 deletions(-) diff --git a/docs/architecture/08-crosscutting-concepts.md b/docs/architecture/08-crosscutting-concepts.md index f572e29..87db897 100644 --- a/docs/architecture/08-crosscutting-concepts.md +++ b/docs/architecture/08-crosscutting-concepts.md @@ -310,7 +310,7 @@ The scorer probes each build's structure directly (FR-09-06). | Probe | Asks | | ----- | ---- | -| Layering | Does the domain import neither Flask nor persistence? Do routes do no `Decimal` arithmetic? | +| Layering | Do the modules `import tariff` loads, its `__init__` and what that reaches through the package's own imports, import neither Flask nor persistence? Do routes do no `Decimal` arithmetic? | | Import graph | Mutual imports, and the domain modules' mean instability (Martin), through `grimp` | | Extension points | Can a discount kind, a jurisdiction or an export format be added without editing existing code? Per axis | | Kind ladders | Conditionals that dispatch on a rule's kind | diff --git a/docs/coverage.md b/docs/coverage.md index 402b046..920804e 100644 --- a/docs/coverage.md +++ b/docs/coverage.md @@ -129,7 +129,7 @@ when: | FR-09-03 | Self-test | `score.py` "an hx-* error is set aside and another counts" | | FR-09-04 | Self-test | `score.py` "app code is the app's Python and templates, not its tests"
`score.py` "test code is a tests directory or a pytest name, anywhere"
`score.py` "docs and configuration count toward no size"
`score.py` "committed bytecode and a database are artifacts nobody asked for" | | FR-09-05 | Self-test | `score.py` "the checklist holds 13 items and reads none as any arm" | -| FR-09-06 | Self-test | `probes.py` "every design probe answers in the one structure report"
`probes.py` "a module's role follows what it imports"
`probes.py` "Decimal arithmetic inside a route is money in a route"
`probes.py` "a bool parameter is named by file, function and argument"
`probes.py` "an abstract base over rule kinds is an extension point"
`probes.py` "a three-deep conditional on a rule's kind is a kind ladder"
`probes.py` "the public surface is read against the names the spec requires"
`probes.py` "a price parameter annotated as a connection leaks IO"
`probes.py` "the import graph is a measurement or a named absence"
`probes.py` "a print call, a ticket citation and an exception base are findings"
`probes.py` "logging without a NullHandler is reported as used and unhandled"
`probes.py` "without source roots the structure metric is missing, not zero" | +| FR-09-06 | Self-test | `probes.py` "every design probe answers in the one structure report"
`probes.py` "a module's role follows what it imports"
`probes.py` "a module import tariff loads that reaches the store is a violation"
`probes.py` "a module the package never imports is outside the layering reading"
`probes.py` "without a package entry the layering reading is missing, not zero"
`probes.py` "Decimal arithmetic inside a route is money in a route"
`probes.py` "a bool parameter is named by file, function and argument"
`probes.py` "an abstract base over rule kinds is an extension point"
`probes.py` "a three-deep conditional on a rule's kind is a kind ladder"
`probes.py` "the public surface is read against the names the spec requires"
`probes.py` "a price parameter annotated as a connection leaks IO"
`probes.py` "the import graph is a measurement or a named absence"
`probes.py` "a print call, a ticket citation and an exception base are findings"
`probes.py` "logging without a NullHandler is reported as used and unhandled"
`probes.py` "without source roots the structure metric is missing, not zero" | | FR-09-07 | Self-test | `score.py` "churn counts an edit and a new file, not an environment"
`score.py` "the churn plant landed" | | FR-09-08 | Self-test | `harness.py` "the change prompt names the API and fields the grader drives"
`harness.py` "the change prompt is read whole from its file" | | FR-09-09 | Untestable | An order of work; the round's registration fixes it. | diff --git a/tests/efficacy/probes.py b/tests/efficacy/probes.py index 0716261..a7f1ec8 100644 --- a/tests/efficacy/probes.py +++ b/tests/efficacy/probes.py @@ -119,16 +119,80 @@ def imported_names(tree): roles[path] = "domain" report["roles"] = {os.path.basename(p): r for p, r in roles.items()} + + # The contract the specification states in its own words: `import tariff` -# works without Flask and without touching the database, so no domain module -# may reach either. -violations = [] -for path, tree in TREES.items(): - if roles[path] != "domain": - continue - for name in sorted(imported_names(tree) & (WEB | STORE)): - violations.append([os.path.basename(path), name]) -report["layering_violations"] = violations +# works without Flask and without touching the database. So the modules that +# import loads, the package's `__init__` and every module it reaches through +# the package's own imports, may import neither. A role says nothing here: a +# module that imports Flask is web by that fact, so asking whether a domain +# module imports Flask asks nothing. +def resolve(base, parts): + stem = os.path.join(base, *parts) if parts else base + for candidate in (stem + ".py", os.path.join(stem, "__init__.py")): + if candidate in TREES: + return candidate + return None + + +def package_imports(path, tree): + found = [] + here = os.path.dirname(path) + top = os.path.dirname(ENTRY) + for node in ast.walk(tree): + if isinstance(node, ast.ImportFrom): + if node.level: + base = here + for _ in range(node.level - 1): + base = os.path.dirname(base) + parts = node.module.split(".") if node.module else [] + elif node.module and node.module.split(".")[0] == PACKAGE: + base, parts = top, node.module.split(".")[1:] + else: + continue + target = resolve(base, parts) + if target: + found.append(target) + for alias in node.names: + target = resolve(base, parts + [alias.name]) + if target: + found.append(target) + elif isinstance(node, ast.Import): + for alias in node.names: + parts = alias.name.split(".") + if parts[0] == PACKAGE: + target = resolve(top, parts[1:]) + if target: + found.append(target) + return found + + +ENTRY = None +for path in TREES: + if os.path.basename(path) == "__init__.py" and os.path.basename( + os.path.dirname(path)) == PACKAGE: + ENTRY = path +if ENTRY is None: + for path in TREES: + if os.path.basename(path) == PACKAGE + ".py": + ENTRY = path +if ENTRY is None: + report["layering_violations"] = None + report["import_closure"] = None +else: + closure, queue = [], [ENTRY] + while queue: + path = queue.pop(0) + if path in closure: + continue + closure.append(path) + queue.extend(package_imports(path, TREES[path])) + violations = [] + for path in closure: + for name in sorted(imported_names(TREES[path]) & (WEB | STORE)): + violations.append([os.path.basename(path), name]) + report["layering_violations"] = violations + report["import_closure"] = [os.path.basename(path) for path in closure] def is_route(node): @@ -636,7 +700,9 @@ def web_quality(ctx, workspace): # A build planted with one finding per design probe, so each probe's answer # can be checked against what was planted. `web.py` names Flask but is never -# imported, so the plant needs nothing installed; `broken.py` does not parse. +# imported, so the plant needs nothing installed, and it lies outside what +# `import tariff` loads; `store.py` lies inside it, through an absolute +# import; `broken.py` does not parse. PLANTED_BUILD = { "tariff/__init__.py": '''\ """A planted build.""" @@ -644,7 +710,7 @@ def web_quality(ctx, workspace): from decimal import Decimal from .rules import BulkRule, CouponRule, PercentageRule, Rule, TieredRule -from .store import load +from .rules import load __all__ = ["Product", "Catalog", "InvoiceLine", "Invoice", "Jurisdiction", "PercentageRule", "BulkRule", "TieredRule", "CouponRule", "price", @@ -690,6 +756,8 @@ def apply(rule, verbose: bool = False): "tariff/rules.py": '''\ from abc import ABC +from tariff.store import load # noqa: F401 + class Rule(ABC): kind = "rule" @@ -780,6 +848,9 @@ def self_test(): roots = [os.path.join(workspace, "tariff")] seen = {"files": 5} metric = structure(planted_context(), workspace, roots, seen) + headless = structure(planted_context(), workspace, + [os.path.join(workspace, "tariff", "rules.py")], + seen) rootless = structure(planted_context(), workspace, [], seen) broken = structure(planted_context(failing="exited 1"), workspace, roots, seen) @@ -800,6 +871,13 @@ def self_test(): facts.get("roles", {}).get("web.py") == "web" and facts.get("roles", {}).get("store.py") == "persistence" and facts.get("roles", {}).get("rules.py") == "domain"), + ("a module import tariff loads that reaches the store is a violation", + facts.get("layering_violations") == [["store.py", "sqlite3"]]), + ("a module the package never imports is outside the layering reading", + facts.get("import_closure") == ["__init__.py", "rules.py", "store.py"]), + ("without a package entry the layering reading is missing, not zero", + (headless.get("value") or {}).get("layering_violations") is None + and (headless.get("value") or {}).get("files_parsed") == 1), ("Decimal arithmetic inside a route is money in a route", facts.get("money_in_routes") == [["web.py", "total"]]), ("a bool parameter is named by file, function and argument", diff --git a/tests/efficacy/report.py b/tests/efficacy/report.py index 89c88f4..2e9bf09 100644 --- a/tests/efficacy/report.py +++ b/tests/efficacy/report.py @@ -388,8 +388,8 @@ def pattern_count(trial, verdict): ("extension_points", "Extension axes present, of three", UP, extension_points), - ("layering_violations", "Domain modules reaching Flask or the database", - DOWN, lambda t: structure_count(t, "layering_violations")), + ("layering_violations", "Modules import tariff loads that reach Flask or " + "the database", DOWN, lambda t: structure_count(t, "layering_violations")), ("money_in_routes", "Routes doing pricing arithmetic", DOWN, lambda t: structure_count(t, "money_in_routes")), ("bool_parameters", "Boolean parameters", DOWN, From cde280edea4edbae4000d842906b83633943bce9 Mon Sep 17 00:00:00 2001 From: Branimir Georgiev <66906831+braboj@users.noreply.github.com> Date: Wed, 4 Nov 2026 13:45:47 +0200 Subject: [PATCH 2/6] fix(efficacy): the layering closure follows import-time imports through every initializer An import inside a function body, the factory's lazy import of the web module that the specification allows, is not what import tariff loads, so the closure and the names it counts skip function bodies. A dotted import runs every package initializer on its way, so each one joins the closure. Files are named by their path under the package, which tells two initializers apart. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01DpT5xPGzmuB6WLaUppMRKY --- docs/coverage.md | 2 +- tests/efficacy/probes.py | 106 ++++++++++++++++++++++++++++----------- 2 files changed, 77 insertions(+), 31 deletions(-) diff --git a/docs/coverage.md b/docs/coverage.md index 920804e..2266080 100644 --- a/docs/coverage.md +++ b/docs/coverage.md @@ -129,7 +129,7 @@ when: | FR-09-03 | Self-test | `score.py` "an hx-* error is set aside and another counts" | | FR-09-04 | Self-test | `score.py` "app code is the app's Python and templates, not its tests"
`score.py` "test code is a tests directory or a pytest name, anywhere"
`score.py` "docs and configuration count toward no size"
`score.py` "committed bytecode and a database are artifacts nobody asked for" | | FR-09-05 | Self-test | `score.py` "the checklist holds 13 items and reads none as any arm" | -| FR-09-06 | Self-test | `probes.py` "every design probe answers in the one structure report"
`probes.py` "a module's role follows what it imports"
`probes.py` "a module import tariff loads that reaches the store is a violation"
`probes.py` "a module the package never imports is outside the layering reading"
`probes.py` "without a package entry the layering reading is missing, not zero"
`probes.py` "Decimal arithmetic inside a route is money in a route"
`probes.py` "a bool parameter is named by file, function and argument"
`probes.py` "an abstract base over rule kinds is an extension point"
`probes.py` "a three-deep conditional on a rule's kind is a kind ladder"
`probes.py` "the public surface is read against the names the spec requires"
`probes.py` "a price parameter annotated as a connection leaks IO"
`probes.py` "the import graph is a measurement or a named absence"
`probes.py` "a print call, a ticket citation and an exception base are findings"
`probes.py` "logging without a NullHandler is reported as used and unhandled"
`probes.py` "without source roots the structure metric is missing, not zero" | +| FR-09-06 | Self-test | `probes.py` "every design probe answers in the one structure report"
`probes.py` "a module's role follows what it imports"
`probes.py` "a module import tariff loads that reaches the store is a violation"
`probes.py` "an initializer on the way to an imported module is loaded too"
`probes.py` "an import inside a function is outside what import tariff loads"
`probes.py` "without a package entry the layering reading is missing, not zero"
`probes.py` "Decimal arithmetic inside a route is money in a route"
`probes.py` "a bool parameter is named by file, function and argument"
`probes.py` "an abstract base over rule kinds is an extension point"
`probes.py` "a three-deep conditional on a rule's kind is a kind ladder"
`probes.py` "the public surface is read against the names the spec requires"
`probes.py` "a price parameter annotated as a connection leaks IO"
`probes.py` "the import graph is a measurement or a named absence"
`probes.py` "a print call, a ticket citation and an exception base are findings"
`probes.py` "logging without a NullHandler is reported as used and unhandled"
`probes.py` "without source roots the structure metric is missing, not zero" | | FR-09-07 | Self-test | `score.py` "churn counts an edit and a new file, not an environment"
`score.py` "the churn plant landed" | | FR-09-08 | Self-test | `harness.py` "the change prompt names the API and fields the grader drives"
`harness.py` "the change prompt is read whole from its file" | | FR-09-09 | Untestable | An order of work; the round's registration fixes it. | diff --git a/tests/efficacy/probes.py b/tests/efficacy/probes.py index a7f1ec8..b7a7bfd 100644 --- a/tests/efficacy/probes.py +++ b/tests/efficacy/probes.py @@ -127,19 +127,51 @@ def imported_names(tree): # the package's own imports, may import neither. A role says nothing here: a # module that imports Flask is web by that fact, so asking whether a domain # module imports Flask asks nothing. +def import_time(tree): + """The nodes that run when the module is imported: a function body does + not, so an import inside one, the factory's lazy `from . import web` the + specification allows, is not what `import tariff` loads.""" + pending = list(ast.iter_child_nodes(tree)) + while pending: + node = pending.pop() + if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, + ast.Lambda)): + continue + yield node + pending.extend(ast.iter_child_nodes(node)) + + +def import_time_names(tree): + names = set() + for node in import_time(tree): + if isinstance(node, ast.Import): + names.update(alias.name.split(".")[0] for alias in node.names) + elif isinstance(node, ast.ImportFrom) and node.module: + names.add(node.module.split(".")[0]) + return names + + def resolve(base, parts): + """The files Python runs to import `parts` under `base`: each package + initializer on the way, then the module or its own initializer.""" + found = [] + for depth in range(1, len(parts)): + initializer = os.path.join(base, *(parts[:depth] + ["__init__.py"])) + if initializer in TREES: + found.append(initializer) stem = os.path.join(base, *parts) if parts else base for candidate in (stem + ".py", os.path.join(stem, "__init__.py")): if candidate in TREES: - return candidate - return None + found.append(candidate) + break + return found def package_imports(path, tree): found = [] here = os.path.dirname(path) top = os.path.dirname(ENTRY) - for node in ast.walk(tree): + for node in import_time(tree): if isinstance(node, ast.ImportFrom): if node.level: base = here @@ -150,23 +182,24 @@ def package_imports(path, tree): base, parts = top, node.module.split(".")[1:] else: continue - target = resolve(base, parts) - if target: - found.append(target) + found.extend(resolve(base, parts)) for alias in node.names: - target = resolve(base, parts + [alias.name]) - if target: - found.append(target) + found.extend(resolve(base, parts + [alias.name])) elif isinstance(node, ast.Import): for alias in node.names: parts = alias.name.split(".") if parts[0] == PACKAGE: - target = resolve(top, parts[1:]) - if target: - found.append(target) + found.extend(resolve(top, parts[1:])) return found +def under_package(path): + """A file's path from the package's parent, with forward slashes, so two + initializers are told apart.""" + relative = os.path.relpath(path, os.path.dirname(os.path.dirname(ENTRY))) + return relative.replace(os.sep, "/") + + ENTRY = None for path in TREES: if os.path.basename(path) == "__init__.py" and os.path.basename( @@ -189,10 +222,10 @@ def package_imports(path, tree): queue.extend(package_imports(path, TREES[path])) violations = [] for path in closure: - for name in sorted(imported_names(TREES[path]) & (WEB | STORE)): - violations.append([os.path.basename(path), name]) + for name in sorted(import_time_names(TREES[path]) & (WEB | STORE)): + violations.append([under_package(path), name]) report["layering_violations"] = violations - report["import_closure"] = [os.path.basename(path) for path in closure] + report["import_closure"] = [under_package(path) for path in closure] def is_route(node): @@ -700,9 +733,11 @@ def web_quality(ctx, workspace): # A build planted with one finding per design probe, so each probe's answer # can be checked against what was planted. `web.py` names Flask but is never -# imported, so the plant needs nothing installed, and it lies outside what -# `import tariff` loads; `store.py` lies inside it, through an absolute -# import; `broken.py` does not parse. +# imported at import time, so the plant needs nothing installed, and it lies +# outside what `import tariff` loads though the factory imports it lazily; +# `db.py` is persistence by its imports and lies outside too; +# `data/__init__.py` lies inside, as the initializer on the way to a module an +# absolute import names; `broken.py` does not parse. PLANTED_BUILD = { "tariff/__init__.py": '''\ """A planted build.""" @@ -752,11 +787,16 @@ def apply(rule, verbose: bool = False): if verbose: print("applying", rule) return load(rule) + + +def create_app(database): + from . import web + return web.app ''', "tariff/rules.py": '''\ from abc import ABC -from tariff.store import load # noqa: F401 +from tariff.data.store import load # noqa: F401 class Rule(ABC): @@ -788,12 +828,12 @@ def rate_for(rule): return 3 return 0 ''', - "tariff/store.py": '''\ -import sqlite3 - - + "tariff/db.py": "import sqlite3\n\n\ndef connect():\n" + " return sqlite3.connect(':memory:')\n", + "tariff/data/__init__.py": "import sqlite3 # noqa: F401\n", + "tariff/data/store.py": '''\ def load(rule): - return sqlite3.connect(":memory:") + return rule ''', "tariff/web.py": '''\ from decimal import Decimal @@ -846,7 +886,7 @@ def self_test(): with io.open(path, "w", encoding="utf-8") as handle: handle.write(text) roots = [os.path.join(workspace, "tariff")] - seen = {"files": 5} + seen = {"files": 7} metric = structure(planted_context(), workspace, roots, seen) headless = structure(planted_context(), workspace, [os.path.join(workspace, "tariff", "rules.py")], @@ -869,12 +909,18 @@ def self_test(): metric["missing"] is None and all(key in facts for key in probes)), ("a module's role follows what it imports", facts.get("roles", {}).get("web.py") == "web" - and facts.get("roles", {}).get("store.py") == "persistence" + and facts.get("roles", {}).get("db.py") == "persistence" and facts.get("roles", {}).get("rules.py") == "domain"), ("a module import tariff loads that reaches the store is a violation", - facts.get("layering_violations") == [["store.py", "sqlite3"]]), - ("a module the package never imports is outside the layering reading", - facts.get("import_closure") == ["__init__.py", "rules.py", "store.py"]), + facts.get("layering_violations") + == [["tariff/data/__init__.py", "sqlite3"]]), + ("an initializer on the way to an imported module is loaded too", + "tariff/data/__init__.py" in facts.get("import_closure", []) + and "tariff/data/store.py" in facts.get("import_closure", [])), + ("an import inside a function is outside what import tariff loads", + facts.get("import_closure") + == ["tariff/__init__.py", "tariff/rules.py", "tariff/data/__init__.py", + "tariff/data/store.py"]), ("without a package entry the layering reading is missing, not zero", (headless.get("value") or {}).get("layering_violations") is None and (headless.get("value") or {}).get("files_parsed") == 1), @@ -905,7 +951,7 @@ def self_test(): ("a file that does not parse is named, and the rest are read", [entry[0] for entry in facts.get("unparsed", [])] == [os.path.join(workspace, "tariff", "broken.py")] - and facts.get("files_parsed") == 4), + and facts.get("files_parsed") == 6), ("without source roots the structure metric is missing, not zero", rootless["value"] is None and "no source roots" in rootless["missing"]), ("a probe that fails leaves its metric missing, with the command", From 86b902a6e63b70de81a9f5e1b77717df987e5159 Mon Sep 17 00:00:00 2001 From: Branimir Georgiev <66906831+braboj@users.noreply.github.com> Date: Wed, 4 Nov 2026 13:53:13 +0200 Subject: [PATCH 3/6] fix(efficacy): an import under if TYPE_CHECKING is not what import tariff loads Only a type checker reads such a branch, so the closure skips its body and takes its else branch. The planted store names Flask and the web module under the guard; neither reads as loaded. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01DpT5xPGzmuB6WLaUppMRKY --- docs/coverage.md | 2 +- tests/efficacy/probes.py | 30 +++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/docs/coverage.md b/docs/coverage.md index 2266080..571fe67 100644 --- a/docs/coverage.md +++ b/docs/coverage.md @@ -129,7 +129,7 @@ when: | FR-09-03 | Self-test | `score.py` "an hx-* error is set aside and another counts" | | FR-09-04 | Self-test | `score.py` "app code is the app's Python and templates, not its tests"
`score.py` "test code is a tests directory or a pytest name, anywhere"
`score.py` "docs and configuration count toward no size"
`score.py` "committed bytecode and a database are artifacts nobody asked for" | | FR-09-05 | Self-test | `score.py` "the checklist holds 13 items and reads none as any arm" | -| FR-09-06 | Self-test | `probes.py` "every design probe answers in the one structure report"
`probes.py` "a module's role follows what it imports"
`probes.py` "a module import tariff loads that reaches the store is a violation"
`probes.py` "an initializer on the way to an imported module is loaded too"
`probes.py` "an import inside a function is outside what import tariff loads"
`probes.py` "without a package entry the layering reading is missing, not zero"
`probes.py` "Decimal arithmetic inside a route is money in a route"
`probes.py` "a bool parameter is named by file, function and argument"
`probes.py` "an abstract base over rule kinds is an extension point"
`probes.py` "a three-deep conditional on a rule's kind is a kind ladder"
`probes.py` "the public surface is read against the names the spec requires"
`probes.py` "a price parameter annotated as a connection leaks IO"
`probes.py` "the import graph is a measurement or a named absence"
`probes.py` "a print call, a ticket citation and an exception base are findings"
`probes.py` "logging without a NullHandler is reported as used and unhandled"
`probes.py` "without source roots the structure metric is missing, not zero" | +| FR-09-06 | Self-test | `probes.py` "every design probe answers in the one structure report"
`probes.py` "a module's role follows what it imports"
`probes.py` "a module import tariff loads that reaches the store is a violation"
`probes.py` "an initializer on the way to an imported module is loaded too"
`probes.py` "an import inside a function is outside what import tariff loads"
`probes.py` "an import under if TYPE_CHECKING loads nothing"
`probes.py` "without a package entry the layering reading is missing, not zero"
`probes.py` "Decimal arithmetic inside a route is money in a route"
`probes.py` "a bool parameter is named by file, function and argument"
`probes.py` "an abstract base over rule kinds is an extension point"
`probes.py` "a three-deep conditional on a rule's kind is a kind ladder"
`probes.py` "the public surface is read against the names the spec requires"
`probes.py` "a price parameter annotated as a connection leaks IO"
`probes.py` "the import graph is a measurement or a named absence"
`probes.py` "a print call, a ticket citation and an exception base are findings"
`probes.py` "logging without a NullHandler is reported as used and unhandled"
`probes.py` "without source roots the structure metric is missing, not zero" | | FR-09-07 | Self-test | `score.py` "churn counts an edit and a new file, not an environment"
`score.py` "the churn plant landed" | | FR-09-08 | Self-test | `harness.py` "the change prompt names the API and fields the grader drives"
`harness.py` "the change prompt is read whole from its file" | | FR-09-09 | Untestable | An order of work; the round's registration fixes it. | diff --git a/tests/efficacy/probes.py b/tests/efficacy/probes.py index b7a7bfd..10c3587 100644 --- a/tests/efficacy/probes.py +++ b/tests/efficacy/probes.py @@ -127,16 +127,27 @@ def imported_names(tree): # the package's own imports, may import neither. A role says nothing here: a # module that imports Flask is web by that fact, so asking whether a domain # module imports Flask asks nothing. +def type_checking_only(node): + test = node.test + return ((isinstance(test, ast.Name) and test.id == "TYPE_CHECKING") + or (isinstance(test, ast.Attribute) + and test.attr == "TYPE_CHECKING")) + + def import_time(tree): - """The nodes that run when the module is imported: a function body does + """The nodes that run when the module is imported. A function body does not, so an import inside one, the factory's lazy `from . import web` the - specification allows, is not what `import tariff` loads.""" + specification allows, is not what `import tariff` loads; nor is an import + under `if TYPE_CHECKING:`, which only a type checker reads.""" pending = list(ast.iter_child_nodes(tree)) while pending: node = pending.pop() if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.Lambda)): continue + if isinstance(node, ast.If) and type_checking_only(node): + pending.extend(node.orelse) + continue yield node pending.extend(ast.iter_child_nodes(node)) @@ -735,7 +746,9 @@ def web_quality(ctx, workspace): # can be checked against what was planted. `web.py` names Flask but is never # imported at import time, so the plant needs nothing installed, and it lies # outside what `import tariff` loads though the factory imports it lazily; -# `db.py` is persistence by its imports and lies outside too; +# `db.py` is persistence by its imports and lies outside too; `data/store.py` +# names Flask and the web module under `if TYPE_CHECKING:`, which loads +# neither; # `data/__init__.py` lies inside, as the initializer on the way to a module an # absolute import names; `broken.py` does not parse. PLANTED_BUILD = { @@ -832,6 +845,13 @@ def rate_for(rule): " return sqlite3.connect(':memory:')\n", "tariff/data/__init__.py": "import sqlite3 # noqa: F401\n", "tariff/data/store.py": '''\ +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from flask import Flask # noqa: F401 + from tariff import web # noqa: F401 + + def load(rule): return rule ''', @@ -921,6 +941,10 @@ def self_test(): facts.get("import_closure") == ["tariff/__init__.py", "tariff/rules.py", "tariff/data/__init__.py", "tariff/data/store.py"]), + ("an import under if TYPE_CHECKING loads nothing", + not any(name == "flask" for _, name in + facts.get("layering_violations") or []) + and "tariff/web.py" not in (facts.get("import_closure") or [])), ("without a package entry the layering reading is missing, not zero", (headless.get("value") or {}).get("layering_violations") is None and (headless.get("value") or {}).get("files_parsed") == 1), From a3c09fc283dcb6b64e24e7bc93b1a3381ca819df Mon Sep 17 00:00:00 2001 From: Branimir Georgiev <66906831+braboj@users.noreply.github.com> Date: Wed, 4 Nov 2026 14:05:44 +0200 Subject: [PATCH 4/6] fix(efficacy): a main guard loads nothing, and a package wins over a module An import under if __name__ == "__main__" runs only for a script, so the closure skips it as it skips a type-checking guard. Where a package and a module share a name, the package is what Python imports, so the resolver takes it first. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01DpT5xPGzmuB6WLaUppMRKY --- docs/coverage.md | 2 +- tests/efficacy/probes.py | 46 +++++++++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/docs/coverage.md b/docs/coverage.md index 571fe67..4316a2a 100644 --- a/docs/coverage.md +++ b/docs/coverage.md @@ -129,7 +129,7 @@ when: | FR-09-03 | Self-test | `score.py` "an hx-* error is set aside and another counts" | | FR-09-04 | Self-test | `score.py` "app code is the app's Python and templates, not its tests"
`score.py` "test code is a tests directory or a pytest name, anywhere"
`score.py` "docs and configuration count toward no size"
`score.py` "committed bytecode and a database are artifacts nobody asked for" | | FR-09-05 | Self-test | `score.py` "the checklist holds 13 items and reads none as any arm" | -| FR-09-06 | Self-test | `probes.py` "every design probe answers in the one structure report"
`probes.py` "a module's role follows what it imports"
`probes.py` "a module import tariff loads that reaches the store is a violation"
`probes.py` "an initializer on the way to an imported module is loaded too"
`probes.py` "an import inside a function is outside what import tariff loads"
`probes.py` "an import under if TYPE_CHECKING loads nothing"
`probes.py` "without a package entry the layering reading is missing, not zero"
`probes.py` "Decimal arithmetic inside a route is money in a route"
`probes.py` "a bool parameter is named by file, function and argument"
`probes.py` "an abstract base over rule kinds is an extension point"
`probes.py` "a three-deep conditional on a rule's kind is a kind ladder"
`probes.py` "the public surface is read against the names the spec requires"
`probes.py` "a price parameter annotated as a connection leaks IO"
`probes.py` "the import graph is a measurement or a named absence"
`probes.py` "a print call, a ticket citation and an exception base are findings"
`probes.py` "logging without a NullHandler is reported as used and unhandled"
`probes.py` "without source roots the structure metric is missing, not zero" | +| FR-09-06 | Self-test | `probes.py` "every design probe answers in the one structure report"
`probes.py` "a module's role follows what it imports"
`probes.py` "a module import tariff loads that reaches the store is a violation"
`probes.py` "an initializer on the way to an imported module is loaded too"
`probes.py` "an import inside a function is outside what import tariff loads"
`probes.py` "an import under if TYPE_CHECKING loads nothing"
`probes.py` "an import under a main guard loads nothing"
`probes.py` "a package wins over a module of its name, as in Python"
`probes.py` "without a package entry the layering reading is missing, not zero"
`probes.py` "Decimal arithmetic inside a route is money in a route"
`probes.py` "a bool parameter is named by file, function and argument"
`probes.py` "an abstract base over rule kinds is an extension point"
`probes.py` "a three-deep conditional on a rule's kind is a kind ladder"
`probes.py` "the public surface is read against the names the spec requires"
`probes.py` "a price parameter annotated as a connection leaks IO"
`probes.py` "the import graph is a measurement or a named absence"
`probes.py` "a print call, a ticket citation and an exception base are findings"
`probes.py` "logging without a NullHandler is reported as used and unhandled"
`probes.py` "without source roots the structure metric is missing, not zero" | | FR-09-07 | Self-test | `score.py` "churn counts an edit and a new file, not an environment"
`score.py` "the churn plant landed" | | FR-09-08 | Self-test | `harness.py` "the change prompt names the API and fields the grader drives"
`harness.py` "the change prompt is read whole from its file" | | FR-09-09 | Untestable | An order of work; the round's registration fixes it. | diff --git a/tests/efficacy/probes.py b/tests/efficacy/probes.py index 10c3587..d727480 100644 --- a/tests/efficacy/probes.py +++ b/tests/efficacy/probes.py @@ -127,25 +127,36 @@ def imported_names(tree): # the package's own imports, may import neither. A role says nothing here: a # module that imports Flask is web by that fact, so asking whether a domain # module imports Flask asks nothing. -def type_checking_only(node): +def never_on_import(node): + """An `if` whose body never runs on import: `if TYPE_CHECKING:`, which + only a type checker reads, and `if __name__ == "__main__":`, which runs + only for a script. Both are read by name, as a build writes them.""" test = node.test - return ((isinstance(test, ast.Name) and test.id == "TYPE_CHECKING") - or (isinstance(test, ast.Attribute) - and test.attr == "TYPE_CHECKING")) + if isinstance(test, ast.Name) and test.id == "TYPE_CHECKING": + return True + if isinstance(test, ast.Attribute) and test.attr == "TYPE_CHECKING": + return True + if isinstance(test, ast.Compare): + sides = [test.left] + list(test.comparators) + names = [side.id for side in sides if isinstance(side, ast.Name)] + values = [side.value for side in sides + if isinstance(side, ast.Constant)] + return "__name__" in names and "__main__" in values + return False def import_time(tree): """The nodes that run when the module is imported. A function body does not, so an import inside one, the factory's lazy `from . import web` the specification allows, is not what `import tariff` loads; nor is an import - under `if TYPE_CHECKING:`, which only a type checker reads.""" + under a guard that never holds on import.""" pending = list(ast.iter_child_nodes(tree)) while pending: node = pending.pop() if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.Lambda)): continue - if isinstance(node, ast.If) and type_checking_only(node): + if isinstance(node, ast.If) and never_on_import(node): pending.extend(node.orelse) continue yield node @@ -170,8 +181,10 @@ def resolve(base, parts): initializer = os.path.join(base, *(parts[:depth] + ["__init__.py"])) if initializer in TREES: found.append(initializer) + # A package wins over a module of the same name, as Python's finder has + # it. stem = os.path.join(base, *parts) if parts else base - for candidate in (stem + ".py", os.path.join(stem, "__init__.py")): + for candidate in (os.path.join(stem, "__init__.py"), stem + ".py"): if candidate in TREES: found.append(candidate) break @@ -748,7 +761,8 @@ def web_quality(ctx, workspace): # outside what `import tariff` loads though the factory imports it lazily; # `db.py` is persistence by its imports and lies outside too; `data/store.py` # names Flask and the web module under `if TYPE_CHECKING:`, which loads -# neither; +# neither, and `__init__.py` names Werkzeug under a main guard; `data.py` +# loses to the `data` package of the same name, as in Python; # `data/__init__.py` lies inside, as the initializer on the way to a module an # absolute import names; `broken.py` does not parse. PLANTED_BUILD = { @@ -805,6 +819,10 @@ def apply(rule, verbose: bool = False): def create_app(database): from . import web return web.app + + +if __name__ == "__main__": + import werkzeug # noqa: F401 ''', "tariff/rules.py": '''\ from abc import ABC @@ -843,6 +861,7 @@ def rate_for(rule): ''', "tariff/db.py": "import sqlite3\n\n\ndef connect():\n" " return sqlite3.connect(':memory:')\n", + "tariff/data.py": "import jinja2 # noqa: F401\n", "tariff/data/__init__.py": "import sqlite3 # noqa: F401\n", "tariff/data/store.py": '''\ from typing import TYPE_CHECKING @@ -906,7 +925,7 @@ def self_test(): with io.open(path, "w", encoding="utf-8") as handle: handle.write(text) roots = [os.path.join(workspace, "tariff")] - seen = {"files": 7} + seen = {"files": 8} metric = structure(planted_context(), workspace, roots, seen) headless = structure(planted_context(), workspace, [os.path.join(workspace, "tariff", "rules.py")], @@ -945,6 +964,13 @@ def self_test(): not any(name == "flask" for _, name in facts.get("layering_violations") or []) and "tariff/web.py" not in (facts.get("import_closure") or [])), + ("an import under a main guard loads nothing", + not any(name == "werkzeug" for _, name in + facts.get("layering_violations") or [])), + ("a package wins over a module of its name, as in Python", + "tariff/data.py" not in (facts.get("import_closure") or []) + and not any(name == "jinja2" for _, name in + facts.get("layering_violations") or [])), ("without a package entry the layering reading is missing, not zero", (headless.get("value") or {}).get("layering_violations") is None and (headless.get("value") or {}).get("files_parsed") == 1), @@ -975,7 +1001,7 @@ def self_test(): ("a file that does not parse is named, and the rest are read", [entry[0] for entry in facts.get("unparsed", [])] == [os.path.join(workspace, "tariff", "broken.py")] - and facts.get("files_parsed") == 6), + and facts.get("files_parsed") == 7), ("without source roots the structure metric is missing, not zero", rootless["value"] is None and "no source roots" in rootless["missing"]), ("a probe that fails leaves its metric missing, with the command", From 07a27209882486e653ebc0a5046ee7ebd846fba6 Mon Sep 17 00:00:00 2001 From: Branimir Georgiev <66906831+braboj@users.noreply.github.com> Date: Wed, 4 Nov 2026 14:12:06 +0200 Subject: [PATCH 5/6] fix(efficacy): only an equality with __main__ is a main guard An inverted guard, if __name__ != "__main__", runs on import, so its imports count. The planted store carries one over sqlite3. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01DpT5xPGzmuB6WLaUppMRKY --- docs/coverage.md | 2 +- tests/efficacy/probes.py | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/docs/coverage.md b/docs/coverage.md index 4316a2a..5176a85 100644 --- a/docs/coverage.md +++ b/docs/coverage.md @@ -129,7 +129,7 @@ when: | FR-09-03 | Self-test | `score.py` "an hx-* error is set aside and another counts" | | FR-09-04 | Self-test | `score.py` "app code is the app's Python and templates, not its tests"
`score.py` "test code is a tests directory or a pytest name, anywhere"
`score.py` "docs and configuration count toward no size"
`score.py` "committed bytecode and a database are artifacts nobody asked for" | | FR-09-05 | Self-test | `score.py` "the checklist holds 13 items and reads none as any arm" | -| FR-09-06 | Self-test | `probes.py` "every design probe answers in the one structure report"
`probes.py` "a module's role follows what it imports"
`probes.py` "a module import tariff loads that reaches the store is a violation"
`probes.py` "an initializer on the way to an imported module is loaded too"
`probes.py` "an import inside a function is outside what import tariff loads"
`probes.py` "an import under if TYPE_CHECKING loads nothing"
`probes.py` "an import under a main guard loads nothing"
`probes.py` "a package wins over a module of its name, as in Python"
`probes.py` "without a package entry the layering reading is missing, not zero"
`probes.py` "Decimal arithmetic inside a route is money in a route"
`probes.py` "a bool parameter is named by file, function and argument"
`probes.py` "an abstract base over rule kinds is an extension point"
`probes.py` "a three-deep conditional on a rule's kind is a kind ladder"
`probes.py` "the public surface is read against the names the spec requires"
`probes.py` "a price parameter annotated as a connection leaks IO"
`probes.py` "the import graph is a measurement or a named absence"
`probes.py` "a print call, a ticket citation and an exception base are findings"
`probes.py` "logging without a NullHandler is reported as used and unhandled"
`probes.py` "without source roots the structure metric is missing, not zero" | +| FR-09-06 | Self-test | `probes.py` "every design probe answers in the one structure report"
`probes.py` "a module's role follows what it imports"
`probes.py` "a module import tariff loads that reaches the store is a violation"
`probes.py` "an initializer on the way to an imported module is loaded too"
`probes.py` "an import inside a function is outside what import tariff loads"
`probes.py` "an import under if TYPE_CHECKING loads nothing"
`probes.py` "an import under a main guard loads nothing"
`probes.py` "an inverted main guard runs on import, so its import counts"
`probes.py` "a package wins over a module of its name, as in Python"
`probes.py` "without a package entry the layering reading is missing, not zero"
`probes.py` "Decimal arithmetic inside a route is money in a route"
`probes.py` "a bool parameter is named by file, function and argument"
`probes.py` "an abstract base over rule kinds is an extension point"
`probes.py` "a three-deep conditional on a rule's kind is a kind ladder"
`probes.py` "the public surface is read against the names the spec requires"
`probes.py` "a price parameter annotated as a connection leaks IO"
`probes.py` "the import graph is a measurement or a named absence"
`probes.py` "a print call, a ticket citation and an exception base are findings"
`probes.py` "logging without a NullHandler is reported as used and unhandled"
`probes.py` "without source roots the structure metric is missing, not zero" | | FR-09-07 | Self-test | `score.py` "churn counts an edit and a new file, not an environment"
`score.py` "the churn plant landed" | | FR-09-08 | Self-test | `harness.py` "the change prompt names the API and fields the grader drives"
`harness.py` "the change prompt is read whole from its file" | | FR-09-09 | Untestable | An order of work; the round's registration fixes it. | diff --git a/tests/efficacy/probes.py b/tests/efficacy/probes.py index d727480..14779ab 100644 --- a/tests/efficacy/probes.py +++ b/tests/efficacy/probes.py @@ -136,7 +136,8 @@ def never_on_import(node): return True if isinstance(test, ast.Attribute) and test.attr == "TYPE_CHECKING": return True - if isinstance(test, ast.Compare): + if isinstance(test, ast.Compare) and len(test.ops) == 1 and isinstance( + test.ops[0], ast.Eq): sides = [test.left] + list(test.comparators) names = [side.id for side in sides if isinstance(side, ast.Name)] values = [side.value for side in sides @@ -761,7 +762,8 @@ def web_quality(ctx, workspace): # outside what `import tariff` loads though the factory imports it lazily; # `db.py` is persistence by its imports and lies outside too; `data/store.py` # names Flask and the web module under `if TYPE_CHECKING:`, which loads -# neither, and `__init__.py` names Werkzeug under a main guard; `data.py` +# neither, and sqlite3 under an inverted main guard, which does run on +# import; `__init__.py` names Werkzeug under a main guard; `data.py` # loses to the `data` package of the same name, as in Python; # `data/__init__.py` lies inside, as the initializer on the way to a module an # absolute import names; `broken.py` does not parse. @@ -870,6 +872,9 @@ def rate_for(rule): from flask import Flask # noqa: F401 from tariff import web # noqa: F401 +if __name__ != "__main__": + import sqlite3 # noqa: F401 + def load(rule): return rule @@ -951,8 +956,11 @@ def self_test(): and facts.get("roles", {}).get("db.py") == "persistence" and facts.get("roles", {}).get("rules.py") == "domain"), ("a module import tariff loads that reaches the store is a violation", - facts.get("layering_violations") - == [["tariff/data/__init__.py", "sqlite3"]]), + ["tariff/data/__init__.py", "sqlite3"] + in (facts.get("layering_violations") or [])), + ("an inverted main guard runs on import, so its import counts", + ["tariff/data/store.py", "sqlite3"] + in (facts.get("layering_violations") or [])), ("an initializer on the way to an imported module is loaded too", "tariff/data/__init__.py" in facts.get("import_closure", []) and "tariff/data/store.py" in facts.get("import_closure", [])), From 8decf110517f10df05084044f7ae47f790776d6e Mon Sep 17 00:00:00 2001 From: Branimir Georgiev <66906831+braboj@users.noreply.github.com> Date: Wed, 4 Nov 2026 14:21:45 +0200 Subject: [PATCH 6/6] fix(efficacy): a relative import is the package's own module, not a driver from .sqlite3 import x imports tariff.sqlite3, so the layering count reads only absolute imports; the relative ones the closure already follows. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01DpT5xPGzmuB6WLaUppMRKY --- docs/coverage.md | 2 +- tests/efficacy/probes.py | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/docs/coverage.md b/docs/coverage.md index 5176a85..cbf3770 100644 --- a/docs/coverage.md +++ b/docs/coverage.md @@ -129,7 +129,7 @@ when: | FR-09-03 | Self-test | `score.py` "an hx-* error is set aside and another counts" | | FR-09-04 | Self-test | `score.py` "app code is the app's Python and templates, not its tests"
`score.py` "test code is a tests directory or a pytest name, anywhere"
`score.py` "docs and configuration count toward no size"
`score.py` "committed bytecode and a database are artifacts nobody asked for" | | FR-09-05 | Self-test | `score.py` "the checklist holds 13 items and reads none as any arm" | -| FR-09-06 | Self-test | `probes.py` "every design probe answers in the one structure report"
`probes.py` "a module's role follows what it imports"
`probes.py` "a module import tariff loads that reaches the store is a violation"
`probes.py` "an initializer on the way to an imported module is loaded too"
`probes.py` "an import inside a function is outside what import tariff loads"
`probes.py` "an import under if TYPE_CHECKING loads nothing"
`probes.py` "an import under a main guard loads nothing"
`probes.py` "an inverted main guard runs on import, so its import counts"
`probes.py` "a package wins over a module of its name, as in Python"
`probes.py` "without a package entry the layering reading is missing, not zero"
`probes.py` "Decimal arithmetic inside a route is money in a route"
`probes.py` "a bool parameter is named by file, function and argument"
`probes.py` "an abstract base over rule kinds is an extension point"
`probes.py` "a three-deep conditional on a rule's kind is a kind ladder"
`probes.py` "the public surface is read against the names the spec requires"
`probes.py` "a price parameter annotated as a connection leaks IO"
`probes.py` "the import graph is a measurement or a named absence"
`probes.py` "a print call, a ticket citation and an exception base are findings"
`probes.py` "logging without a NullHandler is reported as used and unhandled"
`probes.py` "without source roots the structure metric is missing, not zero" | +| FR-09-06 | Self-test | `probes.py` "every design probe answers in the one structure report"
`probes.py` "a module's role follows what it imports"
`probes.py` "a module import tariff loads that reaches the store is a violation"
`probes.py` "an initializer on the way to an imported module is loaded too"
`probes.py` "an import inside a function is outside what import tariff loads"
`probes.py` "an import under if TYPE_CHECKING loads nothing"
`probes.py` "an import under a main guard loads nothing"
`probes.py` "an inverted main guard runs on import, so its import counts"
`probes.py` "a relative import names the package's module, not a driver"
`probes.py` "a package wins over a module of its name, as in Python"
`probes.py` "without a package entry the layering reading is missing, not zero"
`probes.py` "Decimal arithmetic inside a route is money in a route"
`probes.py` "a bool parameter is named by file, function and argument"
`probes.py` "an abstract base over rule kinds is an extension point"
`probes.py` "a three-deep conditional on a rule's kind is a kind ladder"
`probes.py` "the public surface is read against the names the spec requires"
`probes.py` "a price parameter annotated as a connection leaks IO"
`probes.py` "the import graph is a measurement or a named absence"
`probes.py` "a print call, a ticket citation and an exception base are findings"
`probes.py` "logging without a NullHandler is reported as used and unhandled"
`probes.py` "without source roots the structure metric is missing, not zero" | | FR-09-07 | Self-test | `score.py` "churn counts an edit and a new file, not an environment"
`score.py` "the churn plant landed" | | FR-09-08 | Self-test | `harness.py` "the change prompt names the API and fields the grader drives"
`harness.py` "the change prompt is read whole from its file" | | FR-09-09 | Untestable | An order of work; the round's registration fixes it. | diff --git a/tests/efficacy/probes.py b/tests/efficacy/probes.py index 14779ab..3cc0d23 100644 --- a/tests/efficacy/probes.py +++ b/tests/efficacy/probes.py @@ -165,11 +165,14 @@ def import_time(tree): def import_time_names(tree): + """The top-level names a module imports at import time. A relative + import names the package's own module, whatever it is called, and is + followed by `package_imports` instead.""" names = set() for node in import_time(tree): if isinstance(node, ast.Import): names.update(alias.name.split(".")[0] for alias in node.names) - elif isinstance(node, ast.ImportFrom) and node.module: + elif isinstance(node, ast.ImportFrom) and node.module and not node.level: names.add(node.module.split(".")[0]) return names @@ -763,7 +766,8 @@ def web_quality(ctx, workspace): # `db.py` is persistence by its imports and lies outside too; `data/store.py` # names Flask and the web module under `if TYPE_CHECKING:`, which loads # neither, and sqlite3 under an inverted main guard, which does run on -# import; `__init__.py` names Werkzeug under a main guard; `data.py` +# import, and imports the package's own `data/sqlalchemy.py` relatively; +# `__init__.py` names Werkzeug under a main guard; `data.py` # loses to the `data` package of the same name, as in Python; # `data/__init__.py` lies inside, as the initializer on the way to a module an # absolute import names; `broken.py` does not parse. @@ -875,10 +879,13 @@ def rate_for(rule): if __name__ != "__main__": import sqlite3 # noqa: F401 +from .sqlalchemy import nothing # noqa: E402, F401 + def load(rule): return rule ''', + "tariff/data/sqlalchemy.py": "nothing = None\n", "tariff/web.py": '''\ from decimal import Decimal @@ -930,7 +937,7 @@ def self_test(): with io.open(path, "w", encoding="utf-8") as handle: handle.write(text) roots = [os.path.join(workspace, "tariff")] - seen = {"files": 8} + seen = {"files": 9} metric = structure(planted_context(), workspace, roots, seen) headless = structure(planted_context(), workspace, [os.path.join(workspace, "tariff", "rules.py")], @@ -961,13 +968,17 @@ def self_test(): ("an inverted main guard runs on import, so its import counts", ["tariff/data/store.py", "sqlite3"] in (facts.get("layering_violations") or [])), + ("a relative import names the package's module, not a driver", + "tariff/data/sqlalchemy.py" in (facts.get("import_closure") or []) + and not any(name == "sqlalchemy" for _, name in + facts.get("layering_violations") or [])), ("an initializer on the way to an imported module is loaded too", "tariff/data/__init__.py" in facts.get("import_closure", []) and "tariff/data/store.py" in facts.get("import_closure", [])), ("an import inside a function is outside what import tariff loads", facts.get("import_closure") == ["tariff/__init__.py", "tariff/rules.py", "tariff/data/__init__.py", - "tariff/data/store.py"]), + "tariff/data/store.py", "tariff/data/sqlalchemy.py"]), ("an import under if TYPE_CHECKING loads nothing", not any(name == "flask" for _, name in facts.get("layering_violations") or []) @@ -1009,7 +1020,7 @@ def self_test(): ("a file that does not parse is named, and the rest are read", [entry[0] for entry in facts.get("unparsed", [])] == [os.path.join(workspace, "tariff", "broken.py")] - and facts.get("files_parsed") == 7), + and facts.get("files_parsed") == 8), ("without source roots the structure metric is missing, not zero", rootless["value"] is None and "no source roots" in rootless["missing"]), ("a probe that fails leaves its metric missing, with the command",