Repository navigation
feat: report imports across the app boundary in the check - #193
Conversation
Task 4 of the boundary check: neolith._check_imports reports neolith.E101 to E105 and W105 over the hosted apps' source and contract packages, as a function over a host that task 7 wires into the command.
A contract package that reached its own app's internals through code in no app gave no finding, so importing it loaded the internals: E104 now follows chains from it as E103 does. E103 now counts the packages above the importing module as imported too (ADR 0030 Decision 2), so a no-app package above a hosted app that loads another app's internals is found. Tests also cover the corners whose rules no test held: a module under a models package that no model names, relative imports and packages above inside a chain, a nested undeclared app, a multi-table child's generic relation, and a contract module importing another app's internals.
davinerdo
left a comment
There was a problem hiding this comment.
Verdict: Changes needed. E103 from a package above the app is missed or repeated depending on unrelated imports, and E105 gives the wrong line for common shapes.
-
src/neolith/_check_imports.py:267-271 — E103 from a no-app package above the importing app depends on which imports the app happens to have. The starts are
[*_above(target), target], soproj/__init__.pyreaching another app's internals is only found through an import whose target lies underproj. In a probe with hostedbillingand hostedproj.side, andproj/__init__.pydoingimport billing.services: ifproj/side/views.pyonly doesimport json, nothing is reported. If its modules dofrom . import helpers,from .helpers import x,import proj.side.helpers, andforms.pydoesfrom .helpers import x, you get four identical E103s, each saying "imports proj.side.helpers; through … (proj)", though none of those imports runsproj. It already ran before the importing module. So the same fault inproj/__init__.pyis either missed or reported once per relative import, at lines that aren't the cause. The module docstring and the body ("the importer's own included") claim it covers every app under such a package. Fix: take the importer's packages out of the per-import starts (self.runs(target, module.file.module), as in the first commit), and judge the packages above each hosted app's package once per app, as one E103 at the app's__init__.py, line 1, naming the package and its chain (for exampleapp side: package proj.side: Python runs proj before it; through first-party code in no hosted app (proj), it reaches billing.services, …). Test: the app with onlyimport json, and the app with the four relative imports, each give exactly one E103. Body sentence for the E103 bullet: "A package in no app above a hosted app's package is judged once per app, at the app's__init__.py, line 1, since Python runs it before any module of the app; an import gives E103 only through the modules Python runs for it that are not above the importer." -
src/neolith/_check_imports.py:442-450 — E105's line is wrong whenever the model or the implementation isn't a plain top-level
class._bindsreads onlytree.body, and models are matched by bare class name at any depth. Probes: an implementationclass Serviceinsidetry: … except ImportError:givescontracts.py:1; one insideif …:next to a model gives the model's line (21) instead of 14; a nestedclass Holder: class Thingbefore the real modelThing, built in a function, gives the holder's line. The body promises "at the first line of its model or implementation", and the line is what the author jumps to. Fix: take each model's line from_check_models._source(model), as W105 already does (inspectfinds the class by qualname). Find the implementation'sclass/defby name in every module-scope block (if,try/except/else/finally,with,for,while,match), never inside adeforclass. Add a test per form. The host only accepts a top-level class whose own path is the implementation path (_host.py:957-969), so the import, assignment anddefbranches of_bindscan never match an implementation: drop them rather than test them. -
tests/test_check_chains.py — Several rules have no test that fails when they go. In a 61-mutant pass over
_check_imports.py, 36 were killed and 9 survivors are equivalent; these are real gaps:own()withoutname not in self.public:till/records.pydoesimport helpers.back, andhelpers/back.pydoesimport till.contracts. The mutant reports a false E104 ("reaches till.contracts, a module of app till outside its contract package"); the head correctly reports nothing. A helper that imports back into the contract package is an ordinary cycle, so pin it.- Which hit and which chain is named:
sorted(set(hits))→hits,_hits[name][0]→[-1], andmin→maxat the step inshortestall survive. Use a node that imports two internals (b.zetaon line 1,b.alphaon line 2) and a node with two followers at equal distance. The Hypothesis test checks only the chain's length and edges, so a fixture has to pin the names. - Message forms: E105's plural "models" / "contract modules", and the E104 "in the contract package of app X" label when a module is public through two apps (
of = labels[0]survives). - E105's line with a model and an implementation on different lines (
min→maxsurvives); this comes with finding 2's tests.
Direction: Findings 1 and 2 have one cause: a rule reads a narrower view than the thing it describes. E103 found "the packages Python runs" through the text of each import, not from where the app's package sits. E105 looked for "the line that defines X" in tree.body only, the same top-level-only gap as E108's module-level blocks on #130. Before the next push, list for each rule the facts it claims (what Python runs, where a name is defined), and derive each one from its source: the app's package for what runs first, and every module-scope block (or inspect) for definitions. Then add one fixture where the fact holds and nothing in the import text shows it.
Checked: the cited CPython 3.12.13 / 3.15.0b1 and Django 5.2.19 lines, and the CI counts in the job logs (11,558 + 4 / 11,559 + 3). W105 held for every relation kind tried (FK, O2O, M2M with automatic and explicit through, generic relation, multi-table link, an abstract base's field, swapped user model, ForeignObject, lazy string) and gave nothing for a library app. E102 held for contrib (auth.models vs auth.decorators/forms), a model's __module__ outside models, and an app whose label differs from its name. Nothing crashed.
A package in no hosted app above a hosted app's package runs before any module of the app, so E103 judged it through each import of a module under it: missed when the app imported nothing there, repeated once per such import otherwise. It is now judged once per app, at the app's __init__.py, line 1, and an import starts chains only at the modules Python runs for it that are not above the importer. E105 read definitions from the module body only and matched models by bare class name at any depth. A model's line now comes from inspect, as for W105, and an implementation's class or def is found in every block at module scope. Tests pin the chain and hit named among equals, an import back into the contract package, E104's label for a module public through two apps, and E105's plural forms.
|
Review 6067743820, fixed at 4de9464:
|
davinerdo
left a comment
There was a problem hiding this comment.
Verdict: Approve. Since 7af0bf2, all three points from the last review are fixed and tested, and the description matches 4de9464.
- tests/test_check_chains.py (non-blocking): in the new "Python runs X before it" message, no test fixes which link of the chain is named. Every package-above fixture has a one-step chain (
proj), so the mutantthrough[0]→through[-1]inbefore()survives. With a two-step chain (top/__init__.pyimportshelpers2.mid, which importsbilling.services), that mutant would print "Python runs helpers2.mid before it". If you touch the fixture again, aproj.quiet-style app under a package that reaches the target through a helper would pin it.
Checked: E103 for packages above an app with a two-level no-app package (a.b.side, where only a.b is bad), with nested hosted apps under one (top.outer, top.outer.inner), and with a chain through a helper. Each gives exactly one finding per app at its __init__.py, naming the right package. I ran 11 mutants of the new code (_defined's handler, match-case and def-skip branches, first, the per-import starts, before's path, the hit filter, E105's min and model lines). All are killed except the one above and one equivalent mutant. CI at 4de9464: five jobs green, and the 11,572 + 4 / 11,573 + 3 counts match the job logs.
Summary
neolith._check_imports.findings(source)reportsneolith.E101toE105andW105over the hosted apps' source (_check_source.Source, one parse per file) and their contract packages (_check_package.packages), as one function over a host. Task 7 wires it into the command;_check.pyis untouched.<name>.models, a module under it, or a module a model of that app names as its__module__; E103: when first-party code in no hosted app that Python runs reaches, through a chain of such code, what E101 or E102 reports, one shortest chain is named; never into a hosted app's module, and an unreadable module ends the chain. A package in no app above a hosted app's package is judged once per app, at the app's__init__.py, line 1, since Python runs it before any module of the app; an import gives E103 only through the modules Python runs for it that are not above the importer (Decision 2).<c>that imports (or runs a package above) a module of<c>outside every contract package, or runs code in no app from which a chain reaches one, so importing a contract package never loads its app's internals (Decision 2). E105: each module_check_packageleaves out, at the first line of its model (whereinspectfinds the class, as for W105) or of its implementation'sclassordefin any block at module scope (if,try,with,for,while,match); per the reading posted on Roadmap step 7: boundary check #14, a package module above that defines nothing is named with the contract module below it. W105: a first-party undeclared app's import of a hosted app's internals, or a relation of its model (foreign key, one-to-one, many-to-many and its explicit through model, generic relation, multi-table link, a child's inherited generic relation once) to a hosted app's model, reported against that app.Files owned
src/neolith/_check_imports.py,tests/test_check_imports.py,tests/test_check_chains.py(new). Specification and decision records touched: none; tests cite APP-66.How it was verified
_check_imports.pywere run against both test files; the seven behaviour-changing ones an adversarial pass found surviving (models-package depth, relative-import package, a package above inside a chain, nested undeclared app,mti_inherited, E104's own-app filter) now each fail a test, and so do those a later review found (the hit and chain named among equals,own()'s public filter, E104's label, E105's line and plural forms).importlib/_bootstrap.py_find_and_load_unlocked1304-1310 / 1263-1277 (each parent package runs first);inspect.findsource(by__qualname__on 3.12, by the class's__firstlineno__from 3.13,inspect.py1087 on 3.13.5). Django 5.2.19 / 6.1.3:apps/registry.pyget_containing_app_config251,get_models169,get_app_configs145;apps/config.pyMODELS_MODULE_NAME10,import_models262-268,get_models241;db/models/fields/related.pycreate_many_to_many_intermediary_model1308 / 1401, its__module__1346 / 1439;db/models/base.pymti_inherited350 / 371;contrib/contenttypes/fields.pyGenericRelation305 / 331.Changelog (Added):
neolith checkreports imports across the app boundary: another hosted app's modules outside its contract package (neolith.E101), an undeclared app's models (neolith.E102), chains through first-party code in no app (neolith.E103), a contract package that does not stand alone (neolith.E104) or holds a model or an implementation (neolith.E105), and an undeclared app's reach into a hosted app (neolith.W105).Part of #14.