Repository navigation
feat: report the project's views in no app for the boundary check - #194
Conversation
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.
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.
davinerdo
left a comment
There was a problem hiding this comment.
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.
-
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 linkslabel_offollows, so these project views are not reported, even though their requests run project code in no app:sync_to_async(view)(anasgiref.sync.SyncToAsyncinstance);functools.lru_cache(view)(afunctools._lru_cache_wrapperinstance);- a library's
partialsubclass 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, aslabel_ofdoes. The body becomes the squash commit message, so this sentence would land on the default branch. Fix: say "under any wrapperlabel_offollows (a bound method, an exactpartial, 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 wrapperlabel_ofdoes not follow: a function that sets no__wrapped__, or a callable object of a library's type, such assync_to_async(view),functools.lru_cache(view)or a library'spartialsubclass." -
_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
islicein_entries. A URLconf whose ownurlpatternsnever ends (itertools.repeat(path(...))) runs out of memory, where the original stops at_ENTRIES.Endlessis endless in depth only, and the_ENTRIES = 7test caps the outer loop, not one resolver's read. - Reading lazily (
iter(resolver.url_patterns)withoutlist). A rooturlpatternsgenerator that raises partway letsRuntimeErrorescapefindings(). withinreduced to the parent's id. Two URLconfs that include each other (a → b → a) give 32 findings instead of one._LOOPcovers only a self-include.isinstance(entry, URLResolver)→else. An object withpattern,urlconf_moduleandurl_patternsthat is not aURLResolvergets 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. - Without
-
_check_views.py:282-337: class placement is mostly unpinned. Each of these mutants survives, with an input that tells it apart:- Unwrapping
staticmethodonly: a@classmethodin a class defined twice → line 1. - Dropping
ast.AsyncFunctionDefat :335: a class defined inside anasync def→ line 1. inner.endswith(qualname)in place of==:Twinbeside a nestedNest.Twin→ line 1.- Not resetting
innerfor other nodes: a class inside anifinside a class → line 1. line < end: a class whose last body line is a one-linedef→ line 1.start >= first: a class with an earlier definition kept under another name → line 1.samefile→==: aco_filenameofdir/./views.py→ line 1.- The
OSError/SyntaxErrorarms 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.
- Unwrapping
-
_check_views.py:209-234: the admin and kind guards are untested.startswith(f"{admin}.")without the dot hides a module-levelThingAdminHelpview in the admin's module.- Reading
function.__qualname__in place ofco_qualnamehides a project view whose__qualname__was set toThingAdmin.x. The docstring of_namedexists to rule that out. - Dropping
_sets(...)at :230 hides any project function that carries amodel_adminattribute. issubclass(kind, type)→kind is typemisses a class view with a metaclass (class V(abc.ABC)).link is None→not linkmisses 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. -
_check_views.py:305(non-blocking): the view's file is re-read and re-parsed for every class finding.ast.parseof a 2,934-line module takes about 39 ms on an arm64 board. So a project with 100 class-based routes into one largeviews.pyadds about 4 s to every check run, and K10's measurement (one small fixture file) doesn't show it.Sourcealready reads each file once per run. Fix: cache the tree (or the_definitionsresult) per path for onefindings()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.
|
Addressed at 76d1f9c (merged with
|
davinerdo
left a comment
There was a problem hiding this comment.
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.
-
PR body, "How it was verified":
start < lineis 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'sco_firstlinenoequals 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 toplaced/with expected line 3. Then make the line "54 of 58 killed; the 4 equivalent are …" and dropstart < linefrom the list. -
_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 Nonein_reported_admins:admin.site.register(Ghost)whereGhosthasMeta.app_label = "ghost"and no installed app.admin_codereturns(None, None), so the mutant raisesAttributeErrorout offindings()(I ran it)._DEPTHset 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()returningwalkedinstead ofNonewhen the bound runs out and only one function was walked: a project function whose__wrapped__is a chain of more than_LINKSexactpartials. That's the "deeper thanlabel_offollows" 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 fromPageMixinassigned intoPage's body placesPageat 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.
|
Addressed at 00bc73a.
|
davinerdo
left a comment
There was a problem hiding this comment.
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.
-
tests/test_check_views.py:695:
parted.__wrapped__ = layered(_LINKS)doesn't create the case it is meant to pin.vendorlib.wrap.wrappedreturns plain functions, andcodes()appends every function it walks. So when the bound runs out,walkedholds 65 entries:partedplus the 64 library wrappers. It doesn't hold "that one function of the project's", as the comment at line 1333 says. The mutantreturn None if len(walked) > 1 else walkedat the end ofcodes()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 aschain = 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)isNonefor it, andcodes()returnsNoneon this head. The mutant returns[parted]and reports it. I swapped this chain into the fixture, and the mutant then failstest_every_other_pattern_is_reported_at_its_innermost_project_code, while the unmutated code still passes. Then correct the comment at line 1333. -
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.
|
Addressed at c2662f0.
|
davinerdo
left a comment
There was a problem hiding this comment.
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.
Summary
neolith.W102as a function over a host (neolith._check_views.findings), task 6 of Roadmap step 7: boundary check #14: every URL pattern reachable fromROOT_URLCONFwhose view serves as no app bylabel_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.label_offollows and only those, so a project view is reported under any wrapperlabel_offollows (a bound method, an exactpartial, 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).label_ofdoes not follow (a function that sets no__wrapped__, or a callable object of a library's type, such assync_to_async(view),functools.lru_cache(view)or a library'spartialsubclass); an admin view W001 reports; a walk deeper thanlabel_offollows; 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 whenROOT_URLCONFis unset or does not import (phase 4 reports that).src/neolith/_check.pyis 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
static.serve, contrib) × form (function,as_view(), decorateddispatch, 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.pyon 3.13 / Django 6.1: 60 of 64 killed (each new fixture checked against its own mutant); the 4 equivalent are a non-textROOT_URLCONF(failsimport_module), an empty walk, the installed-app filter (label_ofalready serves such code as its app) andif admins:(an empty set matches nothing).urls/resolvers.py108, 720, 727, 739;views/generic/base.py82, 107, 118;contrib/admin/options.py778;contrib/admin/sites.py211; CPython 3.13inspect.findsource1085 andast.parse(a NUL byte is aSyntaxErrorfrom 3.12).Changelog: none (no public surface; task 7 wires it).
Part of #14.