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

TypedDict: Resolve string-based annotations #60

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/test_typing_extensions.py
Expand Up @@ -638,6 +638,10 @@ class AnnotatedMovie(TypedDict):
title: Annotated[Required[str], "foobar"]
year: NotRequired[Annotated[int, 2000]]

class StringBasedMovie(TypedDict):
title: "str"
year: "NotRequired[int]"


gth = get_type_hints

Expand Down Expand Up @@ -1829,6 +1833,9 @@ def test_required_notrequired_keys(self):
assert TotalMovie.__required_keys__ == frozenset({'title'})
assert TotalMovie.__optional_keys__ == frozenset({'year'})

assert StringBasedMovie.__required_keys__ == frozenset({'title'})
assert StringBasedMovie.__optional_keys__ == frozenset({'year'})


def test_keys_inheritance(self):
assert BaseAnimal.__required_keys__ == frozenset(['name'])
Expand Down
10 changes: 8 additions & 2 deletions src/typing_extensions.py
Expand Up @@ -718,7 +718,10 @@ def __new__(cls, name, bases, ns, total=True):
_maybe_adjust_parameters(tp_dict)

annotations = {}
own_annotations = ns.get('__annotations__', {})
if sys.version_info >= (3, 9):
own_annotations = typing.get_type_hints(tp_dict, include_extras=True)
else:
own_annotations = typing.get_type_hints(tp_dict)
msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
own_annotations = {
n: typing._type_check(tp, msg) for n, tp in own_annotations.items()
Expand All @@ -727,7 +730,10 @@ def __new__(cls, name, bases, ns, total=True):
optional_keys = set()

for base in bases:
annotations.update(base.__dict__.get('__annotations__', {}))
if sys.version_info >= (3, 9):
annotations.update(typing.get_type_hints(base, include_extras=True))
else:
annotations.update(typing.get_type_hints(base))
required_keys.update(base.__dict__.get('__required_keys__', ()))
optional_keys.update(base.__dict__.get('__optional_keys__', ()))

Expand Down