Skip to content

Commit

Permalink
Allow nesting of Annotated with TypedDict special forms inside TypedD…
Browse files Browse the repository at this point in the history
…icts (#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.
  • Loading branch information
brianschubert authored Nov 19, 2024
1 parent 6759dbd commit 11c58a7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 10 additions & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -3989,6 +3989,16 @@ class TP(TypedDict):
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testTypedDictAnnotatedWithSpecialForms]
from typing import NotRequired, ReadOnly, Required, TypedDict
from typing_extensions import Annotated

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

Expand Down

0 comments on commit 11c58a7

Please sign in to comment.