Skip to content

feat: report the project's views in no app for the boundary check - #194

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

vignzpie merged 8 commits into
release/0.1.0a4from
feat/boundary-views

Conversation

@vignzpie

@vignzpie vignzpie commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Adds neolith.W102 as a function over a host (neolith._check_views.findings), task 6 of Roadmap step 7: boundary check #14: every URL pattern reachable from ROOT_URLCONF whose view serves as no app by label_of, and whose request runs the project's own code (a module with a file in the first-party map that no installed app contains), is reported once per pattern, at the innermost such code: a function's first line, or the one definition of a class in its module's file that agrees with it (by __firstlineno__ and its methods' lines), else line 1. Each file is parsed at most once a run.
  • That code is read along the links label_of follows and only those, so a project view is reported under any wrapper label_of follows (a bound method, an exact partial, a function whose __wrapped__ leads to it), and so is a project wrapper around a Django, library or contrib view; those views alone are not (APP-69).
  • Blind spots, not reported: a project view behind a wrapper label_of does not follow (a function that sets no __wrapped__, or a callable object of a library's type, such as sync_to_async(view), functools.lru_cache(view) or a library's partial subclass); an admin view W001 reports; a walk deeper than label_of follows; project code in a module with no file (a namespace package); a URLconf entry that is not Django's or that Django cannot read; a URLconf included inside itself (read once); anything past 64 includes deep or 100,000 entries of one walk or one URLconf. Nothing is reported when ROOT_URLCONF is unset or does not import (phase 4 reports that).
  • Task 7 wires it into phase 5; src/neolith/_check.py is unchanged.

Spec trace

APP-69 (d) is cited by every test in tests/test_check_views.py. No ID added or changed.

