-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Do not treat match value patterns as isinstance checks
#20146
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
Open
sterliakov
wants to merge
2
commits into
python:master
Choose a base branch
from
sterliakov:bugfix/match-value-pattern
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+91
−27
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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 |
|---|---|---|
|
|
@@ -30,9 +30,9 @@ m: Any | |
|
|
||
| match m: | ||
| case 1: | ||
| reveal_type(m) # N: Revealed type is "Literal[1]" | ||
| reveal_type(m) # N: Revealed type is "Any" | ||
| case 2: | ||
| reveal_type(m) # N: Revealed type is "Literal[2]" | ||
| reveal_type(m) # N: Revealed type is "Any" | ||
| case other: | ||
| reveal_type(other) # N: Revealed type is "Any" | ||
|
|
||
|
|
@@ -61,7 +61,7 @@ m: object | |
|
|
||
| match m: | ||
| case b.b: | ||
| reveal_type(m) # N: Revealed type is "builtins.int" | ||
| reveal_type(m) # N: Revealed type is "builtins.object" | ||
| [file b.py] | ||
| b: int | ||
|
|
||
|
|
@@ -83,7 +83,7 @@ m: A | |
|
|
||
| match m: | ||
| case b.b: | ||
| reveal_type(m) # N: Revealed type is "__main__.<subclass of "__main__.A" and "b.B">" | ||
| reveal_type(m) # N: Revealed type is "__main__.A" | ||
|
Collaborator
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. This was confusing for me until I realized that's just how equality works! |
||
| [file b.py] | ||
| class B: ... | ||
| b: B | ||
|
|
@@ -96,7 +96,7 @@ m: int | |
|
|
||
| match m: | ||
| case b.b: | ||
| reveal_type(m) | ||
| reveal_type(m) # N: Revealed type is "builtins.int" | ||
| [file b.py] | ||
| b: str | ||
| [builtins fixtures/primitives.pyi] | ||
|
|
@@ -1742,14 +1742,15 @@ from typing import NoReturn | |
| def assert_never(x: NoReturn) -> None: ... | ||
|
|
||
| class Medal(Enum): | ||
| gold = 1 | ||
| GOLD = 1 | ||
|
|
||
| def f(m: Medal) -> None: | ||
| always_assigned: int | None = None | ||
| match m: | ||
| case Medal.gold: | ||
| case Medal.GOLD: | ||
| always_assigned = 1 | ||
| reveal_type(m) # N: Revealed type is "Literal[__main__.Medal.gold]" | ||
| # This should narrow to literal, see TODO in checker::refine_identity_comparison_expression | ||
| reveal_type(m) # N: Revealed type is "__main__.Medal" | ||
| case _: | ||
| assert_never(m) | ||
|
|
||
|
|
@@ -1785,6 +1786,34 @@ def g(m: Medal) -> int: | |
| return 2 | ||
| [builtins fixtures/enum.pyi] | ||
|
|
||
| [case testMatchLiteralOrValuePattern] | ||
| # flags: --warn-unreachable | ||
| from typing import Literal | ||
|
|
||
| def test1(x: Literal[1,2,3]) -> None: | ||
| match x: | ||
| case 1: | ||
| reveal_type(x) # N: Revealed type is "Literal[1]" | ||
| case other: | ||
| reveal_type(x) # N: Revealed type is "Union[Literal[2], Literal[3]]" | ||
|
|
||
| def test2(x: Literal[1,2,3]) -> None: | ||
| match x: | ||
| case 1: | ||
| reveal_type(x) # N: Revealed type is "Literal[1]" | ||
| case 2: | ||
| reveal_type(x) # N: Revealed type is "Literal[2]" | ||
| case 3: | ||
| reveal_type(x) # N: Revealed type is "Literal[3]" | ||
| case other: | ||
| 1 # E: Statement is unreachable | ||
|
|
||
| def test3(x: Literal[1,2,3]) -> None: | ||
| match x: | ||
| case 1 | 3: | ||
| reveal_type(x) # N: Revealed type is "Union[Literal[1], Literal[3]]" | ||
| case other: | ||
| reveal_type(x) # N: Revealed type is "Literal[2]" | ||
|
|
||
| [case testMatchLiteralPatternEnumWithTypedAttribute] | ||
| from enum import Enum | ||
|
|
@@ -2813,7 +2842,7 @@ match A().foo: | |
| def int_literal() -> None: | ||
| match 12: | ||
| case 1 as s: | ||
| reveal_type(s) # N: Revealed type is "Literal[1]" | ||
| reveal_type(s) # E: Statement is unreachable | ||
| case int(i): | ||
| reveal_type(i) # N: Revealed type is "Literal[12]?" | ||
| case other: | ||
|
|
@@ -2822,7 +2851,7 @@ def int_literal() -> None: | |
| def str_literal() -> None: | ||
| match 'foo': | ||
| case 'a' as s: | ||
| reveal_type(s) # N: Revealed type is "Literal['a']" | ||
| reveal_type(s) # E: Statement is unreachable | ||
| case str(i): | ||
| reveal_type(i) # N: Revealed type is "Literal['foo']?" | ||
| case other: | ||
|
|
@@ -2909,9 +2938,9 @@ T_Choice = TypeVar("T_Choice", bound=b.One | b.Two) | |
| def switch(choice: type[T_Choice]) -> None: | ||
| match choice: | ||
| case b.One: | ||
| reveal_type(choice) # N: Revealed type is "def () -> b.One" | ||
| reveal_type(choice) # N: Revealed type is "type[T_Choice`-1]" | ||
| case b.Two: | ||
| reveal_type(choice) # N: Revealed type is "def () -> b.Two" | ||
| reveal_type(choice) # N: Revealed type is "type[T_Choice`-1]" | ||
| case _: | ||
| reveal_type(choice) # N: Revealed type is "type[T_Choice`-1]" | ||
|
|
||
|
|
||
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.
Is this
coerce_to_literalstill necessary now that we usenarrow_type_by_equalityrather thanconditional_types_with_intersection?