Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Annotated to wrap NotRequired in a TypedDict definition. #15852

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ 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_required=self.allow_required)
elif fullname in ("typing_extensions.Required", "typing.Required"):
if not self.allow_required:
self.fail(
Expand Down Expand Up @@ -1513,11 +1513,18 @@ def anal_array(
self.allow_param_spec_literals = old_allow_param_spec_literals
return self.check_unpacks_in_list(res)

def anal_type(self, t: Type, nested: bool = True, *, allow_param_spec: bool = False) -> Type:
def anal_type(
self,
t: Type,
nested: bool = True,
*,
allow_param_spec: bool = False,
allow_required: bool = False,
) -> Type:
if nested:
self.nesting_level += 1
old_allow_required = self.allow_required
self.allow_required = False
self.allow_required = allow_required
try:
analyzed = t.accept(self)
finally:
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-annotated.test
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,19 @@ from typing_extensions import Annotated
a: Annotated[int, 1:2]
reveal_type(a) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]

[case testAnnotatedNotRequired]
from typing import TypedDict, NotRequired
from typing_extensions import Annotated
class Movie(TypedDict):
title: Annotated[str, "metadata"]
year: Annotated[NotRequired[int], "year_metadata"]
m1: Movie = {
"title": "End of the World",
"year": 1977,
}
m2: Movie = {
"title": "End of the World",
}
[typing fixtures/typing-typeddict.pyi]
[builtins fixtures/dict.pyi]