Skip to content
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

Fix crash on unimported Any in TypedDict #16510

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 11 additions & 9 deletions mypy/semanal_typeddict.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,17 @@ def check_typeddict(
types = [ # unwrap Required[T] to just T
t.item if isinstance(t, RequiredType) else t for t in types
]

# Perform various validations after unwrapping.
for t in types:
check_for_explicit_any(
t, self.options, self.api.is_typeshed_stub_file, self.msg, context=call
)
if self.options.disallow_any_unimported:
for t in types:
if has_any_from_unimported_type(t):
self.msg.unimported_type_becomes_any("Type of a TypedDict key", t, call)

existing_info = None
if isinstance(node.analyzed, TypedDictExpr):
existing_info = node.analyzed.info
Expand Down Expand Up @@ -451,15 +462,6 @@ def parse_typeddict_args(
# One of the types is not ready, defer.
return None
items, types, ok = res
for t in types:
check_for_explicit_any(
t, self.options, self.api.is_typeshed_stub_file, self.msg, context=call
)

if self.options.disallow_any_unimported:
for t in types:
if has_any_from_unimported_type(t):
self.msg.unimported_type_becomes_any("Type of a TypedDict key", t, dictexpr)
assert total is not None
return args[0].value, items, types, total, tvar_defs, ok

Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -3396,3 +3396,12 @@ reveal_type(b["a"]) # N: Revealed type is "Union[builtins.str, None]"
reveal_type(b["g"]) # N: Revealed type is "Union[builtins.int, None]"
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testNoCrashOnUnImportedAnyNotRequired]
# flags: --disallow-any-unimported
from typing import NotRequired, TypedDict
from thismoduledoesntexist import T # type: ignore[import]

B = TypedDict("B", {"T": NotRequired[T]}) # E: Type of a TypedDict key becomes "Any" due to an unfollowed import
ilevkivskyi marked this conversation as resolved.
Show resolved Hide resolved
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]