Resolve field nullability at the call site with `__init__` overloads #3763
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #3763"
general
weight: 0.35
UnknownPlatypus
I have made things!
Another attempt at this (hopefully the last one!). Each concrete field now declares two
__init__overloads:null: Literal[True]one annotatingselfwith the nullable specialisation (typing spec, Constructors ›__init__)The
_pyi_private_set/get_typehints are removed from the stubs. Note that some external tools used them:django-mysqlthat will have to migrate too. I'll open mr to help them migrate laterA few notable side effects of this change that probably need a release note mention:
x: models.IntegerField = models.IntegerField(null=True)is now rejected: a bare annotation means the non-null defaults. _These annotation should not be needed so the fix is to drop them (or add explicit type vars)._call-overloadinstead ofarg-type, so existing# type: ignore[arg-type]on such calls become unused _and need to be migrated to# type: ignore[call-overload]_<details>
<summary>Smoke test all checker on core features</summary>
pyrefly 1.4.0, pyright 1.1.414, ty 0.0.90, mypy 2.4.0, zuban 0.10.1 in mypy mode with its Django emulation active.
| | requirement | pyrefly | pyright | ty | mypy | mypy + plugin | zuban |
|---|---|:-:|:-:|:-:|:-:|:-:|:-:|
| R01 |
IntegerField()reads asint| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ || R02 |
IntegerField()rejects writingNone| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ || R03 |
IntegerField(null=True)reads asint \| None| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ || R04 |
IntegerField(null=True)accepts writingNone| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ || R05 |
IntegerField()accepts writing"12"(Django coerces) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ || R06 |
IntegerField[bool, bool]()reads asbool| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ || R17 |
IntegerField(nul=True)is rejected | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ || R07 |
HTMLField()reads asstr| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ || R08 |
HTMLField(null=True)is not an error | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ || R09 |
HTMLField(null=True)reads asstr \| None| ❌ | ❌ | ❌ | ❌ | ✅ | ❌ || R18 |
x: HTMLField = HTMLField(null=True)accepted | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ || R10 |
GenericInt()reads asint| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ || R11 |
GenericInt(null=True)reads asint \| None| ✅ | ❌¹ | ✅ | ❌¹ | ✅ | ✅ || R12 |
RecipeInt()reads asint| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ || R13 |
RecipeInt(null=True)reads asint \| None| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ || R14 |
f: Field[X \| None, Y \| None] = IntegerField(null=True)accepted | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ || R15 |
f: Field[X, Y] = IntegerField(null=True)rejected | ❌² | ✅ | ❌³ | ✅ | ✅ | ❌² || R16 |
f: Field = IntegerField()reads asAny| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |R09 is the one design limit: a bare non-generic subclass is pinned to
TextField[str, str], so stubs alone cannot make it nullable; only the plugin can. The other red cells are checker gaps:1. pyright and mypy apply the PEP 696 defaults before matching the inherited self-annotated overload, so
GenericInt(null=True)keeps the non-null defaults. The spec says defaults apply only to type variables left unsolved. The documented recipe (R13) passes everywhere.2. pyrefly and zuban do not report a nullable field assigned to a non-null
Field[X, Y]annotation, although_GTis covariant.3. ty accepts it by design since 0.0.99 (the rest of the table was run on 0.0.90): it specialises the call from the declared type, so the field keeps the non-null
Field[X, Y]and readsX. See astral-sh/ty#4977, closed as working as intended.</details>
Related issues
Fixes #766
Fixes #1264
Fixes #2724
Addresses #2043 through the README recipe
Improves #579
Supersedes #3317 and #1900
AI Policy
Followups
FilePathFieldwith concretestrdefaults and the same overloads, together with the other fields still reading asAny(RasterField,HStoreField,GeometryField), see https://github.com/typeddjango/django-stubs/pull/3763#discussion_r4444671814. They areAnyon master too, so this PR keeps parity.pyrefly/lib/alt/class/django.rs) works from the stubs' type parameters:- [x] Read a field's set/get types from its type parameter defaults instead of
_pyi_private_set_type/_pyi_private_get_type. The implicitidonly goes through these hints (it readsUnknownwithout them), soAutoFieldkeeps them until then.- [x] Match the set/get type parameters by position, or by mapping them to
Field's own, instead of the literal names_ST/_GT(_To/_Throughfor M2M). The_contra/_corename in #3784 already breaks explicit arguments likeIntegerField[bool, bool]on master, and the per-field names here (_ST_Int_contra, ...) would too.- [x] Keep folding
Nonein onnull=Truewhen the types come from the type parameters: bare subclasses (class HTMLField(TextField)), generic subclasses whose type vars have no defaults andJSONFieldlose it without the hints.- [x] Report a nullable field assigned to a non-null
Field[X, Y]annotation, or overriding one (is_active = BooleanField(null=True)on anAbstractUsersubclass)._pyi_private_-based Django supportdjango-mysqland the other packages relying on_pyi_private_