@@ -2035,25 +2035,28 @@ else:
2035
2035
[typing fixtures/typing-typeddict.pyi]
2036
2036
2037
2037
[case testOperatorContainsNarrowsTypedDicts_total]
2038
+ # flags: --warn-unreachable
2038
2039
from __future__ import annotations
2039
2040
from typing import assert_type, Literal, TypedDict, TypeVar, Union
2040
- from typing_extensions import final
2041
2041
2042
2042
class D1(TypedDict):
2043
2043
foo: int
2044
2044
2045
-
2046
2045
class D2(TypedDict):
2047
2046
bar: int
2048
2047
2049
-
2050
2048
d: D1 | D2
2051
2049
2052
2050
if 'foo' in d:
2053
2051
assert_type(d, D1)
2054
2052
else:
2055
2053
assert_type(d, D2)
2056
2054
2055
+ if 'unknown_key' in d:
2056
+ object() # E: Statement is unreachable
2057
+ else:
2058
+ assert_type(d, Union[D1, D2])
2059
+
2057
2060
foo_or_bar: Literal['foo', 'bar']
2058
2061
if foo_or_bar in d:
2059
2062
assert_type(d, Union[D1, D2])
@@ -2079,25 +2082,20 @@ def f(arg: TD) -> None:
2079
2082
else:
2080
2083
assert_type(arg['bar'], int)
2081
2084
2082
-
2083
2085
[builtins fixtures/dict.pyi]
2084
2086
[typing fixtures/typing-typeddict.pyi]
2085
2087
2086
- [case testOperatorContainsNarrowsTypedDicts_partialThroughTotalFalse]
2088
+ [case testOperatorContainsNarrowsTypedDicts_totalFalse]
2089
+ # flags: --warn-unreachable
2087
2090
from __future__ import annotations
2088
2091
from typing import assert_type, Literal, TypedDict, Union
2089
- from typing_extensions import final
2090
2092
2091
- @final
2092
2093
class DTotal(TypedDict):
2093
2094
required_key: int
2094
2095
2095
-
2096
- @final
2097
2096
class DNotTotal(TypedDict, total=False):
2098
2097
optional_key: int
2099
2098
2100
-
2101
2099
d: DTotal | DNotTotal
2102
2100
2103
2101
if 'required_key' in d:
@@ -2110,6 +2108,11 @@ if 'optional_key' in d:
2110
2108
else:
2111
2109
assert_type(d, Union[DTotal, DNotTotal])
2112
2110
2111
+ if 'unknown_key' in d:
2112
+ object() # E: Statement is unreachable
2113
+ else:
2114
+ assert_type(d, Union[DTotal, DNotTotal])
2115
+
2113
2116
key: Literal['optional_key', 'required_key']
2114
2117
if key in d:
2115
2118
assert_type(d, Union[DTotal, DNotTotal])
@@ -2119,23 +2122,19 @@ else:
2119
2122
[builtins fixtures/dict.pyi]
2120
2123
[typing fixtures/typing-typeddict.pyi]
2121
2124
2122
- [case testOperatorContainsNarrowsTypedDicts_partialThroughNotRequired]
2125
+ [case testOperatorContainsNarrowsTypedDicts_NotRequired]
2126
+ # flags: --warn-unreachable
2123
2127
from __future__ import annotations
2124
2128
from typing import assert_type, Required, NotRequired, TypedDict, Union
2125
- from typing_extensions import final
2126
2129
2127
- @final
2128
2130
class D1(TypedDict):
2129
2131
required_key: Required[int]
2130
2132
optional_key: NotRequired[int]
2131
2133
2132
-
2133
- @final
2134
2134
class D2(TypedDict):
2135
2135
abc: int
2136
2136
xyz: int
2137
2137
2138
-
2139
2138
d: D1 | D2
2140
2139
2141
2140
if 'required_key' in d:
@@ -2148,6 +2147,11 @@ if 'optional_key' in d:
2148
2147
else:
2149
2148
assert_type(d, Union[D1, D2])
2150
2149
2150
+ if 'unknown_key' in d:
2151
+ object() # E: Statement is unreachable
2152
+ else:
2153
+ assert_type(d, Union[D1, D2])
2154
+
2151
2155
[builtins fixtures/dict.pyi]
2152
2156
[typing fixtures/typing-typeddict.pyi]
2153
2157
0 commit comments