@@ -11,11 +11,30 @@ match m:
11
11
-- Literal Pattern --
12
12
13
13
[case testMatchLiteralPatternNarrows]
14
+ # flags: --warn-unreachable
14
15
m: object
15
16
16
17
match m:
17
18
case 1:
18
19
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"
19
38
20
39
[case testMatchLiteralPatternAlreadyNarrower-skip]
21
40
m: bool
@@ -1070,28 +1089,42 @@ match m:
1070
1089
pass
1071
1090
1072
1091
[case testMatchClassPatternCallable]
1073
- from typing import Callable
1092
+ # flags: --warn-unreachable
1093
+ from typing import Callable, Any
1074
1094
1075
1095
class FnImpl:
1076
1096
def __call__(self, x: object, /) -> int: ...
1077
1097
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
+
1078
1105
def test_object(x: object) -> None:
1079
1106
match x:
1080
1107
case Callable() as fn:
1081
1108
reveal_type(fn) # N: Revealed type is "def (*Any, **Any) -> Any"
1109
+ case other:
1110
+ reveal_type(other) # N: Revealed type is "builtins.object"
1082
1111
1083
1112
def test_impl(x: FnImpl) -> None:
1084
1113
match x:
1085
1114
case Callable() as fn:
1086
1115
reveal_type(fn) # N: Revealed type is "__main__.FnImpl"
1116
+ case other:
1117
+ reveal_type(other) # E: Statement is unreachable
1087
1118
1088
1119
def test_callable(x: Callable[[object], int]) -> None:
1089
1120
match x:
1090
1121
case Callable() as fn:
1091
1122
reveal_type(fn) # N: Revealed type is "def (builtins.object) -> builtins.int"
1092
-
1123
+ case other:
1124
+ reveal_type(other) # E: Statement is unreachable
1093
1125
1094
1126
[case testMatchClassPatternCallbackProtocol]
1127
+ # flags: --warn-unreachable
1095
1128
from typing import Any, Callable
1096
1129
from typing_extensions import Protocol, runtime_checkable
1097
1130
@@ -1102,24 +1135,38 @@ class FnProto(Protocol):
1102
1135
class FnImpl:
1103
1136
def __call__(self, x: object, /) -> int: ...
1104
1137
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
+
1105
1145
def test_object(x: object) -> None:
1106
1146
match x:
1107
1147
case FnProto() as fn:
1108
1148
reveal_type(fn) # N: Revealed type is "__main__.FnProto"
1149
+ case other:
1150
+ reveal_type(other) # N: Revealed type is "builtins.object"
1109
1151
1110
1152
def test_impl(x: FnImpl) -> None:
1111
1153
match x:
1112
1154
case FnProto() as fn:
1113
1155
reveal_type(fn) # N: Revealed type is "__main__.FnImpl"
1156
+ case other:
1157
+ reveal_type(other) # E: Statement is unreachable
1114
1158
1115
1159
def test_callable(x: Callable[[object], int]) -> None:
1116
1160
match x:
1117
1161
case FnProto() as fn:
1118
1162
reveal_type(fn) # N: Revealed type is "def (builtins.object) -> builtins.int"
1163
+ case other:
1164
+ reveal_type(other) # E: Statement is unreachable
1119
1165
1120
1166
[builtins fixtures/dict.pyi]
1121
1167
1122
1168
[case testMatchClassPatternAnyCallableProtocol]
1169
+ # flags: --warn-unreachable
1123
1170
from typing import Any, Callable
1124
1171
from typing_extensions import Protocol, runtime_checkable
1125
1172
@@ -1134,16 +1181,22 @@ def test_object(x: object) -> None:
1134
1181
match x:
1135
1182
case AnyCallable() as fn:
1136
1183
reveal_type(fn) # N: Revealed type is "__main__.AnyCallable"
1184
+ case other:
1185
+ reveal_type(other) # N: Revealed type is "builtins.object"
1137
1186
1138
1187
def test_impl(x: FnImpl) -> None:
1139
1188
match x:
1140
1189
case AnyCallable() as fn:
1141
1190
reveal_type(fn) # N: Revealed type is "__main__.FnImpl"
1191
+ case other:
1192
+ reveal_type(other) # E: Statement is unreachable
1142
1193
1143
1194
def test_callable(x: Callable[[object], int]) -> None:
1144
1195
match x:
1145
1196
case AnyCallable() as fn:
1146
1197
reveal_type(fn) # N: Revealed type is "def (builtins.object) -> builtins.int"
1198
+ case other:
1199
+ reveal_type(other) # E: Statement is unreachable
1147
1200
1148
1201
[builtins fixtures/dict.pyi]
1149
1202
0 commit comments