Skip to content

Commit 9e7acaf

Browse files
gh-101562: typing: add tests for inheritance with NotRequired & Required in parent fields (GH-101563)
(cherry picked from commit b96b344) Co-authored-by: Eclips4 <[email protected]>
1 parent d003bcc commit 9e7acaf

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Lib/test/test_typing.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4872,6 +4872,18 @@ class NontotalMovie(TypedDict, total=False):
48724872
title: Required[str]
48734873
year: int
48744874

4875+
class ParentNontotalMovie(TypedDict, total=False):
4876+
title: Required[str]
4877+
4878+
class ChildTotalMovie(ParentNontotalMovie):
4879+
year: NotRequired[int]
4880+
4881+
class ParentDeeplyAnnotatedMovie(TypedDict):
4882+
title: Annotated[Annotated[Required[str], "foobar"], "another level"]
4883+
4884+
class ChildDeeplyAnnotatedMovie(ParentDeeplyAnnotatedMovie):
4885+
year: NotRequired[Annotated[int, 2000]]
4886+
48754887
class AnnotatedMovie(TypedDict):
48764888
title: Annotated[Required[str], "foobar"]
48774889
year: NotRequired[Annotated[int, 2000]]
@@ -5201,6 +5213,17 @@ def test_get_type_hints_typeddict(self):
52015213
'a': Annotated[Required[int], "a", "b", "c"]
52025214
})
52035215

5216+
self.assertEqual(get_type_hints(ChildTotalMovie), {"title": str, "year": int})
5217+
self.assertEqual(get_type_hints(ChildTotalMovie, include_extras=True), {
5218+
"title": Required[str], "year": NotRequired[int]
5219+
})
5220+
5221+
self.assertEqual(get_type_hints(ChildDeeplyAnnotatedMovie), {"title": str, "year": int})
5222+
self.assertEqual(get_type_hints(ChildDeeplyAnnotatedMovie, include_extras=True), {
5223+
"title": Annotated[Required[str], "foobar", "another level"],
5224+
"year": NotRequired[Annotated[int, 2000]]
5225+
})
5226+
52045227
def test_get_type_hints_collections_abc_callable(self):
52055228
# https://github.com/python/cpython/issues/91621
52065229
P = ParamSpec('P')
@@ -6340,6 +6363,16 @@ def test_required_notrequired_keys(self):
63406363
self.assertEqual(WeirdlyQuotedMovie.__optional_keys__,
63416364
frozenset({"year"}))
63426365

6366+
self.assertEqual(ChildTotalMovie.__required_keys__,
6367+
frozenset({"title"}))
6368+
self.assertEqual(ChildTotalMovie.__optional_keys__,
6369+
frozenset({"year"}))
6370+
6371+
self.assertEqual(ChildDeeplyAnnotatedMovie.__required_keys__,
6372+
frozenset({"title"}))
6373+
self.assertEqual(ChildDeeplyAnnotatedMovie.__optional_keys__,
6374+
frozenset({"year"}))
6375+
63436376
def test_multiple_inheritance(self):
63446377
class One(TypedDict):
63456378
one: int

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,7 @@ Jean-François Piéronne
14031403
Oleg Plakhotnyuk
14041404
Anatoliy Platonov
14051405
Marcel Plch
1406+
Kirill Podoprigora
14061407
Remi Pointel
14071408
Jon Poler
14081409
Ariel Poliak

0 commit comments

Comments
 (0)