Repository navigation
feat: report the boundary check's call rules, E108 to E111 and W103 - #192
Conversation
Generated modules for every way of binding each listed callee, judged against the interpreter's own binding of transaction's signatures, and one host in a child process per file.
neolith._check_calls.findings reports E108 (connect() outside requires), E109 (an alias of default or another hosted app, and the bare forms that mean default), E110 (a command scope for another app), E111 (another app's model or config named by a string) and W103 (testing.boot() outside test modules), over the source that neolith._check_source reads. The command wires it in later (task 7 of #14).
A frozen dataclass in neolith is built once with neolith._frozen.once and has a case in tests/test_frozen.py; App is internal data, like the check's Finding.
A finding is on the line of the value written, or of the name called or loaded; every function of transaction as a bare decorator and with a lambda in the place of using is judged against its signature; get_user_model() is judged by installed labels only; a transaction function the installed Django lacks is not judged; a sender= after another keyword; and a hosted app's module that does not compile.
- Each finding is on the line of the value written, or of the name the call calls or the expression loads, not where a chained call starts. - E109's set of transaction functions holds those the installed Django defines, so savepoint_create is not judged on 5.2. - A transaction function used as a bare decorator is judged as the call it makes: on_commit and the others that leave using out mean default; atomic and non_atomic_requests applied to a lambda mean default as well. - get_user_model() is E111 only for a label an installed app has, as get_model() and the other forms are.
davinerdo
left a comment
There was a problem hiding this comment.
Verdict: Changes needed. One spelling of a cross-app model lookup passes E111 while the same lookup through get_app_config fails it.
- src/neolith/_check_calls.py:437 (
_get_model) and tests/test_check_calls.py:185:apps.get_model("desk", name)gives no E111, only W101, which never fails the check.apps.get_app_config("desk").get_model(name)reaches the same models and is E111. ADR 0030 Decision 5 (line 189) says the check sees "any call of an attribute namedget_modelwith literal arguments that include a label". Its blind-spot example,get_model(label, model), has a computed label. Whatever model a computedmodel_namepicks, a literal label of another installed app already decides that it is that app's model. The test row("'auth', name", None)pins the lenient reading, so a migration that writesapps.get_model("desk", name)passes as an enforced app. Fix: when the label is a literal andmodel_nameis given but computed, return(label, None)and report E111. Only an empty literal name should stay unjudged. Then change the test row to expect E111. I tried this on a copy (name == ""as the only early return, plus a message for the computed case). Both spellings above then give E111, and 91 of 92 tests pass. The one failure is the expected message of the row you'd change. Description sentence for the second bullet: "get_modelwith a literal label of another installed app is E111 whatever its model name, asget_app_configof that label is."
Non-blocking:
- src/neolith/_check_calls.py:331:
transaction.atomic(views.checkout)andnon_atomic_requests(view)called on a named function aren't judged, and no W101 is raised either. Only thelambdaform is judged. Django'scallable(using)branch reads both calls asdefault, the same as the bare decorator, and wrapping a view inatomic(...)in an app'surls.pyis a common idiom. Decision 5's "a computed alias is not reported" covers it on the letter, but the docstring and the description present the bare andlambdaforms as the whole set. Either judge a positional argument that is a name the file binds only by a module-leveldeforclass, or say in the module docstring thatatomic(<name>)is read as a computed alias. - src/neolith/_check_calls.py:231: the E109 for
django.db.connectionat its import is placed on thefromline. Sofrom django.db import (\n connections,\n connection,\n)reports line 1, while the docstring places a form that writes no alias on "the name it ... loads".ast.aliashas carriedlinenosince 3.10, so the line of the matching alias would follow the rule. - src/neolith/_check_calls.py:189 (
_written): changingargument.linenotoargument.end_linenopasses all 92 tests. No test has a literal that spans lines, such asusing=("de"\n"fault"). One row with it would pin the placement that ef8ef3d set.
Checked: every Django citation in the description, and each using position and callable(using) branch, on 5.2.19 and 6.1.3. I ran about 300 hand-written forms through judged/findings and found no other gap. A 96-mutant pass on _check_calls.py killed 77; the survivors are equivalent apart from point 4 and untested defensive branches (non-dict DATABASES, non-str AUTH_USER_MODEL). All 455 tests in tests/test_check*.py pass on 5.2.19.
A literal label of another installed app beside a computed model name is now E111, as get_app_config of that label is. The E109 of an imported django.db.connection is on the line of the name imported, a literal spread over lines is pinned to the line it starts on, and a view passed by name to atomic or non_atomic_requests is documented as a computed alias.
|
Changes in f41bf6f, CI green on all five jobs:
|
davinerdo
left a comment
There was a problem hiding this comment.
Verdict: Approve. f41bf6f fixes all four points from my last review, and the description matches the head.
Checked: apps.get_model("desk", name) and its other computed forms (model_name=, *, **, keyword-only) are now E111. An empty, None or non-str literal name, a dotted label with a name, and the own label all stay unjudged. The E109 of an imported connection is on its own line, and so is its as form. Each of these mutants now fails a test: alias.lineno back to node.lineno, argument.lineno to end_lineno, the computed branch returning None, and the empty-name guard dropped. 270 tests in tests/test_check_calls.py, test_check_aliases.py and test_check_source.py pass on Django 5.2.19. One new reading comes from the change I asked for: AppConfig.get_model("<name>", <computed require_ready>) is now E111 when the model name equals another installed app's label. Decision 5's any-receiver rule covers that, and the spelling is rare, so it doesn't block.
Summary
neolith._check_calls.findings(source)reports the call rules of ADR 0030 over the source thatneolith._check_sourcereads:neolith.E108(connect("<id>")outsiderequires),E109(an alias ofdefaultor of another hosted app, as a literal orDEFAULT_DB_ALIAS, in the five places Decision 5 lists, and the forms that meandefaultby leaving the alias out),E110(command_scopefor another installed app),E111(get_model,get_app_config,sender=andget_user_model()naming another installed app;get_modelwith a literal label of another installed app is E111 whatever its model name, asget_app_configof that label is) andW103(testing.boot()in a first-party module other than a test module).bound); a name bound in any other way is a blind spot, never a finding.get_model,get_app_configandsender=are matched on any receiver, a migration's registry parameter included. A label no installed app has is not reported by any E110 or E111 form,get_user_model()included, since Django refuses it at run time.django.db.transactionis judged as the call it makes, also as a bare decorator:@on_commitand the others whoseusingis not first leave the alias out, andatomicandnon_atomic_requestsread the decorated function, bare or applied to alambda, asdefault. The set holds the functions the installed Django defines, sosavepoint_createis judged on 6.1 only. A name in the place ofusing, a view passed by name toatomic(...)included, is read as a computed alias (Decision 5) and not judged.What changed and why
Files owned (task 5 of #14): new
src/neolith/_check_calls.py, newtests/test_check_calls.py, newtests/test_check_aliases.py.A call spread over lines is placed by its name, not by what a traceback shows, because CPython 3.13 gives the statement's first line for
auth\n.get_user_model()(an imported receiver) and the attribute's line for a method call. A W103 in an undeclared app's module names no hosted app (Finding.appisNone); 0030's findings sentence does not list that case, which #195 takes to a record.Links
Part of #14.
Specification and decision records touched
None (implements ADR 0030's rows E108 to E111 and W103; adds no requirement ID).
Spec trace
tests/test_check_calls.py::test_connect_with_a_literal_outside_requires_is_e108,::test_e108_over_a_hosttests/test_check_aliases.py;tests/test_check_calls.py::test_command_scope_for_another_installed_app_is_e110,::test_e110_over_a_hosttests/test_check_calls.py::test_get_model_on_any_receiver_naming_another_app_is_e111,::test_get_app_config_on_any_receiver_naming_another_app_is_e111,::test_a_sender_named_by_a_string_of_another_app_is_e111,::test_get_user_model_is_e111_when_auth_user_model_is_another_app_s,::test_e111_over_a_host_a_migration_s_registry_includedHow it was verified
lambda; and as a bare decorator on a function, a coroutine and a class) is bound to the real signature, and must be flagged exactly whenusingbinds toNone,"default", another hosted app's alias, or a callable for a function with Django'scallable(using)branch. Single-condition mutants of each rule's guard, thesender=loop, the uncompilable-module guard and the decorator set each fail a test. CI at f41bf6f: all five jobs green; 11,630 passed and 4 skipped on 3.12 with Django 5.2, 11,631 passed and 3 skipped on 3.15 with Django 6.1, on x86-64 and arm64.test_check_aliases.py1.0 s,test_check_calls.py0.5 s, one child-process host each) and 2.0 s on 3.12 x86-64.db/transaction.pysignatures 18-129 / 20-140,atomic316 / 327 andnon_atomic_requests334 / 345 with theircallable(using)branch;db/__init__.py:38,43;db/utils.py:13;db/models/query.pyQuerySet.using(alias)1806 / 2019;db/models/manager.py:136db_manager(using=None, ...);apps/registry.py:188,206get_model,:150get_app_config;apps/config.py:224;db/models/signals.py:15-22;db/models/utils.py:5-16;contrib/auth/__init__.pyget_user_model278 / 261. neolith:_host.py:1051connect(identity, /),:1238command_scope(app, ...).Changelog: none (task 7 wires these rules into the command and writes its changelog line).
Checklist
Reviewers look in this order.
<type>/<short-name>(G1), one logical change (G3).