Repository navigation
Short-circuit failed TypedDict dictionary equivalence - #11771
Conversation
Return immediately when a field makes dictionary equivalence fail, avoiding unnecessary union construction and assignability checks for remaining fields. Credit @Wizzerinus for identifying this optimization in #11759. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
🔒 Automated review in progress — Bill Schnurr (@bschnurr) is auto-reviewing this PR. |
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
|
Result: Verification detailsVerification: Isolated verification observed failures that were not classified as caused by this PR: Dependency and test discovery, 500-field short-circuit CLI reproduction. The relevant tests could not be fully run in the isolated environment; this review is not fully verified. Summary: The 13 targeted TypedDict tests passed, covering closed, required, and read-only cases, and the TypeScript build passed. No tests were added by the PR. A generated 500-field performance reproduction could not be executed because my compiled CLI invocation was malformed, so the claimed short-circuit performance improvement remains unverified. Existing tests validate unchanged diagnostics but do not assert that processing stops after the first failed field. Test runs: 3 passed, 2 failed, 1 not run
❌
|
Bill Schnurr (bschnurr)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
Summary
Return immediately from
getTypedDictDictEquivalentwhen a field is read-only, required, or fails the existing invariant-assignability check. Previously, evaluation continued through every remaining field, repeatedly constructing unions even though the result was already known to beundefined.The equivalence rules and successful return types are unchanged. This implements the early-exit optimization discussed in #11759; the separate synthesized
update()tuple-union optimization is not included.Credit
Credit to Wizzerinus | Alex K. (@Wizzerinus), the author of #11759, for identifying the unnecessary work, proposing the early-exit solution, and providing the
hetero_500.pyreproduction.Performance
Using the unmodified 500-field attachment from #11759, with the same local build setup before and after this change, three separate CLI runs produced:
--statsCheck)This is approximately 7.9× faster, or an 87% reduction in checking time for this reproduction. Measurements exclude startup overhead and are local reproduction measurements, not a general performance guarantee. All runs reported zero errors and zero warnings.
Validation
TypedDictClosedandTypedDictReadOnlycases intypeEvaluator5.test.tspassed; existing expectations were unchanged.git diff --checkpassed.Related to #11759. This is separate from the overload-materialization regression in #11751 and its rollback in #11739.
Fixes: #11751