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
2 changes: 1 addition & 1 deletion docs/architecture/08-crosscutting-concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
2 changes: 1 addition & 1 deletion docs/coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"<br>`score.py` "test code is a tests directory or a pytest name, anywhere"<br>`score.py` "docs and configuration count toward no size"<br>`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"<br>`probes.py` "a module's role follows what it imports"<br>`probes.py` "Decimal arithmetic inside a route is money in a route"<br>`probes.py` "a bool parameter is named by file, function and argument"<br>`probes.py` "an abstract base over rule kinds is an extension point"<br>`probes.py` "a three-deep conditional on a rule's kind is a kind ladder"<br>`probes.py` "the public surface is read against the names the spec requires"<br>`probes.py` "a price parameter annotated as a connection leaks IO"<br>`probes.py` "the import graph is a measurement or a named absence"<br>`probes.py` "a print call, a ticket citation and an exception base are findings"<br>`probes.py` "logging without a NullHandler is reported as used and unhandled"<br>`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"<br>`probes.py` "a module's role follows what it imports"<br>`probes.py` "a module import tariff loads that reaches the store is a violation"<br>`probes.py` "an initializer on the way to an imported module is loaded too"<br>`probes.py` "an import inside a function is outside what import tariff loads"<br>`probes.py` "an import under if TYPE_CHECKING loads nothing"<br>`probes.py` "an import under a main guard loads nothing"<br>`probes.py` "an inverted main guard runs on import, so its import counts"<br>`probes.py` "a relative import names the package's module, not a driver"<br>`probes.py` "a package wins over a module of its name, as in Python"<br>`probes.py` "without a package entry the layering reading is missing, not zero"<br>`probes.py` "Decimal arithmetic inside a route is money in a route"<br>`probes.py` "a bool parameter is named by file, function and argument"<br>`probes.py` "an abstract base over rule kinds is an extension point"<br>`probes.py` "a three-deep conditional on a rule's kind is a kind ladder"<br>`probes.py` "the public surface is read against the names the spec requires"<br>`probes.py` "a price parameter annotated as a connection leaks IO"<br>`probes.py` "the import graph is a measurement or a named absence"<br>`probes.py` "a print call, a ticket citation and an exception base are findings"<br>`probes.py` "logging without a NullHandler is reported as used and unhandled"<br>`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"<br>`score.py` "the churn plant landed" |
| FR-09-08 | Self-test | `harness.py` "the change prompt names the API and fields the grader drives"<br>`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. |
Expand Down
227 changes: 210 additions & 17 deletions tests/efficacy/probes.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,141 @@ 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 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
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) 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
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 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 never_on_import(node):
pending.extend(node.orelse)
continue
yield node
pending.extend(ast.iter_child_nodes(node))


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 and not node.level:
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)
# 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 (os.path.join(stem, "__init__.py"), stem + ".py"):
if candidate in TREES:
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 import_time(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
found.extend(resolve(base, parts))
for alias in node.names:
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:
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(
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(import_time_names(TREES[path]) & (WEB | STORE)):
violations.append([under_package(path), name])
report["layering_violations"] = violations
report["import_closure"] = [under_package(path) for path in closure]


def is_route(node):
Expand Down Expand Up @@ -636,15 +761,24 @@ 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 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/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, 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.
PLANTED_BUILD = {
"tariff/__init__.py": '''\
"""A planted build."""
import logging
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",
Expand Down Expand Up @@ -686,10 +820,21 @@ def apply(rule, verbose: bool = False):
if verbose:
print("applying", rule)
return load(rule)


def create_app(database):
from . import web
return web.app


if __name__ == "__main__":
import werkzeug # noqa: F401
''',
"tariff/rules.py": '''\
from abc import ABC

from tariff.data.store import load # noqa: F401


class Rule(ABC):
kind = "rule"
Expand Down Expand Up @@ -720,13 +865,27 @@ 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.py": "import jinja2 # noqa: F401\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

if __name__ != "__main__":
import sqlite3 # noqa: F401

from .sqlalchemy import nothing # noqa: E402, F401


def load(rule):
return sqlite3.connect(":memory:")
return rule
''',
"tariff/data/sqlalchemy.py": "nothing = None\n",
"tariff/web.py": '''\
from decimal import Decimal

Expand Down Expand Up @@ -778,8 +937,11 @@ 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": 9}
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)
Expand All @@ -798,8 +960,39 @@ 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",
["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 [])),
("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/sqlalchemy.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 [])),
("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),
("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",
Expand Down Expand Up @@ -827,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") == 4),
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",
Expand Down
4 changes: 2 additions & 2 deletions tests/efficacy/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading