Skip to content

Commit 7d805b3

Browse files
ilevkivskyihauntsaninja
authored andcommitted
Unwrap TypedDict item types before storing (#17640)
Fixes #17604 Fixes #17608 Fix is trivial, rectify an obvious omission in my original PR. (cherry picked from commit b56f357)
1 parent 32675dd commit 7d805b3

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

mypy/semanal_typeddict.py

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

test-data/unit/check-typeddict.test

+8
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)