Skip to content

feat: report imports across the app boundary in the check - #193

Merged
vignzpie merged 4 commits into
release/0.1.0a4from
feat/boundary-imports
Nov 26, 2026
Merged

vignzpie merged 4 commits into
release/0.1.0a4from
feat/boundary-imports

Conversation

@vignzpie

@vignzpie vignzpie commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Task 4 of Roadmap step 7: boundary check #14: neolith._check_imports.findings(source) reports neolith.E101 to E105 and W105 over 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.py is untouched.
  • Admitted set: an import of a module that is part of any hosted app's contract package (a contract module or a package above one), of the importer's own app, of a library or of Django (a contrib app's modules other than its models modules) gives nothing. E101: another hosted app's module outside it; E102: an undeclared app's <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).
  • E104: a contract-package module lying in hosted app <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_package leaves out, at the first line of its model (where inspect finds the class, as for W105) or of its implementation's class or def in 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.
  • Fail closed: a name a rule cannot place is no finding.

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

  • Each row's flagged and admitted examples, the importer × target matrix, and E103's shortest chain against a breadth-first search on generated graphs (Hypothesis). Single-rule mutants of _check_imports.py were 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).
  • Read, CPython 3.12.13 / 3.15.0b1: importlib/_bootstrap.py _find_and_load_unlocked 1304-1310 / 1263-1277 (each parent package runs first); inspect.findsource (by __qualname__ on 3.12, by the class's __firstlineno__ from 3.13, inspect.py 1087 on 3.13.5). Django 5.2.19 / 6.1.3: apps/registry.py get_containing_app_config 251, get_models 169, get_app_configs 145; apps/config.py MODELS_MODULE_NAME 10, import_models 262-268, get_models 241; db/models/fields/related.py create_many_to_many_intermediary_model 1308 / 1401, its __module__ 1346 / 1439; db/models/base.py mti_inherited 350 / 371; contrib/contenttypes/fields.py GenericRelation 305 / 331.
  • CI at 4de9464: all five jobs green; 11,572 passed and 4 skipped on 3.12 with Django 5.2, 11,573 passed and 3 skipped on 3.15 with Django 6.1, on x86-64 and arm64.

Changelog (Added): neolith check reports 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.

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.
@vignzpie vignzpie added this to the 0.1.0a4 milestone Nov 26, 2026
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.
@vignzpie
vignzpie marked this pull request as ready for review November 26, 2026 02:16
@vignzpie
vignzpie requested a review from davinerdo November 26, 2026 02:16

@davinerdo davinerdo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

  1. 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], so proj/__init__.py reaching another app's internals is only found through an import whose target lies under proj. In a probe with hosted billing and hosted proj.side, and proj/__init__.py doing import billing.services: if proj/side/views.py only does import json, nothing is reported. If its modules do from . import helpers, from .helpers import x, import proj.side.helpers, and forms.py does from .helpers import x, you get four identical E103s, each saying "imports proj.side.helpers; through … (proj)", though none of those imports runs proj. It already ran before the importing module. So the same fault in proj/__init__.py is 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 example app 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 only import 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."

  2. 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. _binds reads only tree.body, and models are matched by bare class name at any depth. Probes: an implementation class Service inside try: … except ImportError: gives contracts.py:1; one inside if …: next to a model gives the model's line (21) instead of 14; a nested class Holder: class Thing before the real model Thing, 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 (inspect finds the class by qualname). Find the implementation's class/def by name in every module-scope block (if, try/except/else/finally, with, for, while, match), never inside a def or class. 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 and def branches of _binds can never match an implementation: drop them rather than test them.

  3. 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() without name not in self.public: till/records.py does import helpers.back, and helpers/back.py does import 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], and min → max at the step in shortest all survive. Use a node that imports two internals (b.zeta on line 1, b.alpha on 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 → max survives); 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.
@vignzpie

Copy link
Copy Markdown
Collaborator Author

Review 6067743820, fixed at 4de9464:

  1. Packages in no app above a hosted app's package are now judged once per app, at its __init__.py, line 1 (app side: package proj.side: Python runs proj before it; …); an import starts chains only at runs(target, importer). Tests: an app importing only json and one with four relative/absolute imports of its own modules each give exactly one E103.
  2. E105 takes a model's line from _check_models._source and finds the implementation's class/def in every module-scope block (_defined), never inside a def or class; _binds is gone. Tests: one per block form, plus fixtures for if before a model, try, and a nested Holder.Thing before the real model.
  3. Pinned: the own() cycle back into till.contracts, the hit and chain named among equals (billing.alpha, helpers.fa), E104's label through two apps (outer.inner2), and E105's plurals; each listed mutant, and the old E103 starts, now fail a test.

@vignzpie
vignzpie requested a review from davinerdo November 26, 2026 02:53

@davinerdo davinerdo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: Approve. Since 7af0bf2, all three points from the last review are fixed and tested, and the description matches 4de9464.

  1. 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 mutant through[0] → through[-1] in before() survives. With a two-step chain (top/__init__.py imports helpers2.mid, which imports billing.services), that mutant would print "Python runs helpers2.mid before it". If you touch the fixture again, a proj.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.

@vignzpie
vignzpie merged commit 7133118 into release/0.1.0a4 Nov 26, 2026
5 checks passed
@vignzpie
vignzpie deleted the feat/boundary-imports branch November 26, 2026 03:00
This was referenced Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants