[ty] Right-hand side narrowing for if Foo is type(x) expressions#22608
Merged
AlexWaygood merged 2 commits intomainfrom Jan 17, 2026
Merged
[ty] Right-hand side narrowing for if Foo is type(x) expressions#22608AlexWaygood merged 2 commits intomainfrom
if Foo is type(x) expressions#22608AlexWaygood merged 2 commits intomainfrom
Conversation
Typing conformance resultsNo changes detected ✅ |
|
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-return-type |
3 | 1 | 4 |
no-matching-overload |
0 | 7 | 0 |
unresolved-attribute |
0 | 3 | 0 |
possibly-missing-attribute |
0 | 1 | 0 |
| Total | 3 | 12 | 4 |
1ff64d0 to
0ec7b35
Compare
0ec7b35 to
212672c
Compare
charliermarsh
approved these changes
Jan 17, 2026
Member
charliermarsh
left a comment
There was a problem hiding this comment.
Very nice (and great summary).
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
This PR reworks the big
matchstatement here into twoletchains that are independently evaluated. This allows us to apply type narrowing on many expressions where we weren't able to before, including:if Y is type(x)if Y is not type(x)<CALL EXPRESSION> is xwhere<CALL_EXPRESSION>is not a call tobuiltins.type<CALL EXPRESSION> is not xwhere<CALL_EXPRESSION>is not a call tobuiltins.typeThe last two in particular are quite interesting: the AST for
if type(x) is Yis very similar syntactically toif foo() is z, but we want to apply different kinds of narrowing for the two constructs:x, intersectingx's existing type with the top materialization of an instance ofYz, intersectingz's type with the annotated return type offoo.This is the key reason why a
matchno longer serves us well here:matcharms are mutually exclusive! But we don't necessarily know which kind of narrowing we need to apply here until we've examined some of the types, so mutually exclusive branching is not what we want. We only want tocontinueif we succeeded in applying a narrowing constraint; otherwise, we want to fall through and check whether the other kind of narrowing would apply.This PR is inspired by #22511, and seems to result in a surprising number of false positives going away in the ecosystem.
Test Plan
mdtests