Skip to content

Commit a8dbd22

Browse files
committed
TypedDict 'in' narrowing w/o @Final
1 parent b6b6624 commit a8dbd22

File tree

2 files changed

+1
-51
lines changed

2 files changed

+1
-51
lines changed

mypy/checker.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5283,9 +5283,7 @@ def conditional_types_for_iterable(
52835283
for key in item_str_literals:
52845284
if key in possible_iterable_type.required_keys:
52855285
if_types.append(possible_iterable_type)
5286-
elif (
5287-
key in possible_iterable_type.items or not possible_iterable_type.is_final
5288-
):
5286+
elif key in possible_iterable_type.items:
52895287
if_types.append(possible_iterable_type)
52905288
else_types.append(possible_iterable_type)
52915289
else:

test-data/unit/check-typeddict.test

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,9 +2017,7 @@ v = {bad2: 2} # E: Missing key "num" for TypedDict "Value" \
20172017
[case testOperatorContainsNarrowsTypedDicts_unionWithList]
20182018
from __future__ import annotations
20192019
from typing import assert_type, TypedDict, Union
2020-
from typing_extensions import final
20212020

2022-
@final
20232021
class D(TypedDict):
20242022
foo: int
20252023

@@ -2041,12 +2039,10 @@ from __future__ import annotations
20412039
from typing import assert_type, Literal, TypedDict, TypeVar, Union
20422040
from typing_extensions import final
20432041

2044-
@final
20452042
class D1(TypedDict):
20462043
foo: int
20472044

20482045

2049-
@final
20502046
class D2(TypedDict):
20512047
bar: int
20522048

@@ -2087,50 +2083,6 @@ def f(arg: TD) -> None:
20872083
[builtins fixtures/dict.pyi]
20882084
[typing fixtures/typing-typeddict.pyi]
20892085

2090-
[case testOperatorContainsNarrowsTypedDicts_final]
2091-
# flags: --warn-unreachable
2092-
from __future__ import annotations
2093-
from typing import assert_type, TypedDict, 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_final: DFinal
2118-
2119-
if 'spam' in d_final:
2120-
spam = 'ham' # E: Statement is unreachable
2121-
else:
2122-
assert_type(d_final, DFinal)
2123-
2124-
d_union: DFinal | DNotFinal
2125-
2126-
if 'foo' in d_union:
2127-
assert_type(d_union, Union[DFinal, DNotFinal])
2128-
else:
2129-
assert_type(d_union, DNotFinal)
2130-
2131-
[builtins fixtures/dict.pyi]
2132-
[typing fixtures/typing-typeddict.pyi]
2133-
21342086
[case testOperatorContainsNarrowsTypedDicts_partialThroughTotalFalse]
21352087
from __future__ import annotations
21362088
from typing import assert_type, Literal, TypedDict, Union

0 commit comments

Comments
 (0)