Skip to content

Commit b56f357

Browse files
authored
Unwrap TypedDict item types before storing (#17640)
Fixes #17604 Fixes #17608 Fix is trivial, rectify an obvious omission in my original PR.
1 parent bc39f17 commit b56f357

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

mypy/semanal_typeddict.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,9 @@ def analyze_typeddict_classdef_fields(
324324
return None, [], [], set() # Need to defer
325325
types.append(analyzed)
326326
if not has_placeholder(analyzed):
327-
stmt.type = analyzed
327+
stmt.type = (
328+
analyzed.item if isinstance(analyzed, RequiredType) else analyzed
329+
)
328330
# ...despite possible minor failures that allow further analysis.
329331
if stmt.type is None or hasattr(stmt, "new_syntax") and not stmt.new_syntax:
330332
self.fail(TPDICT_CLASS_ERROR, stmt)

test-data/unit/check-typeddict.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,6 +2382,14 @@ class ForceDeferredEval: pass
23822382
[builtins fixtures/dict.pyi]
23832383
[typing fixtures/typing-typeddict.pyi]
23842384

2385+
[case testTypedDictRequiredUnimportedAny]
2386+
# flags: --disallow-any-unimported
2387+
from typing import NotRequired, TypedDict
2388+
from nonexistent import Foo # type: ignore[import-not-found]
2389+
class Bar(TypedDict):
2390+
foo: NotRequired[Foo] # E: Type of variable becomes "Any" due to an unfollowed import
2391+
[typing fixtures/typing-typeddict.pyi]
2392+
23852393
-- Required[]
23862394

23872395
[case testDoesRecognizeRequiredInTypedDictWithClass]

0 commit comments

Comments
 (0)