Skip to content

Commit

Permalink
Unwrap TypedDict item types before storing (#17640)
Browse files Browse the repository at this point in the history
Fixes #17604
Fixes #17608

Fix is trivial, rectify an obvious omission in my original PR.
  • Loading branch information
ilevkivskyi authored Aug 4, 2024
1 parent bc39f17 commit b56f357
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mypy/semanal_typeddict.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ def analyze_typeddict_classdef_fields(
return None, [], [], set() # Need to defer
types.append(analyzed)
if not has_placeholder(analyzed):
stmt.type = analyzed
stmt.type = (
analyzed.item if isinstance(analyzed, RequiredType) else analyzed
)
# ...despite possible minor failures that allow further analysis.
if stmt.type is None or hasattr(stmt, "new_syntax") and not stmt.new_syntax:
self.fail(TPDICT_CLASS_ERROR, stmt)
Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -2382,6 +2382,14 @@ class ForceDeferredEval: pass
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testTypedDictRequiredUnimportedAny]
# flags: --disallow-any-unimported
from typing import NotRequired, TypedDict
from nonexistent import Foo # type: ignore[import-not-found]
class Bar(TypedDict):
foo: NotRequired[Foo] # E: Type of variable becomes "Any" due to an unfollowed import
[typing fixtures/typing-typeddict.pyi]

-- Required[]

[case testDoesRecognizeRequiredInTypedDictWithClass]
Expand Down

0 comments on commit b56f357

Please sign in to comment.