-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Reachability analysis for isinstance(…) branches
#19503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
746382f
[ty] Type inference for isinstance(…) calls
sharkdp f3ce754
Add some more tests
sharkdp 315e442
Add comments explaining the type assertion failures
sharkdp c04a9fc
Handle a few more cases
sharkdp 5615b10
Handle intersections recursively
sharkdp 653ec2a
Add inference for isinstance(SomeClass, type)
sharkdp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
238 changes: 238 additions & 0 deletions
238
crates/ty_python_semantic/resources/mdtest/exhaustiveness_checking.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,238 @@ | ||
| # Exhaustiveness checking | ||
|
|
||
| ```toml | ||
| [environment] | ||
| python-version = "3.11" | ||
| ``` | ||
|
|
||
| ## Checks on literals | ||
|
|
||
| ```py | ||
| from typing import Literal, assert_never | ||
|
|
||
| def if_else_exhaustive(x: Literal[0, 1, "a"]): | ||
| if x == 0: | ||
| pass | ||
| elif x == 1: | ||
| pass | ||
| elif x == "a": | ||
| pass | ||
| else: | ||
| no_diagnostic_here | ||
|
|
||
| assert_never(x) | ||
|
|
||
| def if_else_exhaustive_no_assertion(x: Literal[0, 1, "a"]) -> int: | ||
| if x == 0: | ||
| return 0 | ||
| elif x == 1: | ||
| return 1 | ||
| elif x == "a": | ||
| return 2 | ||
|
|
||
| def if_else_non_exhaustive(x: Literal[0, 1, "a"]): | ||
| if x == 0: | ||
| pass | ||
| elif x == "a": | ||
| pass | ||
| else: | ||
| this_should_be_an_error # error: [unresolved-reference] | ||
|
|
||
| # this diagnostic is correct: the inferred type of `x` is `Literal[1]` | ||
| assert_never(x) # error: [type-assertion-failure] | ||
sharkdp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def match_exhaustive(x: Literal[0, 1, "a"]): | ||
| match x: | ||
| case 0: | ||
| pass | ||
| case 1: | ||
| pass | ||
| case "a": | ||
| pass | ||
| case _: | ||
| # TODO: this should not be an error | ||
| no_diagnostic_here # error: [unresolved-reference] | ||
|
|
||
| assert_never(x) | ||
|
|
||
| # TODO: there should be no error here | ||
| # error: [invalid-return-type] "Function can implicitly return `None`, which is not assignable to return type `int`" | ||
| def match_exhaustive_no_assertion(x: Literal[0, 1, "a"]) -> int: | ||
| match x: | ||
| case 0: | ||
| return 0 | ||
| case 1: | ||
| return 1 | ||
| case "a": | ||
| return 2 | ||
|
|
||
| def match_non_exhaustive(x: Literal[0, 1, "a"]): | ||
| match x: | ||
| case 0: | ||
| pass | ||
| case "a": | ||
| pass | ||
| case _: | ||
| this_should_be_an_error # error: [unresolved-reference] | ||
|
|
||
| # this diagnostic is correct: the inferred type of `x` is `Literal[1]` | ||
| assert_never(x) # error: [type-assertion-failure] | ||
| ``` | ||
|
|
||
| ## Checks on enum literals | ||
|
|
||
| ```py | ||
| from enum import Enum | ||
| from typing import assert_never | ||
|
|
||
| class Color(Enum): | ||
| RED = 1 | ||
| GREEN = 2 | ||
| BLUE = 3 | ||
|
|
||
| def if_else_exhaustive(x: Color): | ||
| if x == Color.RED: | ||
| pass | ||
| elif x == Color.GREEN: | ||
| pass | ||
| elif x == Color.BLUE: | ||
| pass | ||
| else: | ||
| no_diagnostic_here | ||
|
|
||
| assert_never(x) | ||
|
|
||
| def if_else_exhaustive_no_assertion(x: Color) -> int: | ||
| if x == Color.RED: | ||
| return 1 | ||
| elif x == Color.GREEN: | ||
| return 2 | ||
| elif x == Color.BLUE: | ||
| return 3 | ||
|
|
||
| def if_else_non_exhaustive(x: Color): | ||
| if x == Color.RED: | ||
| pass | ||
| elif x == Color.BLUE: | ||
| pass | ||
| else: | ||
| this_should_be_an_error # error: [unresolved-reference] | ||
|
|
||
| # this diagnostic is correct: inferred type of `x` is `Literal[Color.GREEN]` | ||
| assert_never(x) # error: [type-assertion-failure] | ||
|
|
||
| def match_exhaustive(x: Color): | ||
| match x: | ||
| case Color.RED: | ||
| pass | ||
| case Color.GREEN: | ||
| pass | ||
| case Color.BLUE: | ||
| pass | ||
| case _: | ||
| # TODO: this should not be an error | ||
| no_diagnostic_here # error: [unresolved-reference] | ||
|
|
||
| assert_never(x) | ||
|
|
||
| # TODO: there should be no error here | ||
| # error: [invalid-return-type] "Function can implicitly return `None`, which is not assignable to return type `int`" | ||
| def match_exhaustive_no_assertion(x: Color) -> int: | ||
| match x: | ||
| case Color.RED: | ||
| return 1 | ||
| case Color.GREEN: | ||
| return 2 | ||
| case Color.BLUE: | ||
| return 3 | ||
|
|
||
| def match_non_exhaustive(x: Color): | ||
| match x: | ||
| case Color.RED: | ||
| pass | ||
| case Color.BLUE: | ||
| pass | ||
| case _: | ||
| this_should_be_an_error # error: [unresolved-reference] | ||
|
|
||
| # this diagnostic is correct: inferred type of `x` is `Literal[Color.GREEN]` | ||
| assert_never(x) # error: [type-assertion-failure] | ||
sharkdp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| ## `isinstance` checks | ||
|
|
||
| ```py | ||
| from typing import assert_never | ||
|
|
||
| class A: ... | ||
| class B: ... | ||
| class C: ... | ||
|
|
||
| def if_else_exhaustive(x: A | B | C): | ||
| if isinstance(x, A): | ||
| pass | ||
| elif isinstance(x, B): | ||
| pass | ||
| elif isinstance(x, C): | ||
| pass | ||
| else: | ||
| no_diagnostic_here | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On |
||
|
|
||
| assert_never(x) | ||
|
|
||
| def if_else_exhaustive_no_assertion(x: A | B | C) -> int: | ||
| if isinstance(x, A): | ||
| return 0 | ||
| elif isinstance(x, B): | ||
| return 1 | ||
| elif isinstance(x, C): | ||
| return 2 | ||
|
|
||
| def if_else_non_exhaustive(x: A | B | C): | ||
| if isinstance(x, A): | ||
| pass | ||
| elif isinstance(x, C): | ||
| pass | ||
| else: | ||
| this_should_be_an_error # error: [unresolved-reference] | ||
|
|
||
| # this diagnostic is correct: the inferred type of `x` is `B & ~A & ~C` | ||
| assert_never(x) # error: [type-assertion-failure] | ||
sharkdp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def match_exhaustive(x: A | B | C): | ||
| match x: | ||
| case A(): | ||
| pass | ||
| case B(): | ||
| pass | ||
| case C(): | ||
| pass | ||
| case _: | ||
| # TODO: this should not be an error | ||
| no_diagnostic_here # error: [unresolved-reference] | ||
|
|
||
| assert_never(x) | ||
|
|
||
| # TODO: there should be no error here | ||
| # error: [invalid-return-type] "Function can implicitly return `None`, which is not assignable to return type `int`" | ||
| def match_exhaustive_no_assertion(x: A | B | C) -> int: | ||
| match x: | ||
| case A(): | ||
| return 0 | ||
| case B(): | ||
| return 1 | ||
| case C(): | ||
| return 2 | ||
|
|
||
| def match_non_exhaustive(x: A | B | C): | ||
| match x: | ||
| case A(): | ||
| pass | ||
| case C(): | ||
| pass | ||
| case _: | ||
| this_should_be_an_error # error: [unresolved-reference] | ||
|
|
||
| # this diagnostic is correct: the inferred type of `x` is `B & ~A & ~C` | ||
| assert_never(x) # error: [type-assertion-failure] | ||
sharkdp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added tests for
matchstatements as well, but they don't work yet (see astral-sh/ty#99 (comment))