Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Changes from all commits
67ded4aa028936f4a478b443a359File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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" | ||
sterliakov marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| [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]" | ||
Member 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. Does this narrow the type variable upper bound? (You can check e.g. by adding an attribute to only one class and accessing it here). If not, I would say this is a regression.
| ||
| 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]" | ||
Uh oh!
There was an error while loading. Please reload this page.