From 5cc30c9092d40d779ee48f8edc14187b8cc4d9dc Mon Sep 17 00:00:00 2001 From: Oskari Lehto Date: Thu, 29 Feb 2024 16:31:17 +0200 Subject: [PATCH] Fix single item enum match type exhaustion --- mypy/checkpattern.py | 2 +- test-data/unit/check-python310.test | 34 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/mypy/checkpattern.py b/mypy/checkpattern.py index 7b6a55324741a..173dcd79b474a 100644 --- a/mypy/checkpattern.py +++ b/mypy/checkpattern.py @@ -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]), {}) diff --git a/test-data/unit/check-python310.test b/test-data/unit/check-python310.test index b0e27fe1e3a05..26cd67e43e063 100644 --- a/test-data/unit/check-python310.test +++ b/test-data/unit/check-python310.test @@ -1369,6 +1369,40 @@ 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) -> 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 2 + case _: + reveal_type(m) + return 2 +[builtins fixtures/bool.pyi] + +[case testMatchEnumSingleChoice] +from enum import Enum +class Medal(Enum): + gold = 1 + +def f(m: Medal) -> int: + match m: + case Medal.gold: + reveal_type(m) # N: Revealed type is "Literal[__main__.Medal.gold]" + return 0 + case _: + reveal_type(m) + return 1 +[builtins fixtures/bool.pyi] + [case testMatchLiteralPatternEnumNegativeNarrowing] from enum import Enum class Medal(Enum):