How it was verified

  • Matrix in one child process: owner (hosted, undeclared, project, library, Django, static.serve, contrib) × form (function, as_view(), decorated dispatch, bound method, instance, class) × every sequence of up to two of eight wrappers, each compared with an answer worked out in the test; plus a fixture per guard (walk bounds at exactly 64 includes and 7 reads, cycle set; each class-placement step, a one-line class with a lambda body among them; class kinds and admin names, an admin of an uninstalled app's model among them; unreadable files) and per blind spot. Mutation pass over _check_views.py on 3.13 / Django 6.1: 60 of 64 killed (each new fixture checked against its own mutant); the 4 equivalent are a non-text ROOT_URLCONF (fails import_module), an empty walk, the installed-app filter (label_of already serves such code as its app) and if admins: (an empty set matches nothing).
  • Source read: Django 6.1.3 urls/resolvers.py 108, 720, 727, 739; views/generic/base.py 82, 107, 118; contrib/admin/options.py 778; contrib/admin/sites.py 211; CPython 3.13 inspect.findsource 1085 and ast.parse (a NUL byte is a SyntaxError from 3.12).
  • CI at c2662f0: lint, types and all four test jobs pass (11,693 passed, 4 skipped on 3.12 / Django 5.2; 11,694 passed, 3 skipped on 3.15 / Django 6.1); spec_trace reports APP-69 cited by 21 tests.

Changelog: none (no public surface; task 7 wires it).

Part of #14.

Adds neolith.W102 as a function over a host (ADR 0030, spec 05 APP-69):
every URL pattern reachable from ROOT_URLCONF whose view serves as no app
by label_of and runs first-party code in no installed app. Task 7 wires
it into the command.
@vignzpie vignzpie added this to the 0.1.0a4 milestone Nov 26, 2026
Read each URLconf entry that Django cannot read as a blind spot, bound the
walk by include depth and entries read, leave a method or function of an
admin class that neolith.W001 names to W001, and place a class at the one
definition in its module's file that agrees with it, or at line 1.
A class's own entry may be of any type, whose metaclass __eq__ a tuple
membership test would run.
@vignzpie
vignzpie marked this pull request as ready for review November 26, 2026 02:46
@vignzpie
vignzpie requested a review from davinerdo November 26, 2026 02:46

@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. The rule agrees with label_of everywhere I probed, but the description overstates what it reports, and many of the walk's and the placement's guards have no test that fails when they are removed.

  1. PR body, second bullet, and src/neolith/_check_views.py:17: "a project view under any wrapper … [is reported]" is not what the code does. codes() follows only the links label_of follows, so these project views are not reported, even though their requests run project code in no app:

    • sync_to_async(view) (an asgiref.sync.SyncToAsync instance);
    • functools.lru_cache(view) (a functools._lru_cache_wrapper instance);
    • a library's partial subclass around a view;
    • a library decorator that sets no __wrapped__.

    The first two carry __wrapped__ to the view, but the walk stops at the object's type, as label_of does. The body becomes the squash commit message, so this sentence would land on the default branch. Fix: say "under any wrapper label_of follows (a bound method, an exact partial, a function whose __wrapped__ leads to it)" in both places. Add a blind-spot bullet to the body and the docstring: "a project view behind a wrapper label_of does not follow: a function that sets no __wrapped__, or a callable object of a library's type, such as sync_to_async(view), functools.lru_cache(view) or a library's partial subclass."

  2. _check_views.py:162-182: the walk's guards are untested. These mutants each leave all 12 tests passing (I re-ran them myself):

    • Without islice in _entries. A URLconf whose own urlpatterns never ends (itertools.repeat(path(...))) runs out of memory, where the original stops at _ENTRIES. Endless is endless in depth only, and the _ENTRIES = 7 test caps the outer loop, not one resolver's read.
    • Reading lazily (iter(resolver.url_patterns) without list). A root urlpatterns generator that raises partway lets RuntimeError escape findings().
    • within reduced to the parent's id. Two URLconfs that include each other (a → b → a) give 32 findings instead of one. _LOOP covers only a self-include.
    • isinstance(entry, URLResolver) → else. An object with pattern, urlconf_module and url_patterns that is not a URLResolver gets followed, so that documented blind spot isn't pinned.

    Fix: add a fixture for each: an endless flat urlpatterns, a generator that raises, a mutual include, and a resolver look-alike.

  3. _check_views.py:282-337: class placement is mostly unpinned. Each of these mutants survives, with an input that tells it apart:

    • Unwrapping staticmethod only: a @classmethod in a class defined twice → line 1.
    • Dropping ast.AsyncFunctionDef at :335: a class defined inside an async def → line 1.
    • inner.endswith(qualname) in place of ==: Twin beside a nested Nest.Twin → line 1.
    • Not resetting inner for other nodes: a class inside an if inside a class → line 1.
    • line < end: a class whose last body line is a one-line def → line 1.
    • start >= first: a class with an earlier definition kept under another name → line 1.
    • samefile → ==: a co_filename of dir/./views.py → line 1.
    • The OSError/SyntaxError arms at :305: a file deleted or rewritten after the map is built crashes the check.

    Fix: add a placement fixture for each, with the expected line.

  4. _check_views.py:209-234: the admin and kind guards are untested.

    • startswith(f"{admin}.") without the dot hides a module-level ThingAdminHelp view in the admin's module.
    • Reading function.__qualname__ in place of co_qualname hides a project view whose __qualname__ was set to ThingAdmin.x. The docstring of _named exists to rule that out.
    • Dropping _sets(...) at :230 hides any project function that carries a model_admin attribute.
    • issubclass(kind, type) → kind is type misses a class view with a metaclass (class V(abc.ABC)).
    • link is None → not link misses a falsy __wrapped__.

    The matrix's "class" form only uses plain type, and its admin fixtures only use the admin's own class and methods. Fix: add one fixture for each guard.

  5. _check_views.py:305 (non-blocking): the view's file is re-read and re-parsed for every class finding. ast.parse of a 2,934-line module takes about 39 ms on an arm64 board. So a project with 100 class-based routes into one large views.py adds about 4 s to every check run, and K10's measurement (one small fixture file) doesn't show it. Source already reads each file once per run. Fix: cache the tree (or the _definitions result) per path for one findings() call.

Direction: The tests come from a matrix of how views are composed. That's the right model for agreement with label_of, but it doesn't reach the code's own guards: the bounds, the cycle set, the except arms, the class-placement steps and the admin name match. This is the same class as the test-strength rounds on the earlier boundary-check tasks. Before the next push, take each if, except, bound and comparison in _check_views.py, plus each docstring sentence that says the rule does or skips something. Name the fixture that fails when it is deleted or loosened, and run a mutation pass over the file. In the body, state the mutant count as "N of M, with the equivalent ones named", not "each caught".

Checked: Django 6.1.3 urls/resolvers.py 108/720/727/739, views/generic/base.py 82/107/118, contrib/admin/options.py 778, contrib/admin/sites.py 211 and CPython 3.13 inspect.py 1085 all hold. About 45 views were compared for label_of/codes() agreement, and routes, placement and hostile objects were probed with nothing escaping findings(). A 138-mutant pass on 3.13 / Django 6.1.3 left the survivors above (the rest were killed or equivalent). CI at f3452a3 is green.

Add a fixture for every bound, cycle check, read, placement step, kind
check and admin name match of neolith.W102, each failing when its guard
is deleted or loosened, and for the blind spots the module names. Read
each file's class statements once per run instead of once per finding.
State in the docstring which wrappers a project view is followed through.
@vignzpie

Copy link
Copy Markdown
Collaborator Author

Addressed at 76d1f9c (merged with release/0.1.0a4 first).

  1. Docstring and body now say "under any wrapper label_of follows (a bound method, an exact partial, a function whose __wrapped__ leads to it)", with your blind-spot bullet; blind/ pins lru_cache, sync_to_async, a library partial subclass and a library wrapper without __wrapped__ as not reported.
  2. Fixtures for each: flat (endless flat urlpatterns, read to _ENTRIES), raising (a generator that raises, as root and as include), mutual/ (a → b → a) and a resolver look-alike.
  3. placed/ adds one class per step (classmethod, async def, Twin beside Nest.Twin, class in an if in a class, one-line def last, earlier definition kept under another name, a function borrowed into a class body, /./ filename); missing, bad-syntax and NUL files place at line 1 without raising.
  4. kinds/ adds ThingAdminHelp, a __qualname__ claiming ThingAdmin, a model_admin carrier, an admin wrapper with model_admin deleted, an abc.ABC class, an as_view of a class with a metaclass, a falsy __wrapped__, a hosted-app admin over a project mixin, line 0, an unlookupable filename and a non-text module name. The mutation pass over the file then gave 53 of 58 killed, with the 5 equivalent named in the body.
  5. Class statements are now read once per file per run (_read, kept in a dict for one findings() call); a test counts ast.parse calls against the files reported.

@vignzpie
vignzpie requested a review from davinerdo November 26, 2026 04:09

@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. Every gap from my last review is now pinned, but the description names one mutant as equivalent when a valid class tells it apart, and that sentence would land as the squash commit message.

Since f3452a3: I re-ran my mutation pass, adapted to the new _read cache, on 3.13 / Django 6.1.3. All 18 mutants I named last round are now killed, and so are the new cache mutants (no cache, a cache shared across runs, a duplicate definition overwritten or kept first). 120 of 142 are killed.

  1. PR body, "How it was verified": start < line is not equivalent. "A method starts after its class's first line" fails for a one-line class whose body is a lambda: class Page(View): get = lambda self, request: ... on line 3. The lambda's co_firstlineno equals the class's start, so the original places the finding at line 3 and the mutant at line 1 (I ran both). Fix: add that class to placed/ with expected line 3. Then make the line "54 of 58 killed; the 4 equivalent are …" and drop start < line from the list.

  2. _check_views.py (non-blocking): five more guards survive a mutant that a real input tells apart. Fold them into the same push if they're cheap:

    • if label is None and code is not None → if label is None in _reported_admins: admin.site.register(Ghost) where Ghost has Meta.app_label = "ghost" and no installed app. admin_code returns (None, None), so the mutant raises AttributeError out of findings() (I ran it).
    • _DEPTH set to 63 or 65: both pass. The body says "past 64 includes deep", so add a chain of exactly 64 includes (read) and 65 (not read).
    • islice(…, _ENTRIES + 1) in _entries: the docstring says "never more than the walk reads in all". A generator that counts how often it is read would pin it.
    • codes() returning walked instead of None when the bound runs out and only one function was walked: a project function whose __wrapped__ is a chain of more than _LINKS exact partials. That's the "deeper than label_of follows" blind spot, and it's pinned today only when the walk is empty or longer.
    • startswith(f"{qualname}.") without the dot in _class_line: a function from PageMixin assigned into Page's body places Page at line 1.

Checked: CI at 76d1f9c is green on all five jobs. The merge from release/0.1.0a4 touches neither file. The diff since f3452a3 only changes the cache and the wording. Dropping ValueError from _read is safe on 3.12 and later: a NUL byte or a bad encoding is a SyntaxError. compile and ast.parse fail at the same nesting depth, so any file that imports also parses. Docstring and body match the code apart from point 1.

Add fixtures that tell apart five more guards: a one-line class whose body
is a lambda, a class holding a function of a class whose name starts with
its own, an admin of a model whose app is not installed, a chain of
exactly 64 and 65 includes, a count of the entries one URLconf is read,
and a project function whose links run past the bound.
@vignzpie

Copy link
Copy Markdown
Collaborator Author

Addressed at 00bc73a.

  1. placed/oneline/ adds class OneLine: get = lambda self, request: None, placed at its own line; the body now says 59 of 63 killed, with the 4 equivalent named and start < line dropped.
  2. Each of the five now has a fixture that kills it:
    • admin.site.register(Ghost), a model with app_label = "ghost" and no installed app;
    • lazy/ asserted at exactly 64 includes, not 65 (no longer read from _DEPTH);
    • flat counts its reads: 7, not 8;
    • deep/beyond/partials/: a project function whose __wrapped__ is _LINKS library wrappers deep. Exact partials can't build that chain, because a partial of a partial is flattened into one;
    • placed/lent/: Lent.handle = LentMixin.handle.

@vignzpie
vignzpie requested a review from davinerdo November 26, 2026 04:51

@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. Five of the six gaps are closed, but deep/beyond/partials/ doesn't kill the mutant it was added for, and the test's comment and the body's "59 of 63 killed" both say it does.

Since 76d1f9c: only tests/test_check_views.py changed. I re-ran my mutation pass on the new tests (3.13 / Django 6.1.3). The one-line lambda class, _DEPTH 63 and 65, the islice bound, the (None, None) admin guard and the startswith dot are now killed. That makes 126 of 142, and no mutant that was killed before survives now.

  1. tests/test_check_views.py:695: parted.__wrapped__ = layered(_LINKS) doesn't create the case it is meant to pin. vendorlib.wrap.wrapped returns plain functions, and codes() appends every function it walks. So when the bound runs out, walked holds 65 entries: parted plus the 64 library wrappers. It doesn't hold "that one function of the project's", as the comment at line 1333 says. The mutant return None if len(walked) > 1 else walked at the end of codes() still passes the whole file. The reply says partial-of-partial flattening rules out a chain with no functions in it. Bound methods and partials can alternate, though, and a partial of a bound method is not flattened. Fix: build the chain as

    chain = site_views.page
    for i in range(_LINKS):
        chain = functools.partial(chain) if i % 2 else types.MethodType(chain, object())
    parted.__wrapped__ = chain

    label_of(parted) is None for it, and codes() returns None on this head. The mutant returns [parted] and reports it. I swapped this chain into the fixture, and the mutant then fails test_every_other_pattern_is_reported_at_its_innermost_project_code, while the unmutated code still passes. Then correct the comment at line 1333.

  2. PR body, "How it was verified": "59 of 63 killed" went up by exactly the five mutants from my last review, so it counts this one as killed. Today it is 58 of 63. Since the body becomes the squash commit message, either land the fix in point 1, or make the line 58 of 63 and name the survivor.

Direction: In each of the last two rounds, a claim about the tests ("equivalent" last time, "kills it" this time) was written without running the mutant it names. After you add a guard fixture, apply that one mutant and watch the test fail before you claim the fixture kills it. If the test passes under the mutant, the fixture is going down a different path from the one it is named for.

Checked: CI at 00bc73a is green on all five jobs; src/ is unchanged since 76d1f9c. The four mutants the body calls equivalent still survive in my pass, and nothing else in the body or docstrings has gone stale.

Build the chain past the bound from bound methods and exact partials in
turn, so the walk holds one function when the bound runs out, and a
codes() that returns that walk is caught.
@vignzpie

Copy link
Copy Markdown
Collaborator Author

Addressed at c2662f0.

  1. deep/beyond/partials/ now uses your chain (bound methods and exact partials in turn), and the comment is fixed. return None if len(walked) > 1 else walked survives the tests at 00bc73a and fails two tests at c2662f0.
  2. The body now says 60 of 64 killed, with this mutant counted only after I ran it. I applied each new fixture's own mutant before writing that line.

@vignzpie
vignzpie requested a review from davinerdo November 26, 2026 05:19

@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. deep/beyond/partials/ now kills the one-function bound mutant, and the body's count is accurate.

Since 00bc73a: only the fixture and its comment changed. return None if len(walked) > 1 else walked now fails the file, and the unmutated code passes. I re-ran my mutation pass on 3.13 / Django 6.1.3: 127 of 142 killed, one more than last round, and no mutant that was killed before survives now. "60 of 64 killed" matches the change. CI at c2662f0 passes on all five jobs, and src/ is the same as the code I passed last round.

@vignzpie
vignzpie merged commit 9a617d3 into release/0.1.0a4 Nov 26, 2026
5 checks passed
@vignzpie
vignzpie deleted the feat/boundary-views branch November 26, 2026 05:27
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