Skip to content

Commit 11c58a7

Browse files
Allow nesting of Annotated with TypedDict special forms inside TypedDicts (#18165)
This is allowed per the [typing spec](https://typing.readthedocs.io/en/latest/spec/typeddict.html#interaction-with-other-special-types). Updates the TypeAnalyzer to remember whether TypedDict special forms are allowed when visiting `Annotated` types.
1 parent 6759dbd commit 11c58a7

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

mypy/typeanal.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,9 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Typ
688688
code=codes.VALID_TYPE,
689689
)
690690
return AnyType(TypeOfAny.from_error)
691-
return self.anal_type(t.args[0])
691+
return self.anal_type(
692+
t.args[0], allow_typed_dict_special_forms=self.allow_typed_dict_special_forms
693+
)
692694
elif fullname in ("typing_extensions.Required", "typing.Required"):
693695
if not self.allow_typed_dict_special_forms:
694696
self.fail(

test-data/unit/check-typeddict.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3989,6 +3989,16 @@ class TP(TypedDict):
39893989
[builtins fixtures/dict.pyi]
39903990
[typing fixtures/typing-typeddict.pyi]
39913991

3992+
[case testTypedDictAnnotatedWithSpecialForms]
3993+
from typing import NotRequired, ReadOnly, Required, TypedDict
3994+
from typing_extensions import Annotated
3995+
3996+
class A(TypedDict):
3997+
a: Annotated[NotRequired[ReadOnly[int]], ""] # ok
3998+
b: NotRequired[ReadOnly[Annotated[int, ""]]] # ok
3999+
[builtins fixtures/dict.pyi]
4000+
[typing fixtures/typing-typeddict.pyi]
4001+
39924002
[case testTypedDictReadOnlyCovariant]
39934003
from typing import ReadOnly, TypedDict, Union
39944004

0 commit comments

Comments
 (0)