Skip to content

Commit b96b344

Browse files
authored
gh-101562: typing: add tests for inheritance with NotRequired & Required in parent fields (#101563)
1 parent 7a25310 commit b96b344

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
@@ -4892,6 +4892,18 @@ class NontotalMovie(TypedDict, total=False):
48924892
title: Required[str]
48934893
year: int
48944894

4895+
class ParentNontotalMovie(TypedDict, total=False):
4896+
title: Required[str]
4897+
4898+
class ChildTotalMovie(ParentNontotalMovie):
4899+
year: NotRequired[int]
4900+
4901+
class ParentDeeplyAnnotatedMovie(TypedDict):
4902+
title: Annotated[Annotated[Required[str], "foobar"], "another level"]
4903+
4904+
class ChildDeeplyAnnotatedMovie(ParentDeeplyAnnotatedMovie):
4905+
year: NotRequired[Annotated[int, 2000]]
4906+
48954907
class AnnotatedMovie(TypedDict):
48964908
title: Annotated[Required[str], "foobar"]
48974909
year: NotRequired[Annotated[int, 2000]]
@@ -5221,6 +5233,17 @@ def test_get_type_hints_typeddict(self):
52215233
'a': Annotated[Required[int], "a", "b", "c"]
52225234
})
52235235

5236+
self.assertEqual(get_type_hints(ChildTotalMovie), {"title": str, "year": int})
5237+
self.assertEqual(get_type_hints(ChildTotalMovie, include_extras=True), {
5238+
"title": Required[str], "year": NotRequired[int]
5239+
})
5240+
5241+
self.assertEqual(get_type_hints(ChildDeeplyAnnotatedMovie), {"title": str, "year": int})
5242+
self.assertEqual(get_type_hints(ChildDeeplyAnnotatedMovie, include_extras=True), {
5243+
"title": Annotated[Required[str], "foobar", "another level"],
5244+
"year": NotRequired[Annotated[int, 2000]]
5245+
})
5246+
52245247
def test_get_type_hints_collections_abc_callable(self):
52255248
# https://github.com/python/cpython/issues/91621
52265249
P = ParamSpec('P')
@@ -6381,6 +6404,16 @@ def test_required_notrequired_keys(self):
63816404
self.assertEqual(WeirdlyQuotedMovie.__optional_keys__,
63826405
frozenset({"year"}))
63836406

6407+
self.assertEqual(ChildTotalMovie.__required_keys__,
6408+
frozenset({"title"}))
6409+
self.assertEqual(ChildTotalMovie.__optional_keys__,
6410+
frozenset({"year"}))
6411+
6412+
self.assertEqual(ChildDeeplyAnnotatedMovie.__required_keys__,
6413+
frozenset({"title"}))
6414+
self.assertEqual(ChildDeeplyAnnotatedMovie.__optional_keys__,
6415+
frozenset({"year"}))
6416+
63846417
def test_multiple_inheritance(self):
63856418
class One(TypedDict):
63866419
one: int

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,7 @@ Jean-François Piéronne
14141414
Oleg Plakhotnyuk
14151415
Anatoliy Platonov
14161416
Marcel Plch
1417+
Kirill Podoprigora
14171418
Remi Pointel
14181419
Jon Poler
14191420
Ariel Poliak

0 commit comments

Comments
 (0)