Skip to content
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

Fix single item enum match type exhaustion #16966

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/checkpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def visit_value_pattern(self, o: ValuePattern) -> PatternType:
typ = self.chk.expr_checker.accept(o.expr)
typ = coerce_to_literal(typ)
narrowed_type, rest_type = self.chk.conditional_types_with_intersection(
current_type, [get_type_range(typ)], o, default=current_type
current_type, [get_type_range(typ)], o, default=get_proper_type(typ)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue was that on mypy/checker.py on conditional_types function, which is called by this self.chk.conditional_types_with_intersection , the conditional_types returns from here the default value:

return default, UninhabitedType()

When on regular enum that has multiple choices the conditional_types returns here instead:

return proposed_type, remaining_type

But the first one returns wrong default value, so I am changing the default value here to be what the get_proper_type returns instead.

)
if not isinstance(get_proper_type(narrowed_type), (LiteralType, UninhabitedType)):
return PatternType(narrowed_type, UnionType.make_union([narrowed_type, rest_type]), {})
Expand Down
24 changes: 24 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,27 @@ match m3:
reveal_type(m3) # N: Revealed type is "Tuple[Union[builtins.int, builtins.str]]"
[builtins fixtures/tuple.pyi]

[case testMatchEnumSingleChoice]
from enum import Enum
from typing import NoReturn

def assert_never(x: NoReturn) -> None: ...

class Medal(Enum):
gold = 1

def f(m: Medal) -> None:
always_assigned: int | None = None
match m:
case Medal.gold:
always_assigned = 1
reveal_type(m) # N: Revealed type is "Literal[__main__.Medal.gold]"
case _:
assert_never(m)

reveal_type(always_assigned) # N: Revealed type is "builtins.int"
[builtins fixtures/bool.pyi]

[case testMatchLiteralPatternEnumNegativeNarrowing]
from enum import Enum
class Medal(Enum):
Expand All @@ -1388,10 +1409,13 @@ def f(m: Medal) -> int:
def g(m: Medal) -> int:
match m:
case Medal.gold:
reveal_type(m) # N: Revealed type is "Literal[__main__.Medal.gold]"
return 0
case Medal.silver:
reveal_type(m) # N: Revealed type is "Literal[__main__.Medal.silver]"
return 1
case Medal.bronze:
reveal_type(m) # N: Revealed type is "Literal[__main__.Medal.bronze]"
return 2

[case testMatchLiteralPatternEnumCustomEquals-skip]
Expand Down
Loading