]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-109543: Remove unnecessary hasattr check (#109544)
authorJelle Zijlstra <jelle.zijlstra@gmail.com>
Wed, 20 Sep 2023 03:15:52 +0000 (20:15 -0700)
committerGitHub <noreply@github.com>
Wed, 20 Sep 2023 03:15:52 +0000 (20:15 -0700)
Also added a new test case covering the scenario I thought this
might be about.

Lib/test/test_typing.py
Lib/typing.py
Misc/NEWS.d/next/Library/2023-09-18-07-43-22.gh-issue-109543.1tOGoV.rst [new file with mode: 0644]

index 69f5ff913c57bb9b6bc12ae620d475be03066408..4d1c0f2c724b86e39738125a777514264b64199d 100644 (file)
@@ -7586,6 +7586,17 @@ class TypedDictTests(BaseTestCase):
         self.assertEqual(Options.__required_keys__, frozenset())
         self.assertEqual(Options.__optional_keys__, {'log_level', 'log_path'})
 
+    def test_total_inherits_non_total(self):
+        class TD1(TypedDict, total=False):
+            a: int
+
+        self.assertIs(TD1.__total__, False)
+
+        class TD2(TD1):
+            b: str
+
+        self.assertIs(TD2.__total__, True)
+
     def test_optional_keys(self):
         class Point2Dor3D(Point2D, total=False):
             z: int
index 8655b756a9fd13e288c4744fc6878cd3e945f777..183d5b29a233622f018b425d9ce8ff802c76a1c5 100644 (file)
@@ -2886,8 +2886,7 @@ class _TypedDictMeta(type):
         tp_dict.__annotations__ = annotations
         tp_dict.__required_keys__ = frozenset(required_keys)
         tp_dict.__optional_keys__ = frozenset(optional_keys)
-        if not hasattr(tp_dict, '__total__'):
-            tp_dict.__total__ = total
+        tp_dict.__total__ = total
         return tp_dict
 
     __call__ = dict  # static method
diff --git a/Misc/NEWS.d/next/Library/2023-09-18-07-43-22.gh-issue-109543.1tOGoV.rst b/Misc/NEWS.d/next/Library/2023-09-18-07-43-22.gh-issue-109543.1tOGoV.rst
new file mode 100644 (file)
index 0000000..e790f77
--- /dev/null
@@ -0,0 +1,2 @@
+Remove unnecessary :func:`hasattr` check during :data:`typing.TypedDict`
+creation.