8000 Fix crash on unimported Any in TypedDict (#16510) · python/mypy@8c8aa10 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8c8aa10

Browse files
Fix crash on unimported Any in TypedDict (#16510)
Fixes #16498 Fix is trivial: no operations can be done with `Required` types before unwrapping (because they are not real types). --------- Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent b425bd6 commit 8c8aa10

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

mypy/semanal_typeddict.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,17 @@ def check_typeddict(
394394
types = [ # unwrap Required[T] to just T
395395
t.item if isinstance(t, RequiredType) else t for t in types
396396
]
397+
398+
# Perform various validations after unwrapping.
399+
for t in types:
400+
check_for_explicit_any(
401+
t, self.options, self.api.is_typeshed_stub_file, self.msg, context=call
402+
)
403+
if self.options.disallow_any_unimported:
404+
for t in types:
405+
if has_any_from_unimported_type(t):
406+
self.msg.unimported_type_becomes_any("Type of a TypedDict key", t, call)
407+
397408
existing_info = None
398409
if isinstance(node.analyzed, TypedDictExpr):
399410
existing_info = node.analyzed.info
@@ -451,15 +462,6 @@ def parse_typeddict_args(
451462
# One of the types is not ready, defer.
452463
return None
453464
items, types, ok = res
454-
for t in types:
455-
check_for_explicit_any(
456-
t, self.options, self.api.is_typeshed_stub_file, self.msg, context=call
457-
)
458-
459-
if self.options.disallow_any_unimported:
460-
for t in types:
461-
if has_any_from_unimported_type(t):
462-
self.msg.unimported_type_becomes_any("Type of a TypedDict key", t, dictexpr)
463465
assert total is not None
464466
return args[0].value, items, types, total, tvar_defs, ok
465467

test-data/unit/check-typeddict.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3396,3 +3396,15 @@ reveal_type(b["a"]) # N: Revealed type is "Union[builtins.str, None]"
33963396
reveal_type(b["g"]) # N: Revealed type is "Union[builtins.int, None]"
33973397
[builtins fixtures/dict.pyi]
33983398
[typing fixtures/typing-typeddict.pyi]
3399+
3400+
[case testNoCrashOnUnImportedAnyNotRequired]
3401+
# flags: --disallow-any-unimported
3402+
from typing import NotRequired, Required, TypedDict
3403+
from thismoduledoesntexist import T # type: ignore[import]
3404+
3405+
B = TypedDict("B", { # E: Type of a TypedDict key becomes "Any" due to an unfollowed import
3406+
"T1": NotRequired[T],
3407+
"T2": Required[T],
3408+
})
3409+
[builtins fixtures/dict.pyi]
3410+
[typing fixtures/typing-typeddict.pyi]

0 commit comments

Comments
 (0)
0