@@ -11,11 +11,30 @@ match m:
1111-- Literal Pattern --
1212
1313[case testMatchLiteralPatternNarrows]
14+ # flags: --warn-unreachable
1415m: object
1516
1617match m:
1718 case 1:
1819 reveal_type(m) # N: Revealed type is "Literal[1]"
20+ case 2:
21+ reveal_type(m) # N: Revealed type is "Literal[2]"
22+ case other:
23+ reveal_type(other) # N: Revealed type is "builtins.object"
24+
25+ [case testMatchLiteralPatternNarrows2]
26+ # flags: --warn-unreachable
27+ from typing import Any
28+
29+ m: Any
30+
31+ match m:
32+ case 1:
33+ reveal_type(m) # N: Revealed type is "Literal[1]"
34+ case 2:
35+ reveal_type(m) # N: Revealed type is "Literal[2]"
36+ case other:
37+ reveal_type(other) # N: Revealed type is "Any"
1938
2039[case testMatchLiteralPatternAlreadyNarrower-skip]
2140m: bool
@@ -1070,28 +1089,42 @@ match m:
10701089 pass
10711090
10721091[case testMatchClassPatternCallable]
1073- from typing import Callable
1092+ # flags: --warn-unreachable
1093+ from typing import Callable, Any
10741094
10751095class FnImpl:
10761096 def __call__(self, x: object, /) -> int: ...
10771097
1098+ def test_any(x: Any) -> None:
1099+ match x:
1100+ case Callable() as fn:
1101+ reveal_type(fn) # N: Revealed type is "def (*Any, **Any) -> Any"
1102+ case other:
1103+ reveal_type(other) # N: Revealed type is "Any"
1104+
10781105def test_object(x: object) -> None:
10791106 match x:
10801107 case Callable() as fn:
10811108 reveal_type(fn) # N: Revealed type is "def (*Any, **Any) -> Any"
1109+ case other:
1110+ reveal_type(other) # N: Revealed type is "builtins.object"
10821111
10831112def test_impl(x: FnImpl) -> None:
10841113 match x:
10851114 case Callable() as fn:
10861115 reveal_type(fn) # N: Revealed type is "__main__.FnImpl"
1116+ case other:
1117+ reveal_type(other) # E: Statement is unreachable
10871118
10881119def test_callable(x: Callable[[object], int]) -> None:
10891120 match x:
10901121 case Callable() as fn:
10911122 reveal_type(fn) # N: Revealed type is "def (builtins.object) -> builtins.int"
1092-
1123+ case other:
1124+ reveal_type(other) # E: Statement is unreachable
10931125
10941126[case testMatchClassPatternCallbackProtocol]
1127+ # flags: --warn-unreachable
10951128from typing import Any, Callable
10961129from typing_extensions import Protocol, runtime_checkable
10971130
@@ -1102,24 +1135,38 @@ class FnProto(Protocol):
11021135class FnImpl:
11031136 def __call__(self, x: object, /) -> int: ...
11041137
1138+ def test_any(x: Any) -> None:
1139+ match x:
1140+ case FnProto() as fn:
1141+ reveal_type(fn) # N: Revealed type is "__main__.FnProto"
1142+ case other:
1143+ reveal_type(other) # N: Revealed type is "Any"
1144+
11051145def test_object(x: object) -> None:
11061146 match x:
11071147 case FnProto() as fn:
11081148 reveal_type(fn) # N: Revealed type is "__main__.FnProto"
1149+ case other:
1150+ reveal_type(other) # N: Revealed type is "builtins.object"
11091151
11101152def test_impl(x: FnImpl) -> None:
11111153 match x:
11121154 case FnProto() as fn:
11131155 reveal_type(fn) # N: Revealed type is "__main__.FnImpl"
1156+ case other:
1157+ reveal_type(other) # E: Statement is unreachable
11141158
11151159def test_callable(x: Callable[[object], int]) -> None:
11161160 match x:
11171161 case FnProto() as fn:
11181162 reveal_type(fn) # N: Revealed type is "def (builtins.object) -> builtins.int"
1163+ case other:
1164+ reveal_type(other) # E: Statement is unreachable
11191165
11201166[builtins fixtures/dict.pyi]
11211167
11221168[case testMatchClassPatternAnyCallableProtocol]
1169+ # flags: --warn-unreachable
11231170from typing import Any, Callable
11241171from typing_extensions import Protocol, runtime_checkable
11251172
@@ -1134,16 +1181,22 @@ def test_object(x: object) -> None:
11341181 match x:
11351182 case AnyCallable() as fn:
11361183 reveal_type(fn) # N: Revealed type is "__main__.AnyCallable"
1184+ case other:
1185+ reveal_type(other) # N: Revealed type is "builtins.object"
11371186
11381187def test_impl(x: FnImpl) -> None:
11391188 match x:
11401189 case AnyCallable() as fn:
11411190 reveal_type(fn) # N: Revealed type is "__main__.FnImpl"
1191+ case other:
1192+ reveal_type(other) # E: Statement is unreachable
11421193
11431194def test_callable(x: Callable[[object], int]) -> None:
11441195 match x:
11451196 case AnyCallable() as fn:
11461197 reveal_type(fn) # N: Revealed type is "def (builtins.object) -> builtins.int"
1198+ case other:
1199+ reveal_type(other) # E: Statement is unreachable
11471200
11481201[builtins fixtures/dict.pyi]
11491202
0 commit comments