@@ -2017,7 +2017,6 @@ from __future__ import annotations
2017
2017
from typing import assert_type, TypedDict, Union
2018
2018
from typing_extensions import final
2019
2019
2020
-
2021
2020
@final
2022
2021
class D(TypedDict):
2023
2022
foo: int
@@ -2030,10 +2029,10 @@ if 'foo' in d:
2030
2029
else:
2031
2030
assert_type(d, list[str])
2032
2031
2033
- [builtins fixtures / dict.pyi]
2034
- [typing fixtures / typing - typeddict.pyi]
2032
+ [builtins fixtures/ dict.pyi]
2033
+ [typing fixtures/ typing- typeddict.pyi]
2035
2034
2036
- [case testOperatorContainsNarrowsTotalTypedDicts ]
2035
+ [case testOperatorContainsNarrowsTypedDicts_total ]
2037
2036
from __future__ import annotations
2038
2037
from typing import assert_type, Literal, TypedDict, TypeVar, Union
2039
2038
from typing_extensions import final
@@ -2048,6 +2047,13 @@ class D2(TypedDict):
2048
2047
bar: int
2049
2048
2050
2049
d: D1 | D2
2050
+ opt_d: D1 | None
2051
+
2052
+ if 'foo' in opt_d:
2053
+ assert_type(opt_d, D1)
2054
+ else:
2055
+ assert_type(opt_d, None)
2056
+
2051
2057
2052
2058
if 'foo' in d:
2053
2059
assert_type(d, D1)
@@ -2081,7 +2087,44 @@ def f(arg: TD) -> None:
2081
2087
[builtins fixtures/dict.pyi]
2082
2088
[typing fixtures/typing-typeddict.pyi]
2083
2089
2084
- [case testOperatorContainsNarrowsPartialTypedDicts]
2090
+ [case testOperatorContainsNarrowsTypedDicts_final]
2091
+ # flags: --warn-unreachable
2092
+ from __future__ import annotations
2093
+ from typing import assert_type, Literal, TypedDict, TypeVar, Union
2094
+ from typing_extensions import final
2095
+
2096
+ @final
2097
+ class DFinal(TypedDict):
2098
+ foo: int
2099
+
2100
+
2101
+ class DNotFinal(TypedDict):
2102
+ bar: int
2103
+
2104
+
2105
+ d_not_final: DNotFinal
2106
+
2107
+ if 'bar' in d_not_final:
2108
+ assert_type(d_not_final, DNotFinal)
2109
+ else:
2110
+ spam = 'ham' # E: Statement is unreachable
2111
+
2112
+ if 'spam' in d_not_final:
2113
+ assert_type(d_not_final, DNotFinal)
2114
+ else:
2115
+ assert_type(d_not_final, DNotFinal)
2116
+
2117
+ d_union: DFinal | DNotFinal
2118
+
2119
+ if 'foo' in d_union:
2120
+ assert_type(d_union, Union[DFinal, DNotFinal])
2121
+ else:
2122
+ assert_type(d_union, DNotFinal)
2123
+
2124
+ [builtins fixtures/dict.pyi]
2125
+ [typing fixtures/typing-typeddict.pyi]
2126
+
2127
+ [case testOperatorContainsNarrowsTypedDicts_partial]
2085
2128
from __future__ import annotations
2086
2129
from typing import assert_type, Literal, TypedDict, Union
2087
2130
from typing_extensions import final
0 commit comments