From 7d805b364ee80396e0b9ca906f32f901b2ac7e12 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Sun, 4 Aug 2024 17:56:13 +0100 Subject: [PATCH] Unwrap TypedDict item types before storing (#17640) Fixes https://github.com/python/mypy/issues/17604 Fixes https://github.com/python/mypy/issues/17608 Fix is trivial, rectify an obvious omission in my original PR. (cherry picked from commit b56f357a45838c204c951deeb6cbdbf143258e0c) --- mypy/semanal_typeddict.py | 4 +++- test-data/unit/check-typeddict.test | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/mypy/semanal_typeddict.py b/mypy/semanal_typeddict.py index 7b8d874337a2..81e318b57d05 100644 --- a/mypy/semanal_typeddict.py +++ b/mypy/semanal_typeddict.py @@ -323,7 +323,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) diff --git a/test-data/unit/check-typeddict.test b/test-data/unit/check-typeddict.test index d35ec8ddd80e..006fe825528c 100644 --- a/test-data/unit/check-typeddict.test +++ b/test-data/unit/check-typeddict.test @@ -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]