From ec6dc1bc20da15dc0d8dd916d487dd3cbb7f6b69 Mon Sep 17 00:00:00 2001 From: Brian Schubert Date: Mon, 18 Nov 2024 20:23:50 -0500 Subject: [PATCH 1/2] Allow TypedDict special forms inside Annotated --- mypy/typeanal.py | 4 +++- test-data/unit/check-typeddict.test | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/mypy/typeanal.py b/mypy/typeanal.py index 0c241f5c0f99..74f649ed69b1 100644 --- a/mypy/typeanal.py +++ b/mypy/typeanal.py @@ -688,7 +688,9 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Typ code=codes.VALID_TYPE, ) return AnyType(TypeOfAny.from_error) - return self.anal_type(t.args[0]) + return self.anal_type( + t.args[0], allow_typed_dict_special_forms=self.allow_typed_dict_special_forms + ) elif fullname in ("typing_extensions.Required", "typing.Required"): if not self.allow_typed_dict_special_forms: self.fail( diff --git a/test-data/unit/check-typeddict.test b/test-data/unit/check-typeddict.test index affa472bb640..e4c3808ace8d 100644 --- a/test-data/unit/check-typeddict.test +++ b/test-data/unit/check-typeddict.test @@ -3989,6 +3989,15 @@ class TP(TypedDict): [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] +[case testTypedDictAnnotatedWithSpecialForms] +from typing import NotRequired, ReadOnly, Required, TypedDict + +class A(TypedDict): + a: Annotated[NotRequired[ReadOnly[int]], ""] # ok + b: NotRequired[ReadOnly[Annotated[int, ""]]] # ok +[builtins fixtures/dict.pyi] +[typing fixtures/typing-typeddict.pyi] + [case testTypedDictReadOnlyCovariant] from typing import ReadOnly, TypedDict, Union From 959c8df82aecaf97687fe19e06990ae8f4606660 Mon Sep 17 00:00:00 2001 From: Brian Schubert Date: Mon, 18 Nov 2024 21:02:02 -0500 Subject: [PATCH 2/2] Import Annotated from typing_extensions --- test-data/unit/check-typeddict.test | 1 + 1 file changed, 1 insertion(+) diff --git a/test-data/unit/check-typeddict.test b/test-data/unit/check-typeddict.test index e4c3808ace8d..c62061f75521 100644 --- a/test-data/unit/check-typeddict.test +++ b/test-data/unit/check-typeddict.test @@ -3991,6 +3991,7 @@ class TP(TypedDict): [case testTypedDictAnnotatedWithSpecialForms] from typing import NotRequired, ReadOnly, Required, TypedDict +from typing_extensions import Annotated class A(TypedDict): a: Annotated[NotRequired[ReadOnly[int]], ""] # ok