[ty] Reuse expression cache for TypedDict union inference#25643
Merged
charliermarsh merged 1 commit intoJun 4, 2026
Conversation
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 92.13%. The percentage of expected errors that received a diagnostic held steady at 87.18%. The number of fully passing files held steady at 92/134. |
Memory usage reportMemory usage unchanged ✅ |
|
carljm
approved these changes
Jun 4, 2026
Comment on lines
+5
to
+6
| Contextually checking nested collection literals against a recursive union of `TypedDict`s should | ||
| not repeat the same inference for every union arm at every nesting level. |
Contributor
There was a problem hiding this comment.
Does the behavior of this test differ on main vs in this PR? This comment (and the PR description) makes it seem like a pure optimization. An mdtest like this won't prevent perf regressions; a benchmark would.
e25216a to
bfe3737
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When contextually checking a dictionary literal against a union of TypedDicts, we speculatively infer the literal once for each candidate. Recursive fields often give the same nested expression the same type context for every candidate, causing us to repeat the entire nested inference at every level...
This reuses our existing shared expression cache across the candidate checks. We already use the same mechanism during generic-call multi-inference.
The linked issue drops from roughly 1.5s to 0.03s locally.
Closes astral-sh/ty#3663.