Skip to content

Commit

Permalink
Fix single item enum match type exhaustion
Browse files Browse the repository at this point in the history
  • Loading branch information
superosku committed Feb 29, 2024
1 parent 02c50bc commit 14699cb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
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_type_range(typ).item
)
if not isinstance(get_proper_type(narrowed_type), (LiteralType, UninhabitedType)):
return PatternType(narrowed_type, UnionType.make_union([narrowed_type, rest_type]), {})
Expand Down
29 changes: 29 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,35 @@ match m3:
reveal_type(m3) # N: Revealed type is "Tuple[Union[builtins.int, builtins.str]]"
[builtins fixtures/tuple.pyi]

[case testMatchEnumMultipleChoices]
from enum import Enum
class Medal(Enum):
gold = 1
silver = 1

def f(m: Medal) -> None:
match m:
case Medal.gold:
reveal_type(m) # N: Revealed type is "Literal[__main__.Medal.gold]"
case Medal.silver:
reveal_type(m) # N: Revealed type is "Literal[__main__.Medal.silver]"
case _:
reveal_type(m)
[builtins fixtures/bool.pyi]

[case testMatchEnumSingleChoice]
from enum import Enum
class Medal(Enum):
gold = 1

def f(m: Medal) -> None:
match m:
case Medal.gold:
reveal_type(m) # N: Revealed type is "Literal[__main__.Medal.gold]"
case _:
reveal_type(m)
[builtins fixtures/bool.pyi]

[case testMatchLiteralPatternEnumNegativeNarrowing]
from enum import Enum
class Medal(Enum):
Expand Down

0 comments on commit 14699cb

Please sign in to comment.