Skip to content

TypedDict 'in' narrowing w/o @final #15698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5305,9 +5305,7 @@ def conditional_types_for_iterable(
for key in item_str_literals:
if key in possible_iterable_type.required_keys:
if_types.append(possible_iterable_type)
elif (
key in possible_iterable_type.items or not possible_iterable_type.is_final
):
elif key in possible_iterable_type.items:
if_types.append(possible_iterable_type)
else_types.append(possible_iterable_type)
else:
Expand Down
82 changes: 19 additions & 63 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -2017,9 +2017,7 @@ v = {bad2: 2} # E: Missing key "num" for TypedDict "Value" \
[case testOperatorContainsNarrowsTypedDicts_unionWithList]
from __future__ import annotations
from typing import assert_type, TypedDict, Union
from typing_extensions import final

@final
class D(TypedDict):
foo: int

Expand All @@ -2037,27 +2035,28 @@ else:
[typing fixtures/typing-typeddict.pyi]

[case testOperatorContainsNarrowsTypedDicts_total]
# flags: --warn-unreachable
from __future__ import annotations
from typing import assert_type, Literal, TypedDict, TypeVar, Union
from typing_extensions import final

@final
class D1(TypedDict):
foo: int


@final
class D2(TypedDict):
bar: int


d: D1 | D2

if 'foo' in d:
assert_type(d, D1)
else:
assert_type(d, D2)

if 'unknown_key' in d:
object() # E: Statement is unreachable
else:
assert_type(d, Union[D1, D2])

foo_or_bar: Literal['foo', 'bar']
if foo_or_bar in d:
assert_type(d, Union[D1, D2])
Expand All @@ -2083,69 +2082,20 @@ def f(arg: TD) -> None:
else:
assert_type(arg['bar'], int)


[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testOperatorContainsNarrowsTypedDicts_final]
[case testOperatorContainsNarrowsTypedDicts_totalFalse]
# flags: --warn-unreachable
from __future__ import annotations
from typing import assert_type, TypedDict, Union
from typing_extensions import final

@final
class DFinal(TypedDict):
foo: int


class DNotFinal(TypedDict):
bar: int


d_not_final: DNotFinal

if 'bar' in d_not_final:
assert_type(d_not_final, DNotFinal)
else:
spam = 'ham' # E: Statement is unreachable

if 'spam' in d_not_final:
assert_type(d_not_final, DNotFinal)
else:
assert_type(d_not_final, DNotFinal)

d_final: DFinal

if 'spam' in d_final:
spam = 'ham' # E: Statement is unreachable
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this test really useless? E.g. this seems useful and I'm not sure where else it is tested

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, brought it back in a68b87a (and also removed more @finals).

else:
assert_type(d_final, DFinal)

d_union: DFinal | DNotFinal

if 'foo' in d_union:
assert_type(d_union, Union[DFinal, DNotFinal])
else:
assert_type(d_union, DNotFinal)

[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testOperatorContainsNarrowsTypedDicts_partialThroughTotalFalse]
from __future__ import annotations
from typing import assert_type, Literal, TypedDict, Union
from typing_extensions import final

@final
class DTotal(TypedDict):
required_key: int


@final
class DNotTotal(TypedDict, total=False):
optional_key: int


d: DTotal | DNotTotal

if 'required_key' in d:
Expand All @@ -2158,6 +2108,11 @@ if 'optional_key' in d:
else:
assert_type(d, Union[DTotal, DNotTotal])

if 'unknown_key' in d:
object() # E: Statement is unreachable
else:
assert_type(d, Union[DTotal, DNotTotal])

key: Literal['optional_key', 'required_key']
if key in d:
assert_type(d, Union[DTotal, DNotTotal])
Expand All @@ -2167,23 +2122,19 @@ else:
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testOperatorContainsNarrowsTypedDicts_partialThroughNotRequired]
[case testOperatorContainsNarrowsTypedDicts_NotRequired]
# flags: --warn-unreachable
from __future__ import annotations
from typing import assert_type, Required, NotRequired, TypedDict, Union
from typing_extensions import final

@final
class D1(TypedDict):
required_key: Required[int]
optional_key: NotRequired[int]


@final
class D2(TypedDict):
abc: int
xyz: int


d: D1 | D2

if 'required_key' in d:
Expand All @@ -2196,6 +2147,11 @@ if 'optional_key' in d:
else:
assert_type(d, Union[D1, D2])

if 'unknown_key' in d:
object() # E: Statement is unreachable
else:
assert_type(d, Union[D1, D2])

[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

Expand Down