@@ -2639,6 +2639,164 @@ def f2() -> None:
26392639 reveal_type(y) # N: Revealed type is "builtins.str"
26402640[builtins fixtures/list.pyi]
26412641
2642+ [case testExhaustiveMatchNoFlag]
2643+
2644+ a: int = 5
2645+ match a:
2646+ case 1:
2647+ pass
2648+ case _:
2649+ pass
2650+
2651+ b: str = "hello"
2652+ match b:
2653+ case "bye":
2654+ pass
2655+ case _:
2656+ pass
2657+
2658+ [case testNonExhaustiveMatchNoFlag]
2659+
2660+ a: int = 5
2661+ match a:
2662+ case 1:
2663+ pass
2664+
2665+ b: str = "hello"
2666+ match b:
2667+ case "bye":
2668+ pass
2669+
2670+
2671+ [case testExhaustiveMatchWithFlag]
2672+ # flags: --enable-error-code exhaustive-match
2673+
2674+ a: int = 5
2675+ match a:
2676+ case 1:
2677+ pass
2678+ case _:
2679+ pass
2680+
2681+ b: str = "hello"
2682+ match b:
2683+ case "bye":
2684+ pass
2685+ case _:
2686+ pass
2687+
2688+ [case testNonExhaustiveMatchWithFlag]
2689+ # flags: --enable-error-code exhaustive-match
2690+
2691+ a: int = 5
2692+ match a: # E: Match statement has unhandled case for values of type "int" \
2693+ # N: If match statement is intended to be non-exhaustive, add `case _: pass`
2694+ case 1:
2695+ pass
2696+
2697+ b: str = "hello"
2698+ match b: # E: Match statement has unhandled case for values of type "str" \
2699+ # N: If match statement is intended to be non-exhaustive, add `case _: pass`
2700+ case "bye":
2701+ pass
2702+ [case testNonExhaustiveMatchEnumWithFlag]
2703+ # flags: --enable-error-code exhaustive-match
2704+
2705+ import enum
2706+
2707+ class Color(enum.Enum):
2708+ RED = 1
2709+ BLUE = 2
2710+ GREEN = 3
2711+
2712+ val: Color = Color.RED
2713+
2714+ match val: # E: Match statement has unhandled case for values of type "Literal[Color.GREEN]" \
2715+ # N: If match statement is intended to be non-exhaustive, add `case _: pass`
2716+ case Color.RED:
2717+ a = "red"
2718+ case Color.BLUE:
2719+ a= "blue"
2720+ [builtins fixtures/enum.pyi]
2721+
2722+ [case testExhaustiveMatchEnumWithFlag]
2723+ # flags: --enable-error-code exhaustive-match
2724+
2725+ import enum
2726+
2727+ class Color(enum.Enum):
2728+ RED = 1
2729+ BLUE = 2
2730+
2731+ val: Color = Color.RED
2732+
2733+ match val:
2734+ case Color.RED:
2735+ a = "red"
2736+ case Color.BLUE:
2737+ a= "blue"
2738+ [builtins fixtures/enum.pyi]
2739+
2740+ [case testNonExhaustiveMatchEnumMultipleMissingMatchesWithFlag]
2741+ # flags: --enable-error-code exhaustive-match
2742+
2743+ import enum
2744+
2745+ class Color(enum.Enum):
2746+ RED = 1
2747+ BLUE = 2
2748+ GREEN = 3
2749+
2750+ val: Color = Color.RED
2751+
2752+ match val: # E: Match statement has unhandled case for values of type "Literal[Color.BLUE, Color.GREEN]" \
2753+ # N: If match statement is intended to be non-exhaustive, add `case _: pass`
2754+ case Color.RED:
2755+ a = "red"
2756+ [builtins fixtures/enum.pyi]
2757+
2758+ [case testExhaustiveMatchEnumFallbackWithFlag]
2759+ # flags: --enable-error-code exhaustive-match
2760+
2761+ import enum
2762+
2763+ class Color(enum.Enum):
2764+ RED = 1
2765+ BLUE = 2
2766+ GREEN = 3
2767+
2768+ val: Color = Color.RED
2769+
2770+ match val:
2771+ case Color.RED:
2772+ a = "red"
2773+ case _:
2774+ a = "other"
2775+ [builtins fixtures/enum.pyi]
2776+
2777+ # Fork of testMatchNarrowingUnionTypedDictViaIndex to check behaviour with exhaustive match flag
2778+ [case testExhaustiveMatchNarrowingUnionTypedDictViaIndex]
2779+ # flags: --enable-error-code exhaustive-match
2780+
2781+ from typing import Literal, TypedDict
2782+
2783+ class A(TypedDict):
2784+ tag: Literal["a"]
2785+ name: str
2786+
2787+ class B(TypedDict):
2788+ tag: Literal["b"]
2789+ num: int
2790+
2791+ d: A | B
2792+ match d["tag"]: # E: Match statement has unhandled case for values of type "Literal['b']" \
2793+ # N: If match statement is intended to be non-exhaustive, add `case _: pass` \
2794+ # E: Match statement has unhandled case for values of type "B"
2795+ case "a":
2796+ reveal_type(d) # N: Revealed type is "TypedDict('__main__.A', {'tag': Literal['a'], 'name': builtins.str})"
2797+ reveal_type(d["name"]) # N: Revealed type is "builtins.str"
2798+ [typing fixtures/typing-typeddict.pyi]
2799+
26422800[case testEnumTypeObjectMember]
26432801import enum
26442802from typing import NoReturn
0 commit comments