[Python-checkins] gh-101562: typing: add tests for inheritance with NotRequired & Required in parent fields (GH-101563)

miss-islington webhook-mailer at python.org
Mon Feb 6 14:53:59 EST 2023


https://github.com/python/cpython/commit/9e7acafa14e30a3c0cc20245ff6987cd732bf269
commit: 9e7acafa14e30a3c0cc20245ff6987cd732bf269
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2023-02-06T11:53:53-08:00
summary:

gh-101562: typing: add tests for inheritance with NotRequired & Required in parent fields (GH-101563)

(cherry picked from commit b96b344f251954bb64aeb13c3e0c460350565c2a)

Co-authored-by: Eclips4 <80244920+Eclips4 at users.noreply.github.com>

files:
M Lib/test/test_typing.py
M Misc/ACKS

diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 10023af4f788..276f95bacfcf 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -4872,6 +4872,18 @@ class NontotalMovie(TypedDict, total=False):
     title: Required[str]
     year: int
 
+class ParentNontotalMovie(TypedDict, total=False):
+    title: Required[str]
+
+class ChildTotalMovie(ParentNontotalMovie):
+    year: NotRequired[int]
+
+class ParentDeeplyAnnotatedMovie(TypedDict):
+    title: Annotated[Annotated[Required[str], "foobar"], "another level"]
+
+class ChildDeeplyAnnotatedMovie(ParentDeeplyAnnotatedMovie):
+    year: NotRequired[Annotated[int, 2000]]
+
 class AnnotatedMovie(TypedDict):
     title: Annotated[Required[str], "foobar"]
     year: NotRequired[Annotated[int, 2000]]
@@ -5201,6 +5213,17 @@ def test_get_type_hints_typeddict(self):
             'a': Annotated[Required[int], "a", "b", "c"]
         })
 
+        self.assertEqual(get_type_hints(ChildTotalMovie), {"title": str, "year": int})
+        self.assertEqual(get_type_hints(ChildTotalMovie, include_extras=True), {
+            "title": Required[str], "year": NotRequired[int]
+        })
+
+        self.assertEqual(get_type_hints(ChildDeeplyAnnotatedMovie), {"title": str, "year": int})
+        self.assertEqual(get_type_hints(ChildDeeplyAnnotatedMovie, include_extras=True), {
+            "title": Annotated[Required[str], "foobar", "another level"],
+            "year": NotRequired[Annotated[int, 2000]]
+        })
+
     def test_get_type_hints_collections_abc_callable(self):
         # https://github.com/python/cpython/issues/91621
         P = ParamSpec('P')
@@ -6340,6 +6363,16 @@ def test_required_notrequired_keys(self):
         self.assertEqual(WeirdlyQuotedMovie.__optional_keys__,
                          frozenset({"year"}))
 
+        self.assertEqual(ChildTotalMovie.__required_keys__,
+                         frozenset({"title"}))
+        self.assertEqual(ChildTotalMovie.__optional_keys__,
+                         frozenset({"year"}))
+
+        self.assertEqual(ChildDeeplyAnnotatedMovie.__required_keys__,
+                         frozenset({"title"}))
+        self.assertEqual(ChildDeeplyAnnotatedMovie.__optional_keys__,
+                         frozenset({"year"}))
+
     def test_multiple_inheritance(self):
         class One(TypedDict):
             one: int
diff --git a/Misc/ACKS b/Misc/ACKS
index dc47e894b764..f674332751c3 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1403,6 +1403,7 @@ Jean-François Piéronne
 Oleg Plakhotnyuk
 Anatoliy Platonov
 Marcel Plch
+Kirill Podoprigora
 Remi Pointel
 Jon Poler
 Ariel Poliak



More information about the Python-checkins mailing list