[Python-checkins] bpo-42059: Fix required/optional keys for TypedDict(..., total=False) (GH-22736) (GH-23747)

brandtbucher webhook-mailer at python.org
Mon Dec 14 17:33:38 EST 2020


https://github.com/python/cpython/commit/dbb00062dc3afb12c41c87564e6faefe60766b01
commit: dbb00062dc3afb12c41c87564e6faefe60766b01
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: brandtbucher <brandtbucher at gmail.com>
date: 2020-12-14T14:33:27-08:00
summary:

bpo-42059: Fix required/optional keys for TypedDict(..., total=False) (GH-22736) (GH-23747)

(cherry picked from commit 67b769f5157c9dad1c7dd6b24e067b9fdab5b35d)

Co-authored-by: Alex Grönholm <alex.gronholm at nextday.fi>

files:
A Misc/NEWS.d/next/Library/2020-10-17-12-42-08.bpo-42059.ZGMZ3D.rst
M Lib/test/test_typing.py
M Lib/typing.py

diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 04dd6df6a9cc4..3b3aa29de7221 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -3891,10 +3891,14 @@ def test_total(self):
         self.assertEqual(D(), {})
         self.assertEqual(D(x=1), {'x': 1})
         self.assertEqual(D.__total__, False)
+        self.assertEqual(D.__required_keys__, frozenset())
+        self.assertEqual(D.__optional_keys__, {'x'})
 
         self.assertEqual(Options(), {})
         self.assertEqual(Options(log_level=2), {'log_level': 2})
         self.assertEqual(Options.__total__, False)
+        self.assertEqual(Options.__required_keys__, frozenset())
+        self.assertEqual(Options.__optional_keys__, {'log_level', 'log_path'})
 
     def test_optional_keys(self):
         class Point2Dor3D(Point2D, total=False):
diff --git a/Lib/typing.py b/Lib/typing.py
index 1d6584db5afb2..81e4a2fa403b9 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1987,14 +1987,14 @@ class body be required.
         raise TypeError("TypedDict takes either a dict or keyword arguments,"
                         " but not both")
 
-    ns = {'__annotations__': dict(fields), '__total__': total}
+    ns = {'__annotations__': dict(fields)}
     try:
         # Setting correct module is necessary to make typed dict classes pickleable.
         ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
     except (AttributeError, ValueError):
         pass
 
-    return _TypedDictMeta(typename, (), ns)
+    return _TypedDictMeta(typename, (), ns, total=total)
 
 _TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
 TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
diff --git a/Misc/NEWS.d/next/Library/2020-10-17-12-42-08.bpo-42059.ZGMZ3D.rst b/Misc/NEWS.d/next/Library/2020-10-17-12-42-08.bpo-42059.ZGMZ3D.rst
new file mode 100644
index 0000000000000..3f18824fe6598
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-10-17-12-42-08.bpo-42059.ZGMZ3D.rst
@@ -0,0 +1 @@
+:class:`typing.TypedDict` types created using the alternative call-style syntax now correctly respect the ``total`` keyword argument when setting their ``__required_keys__`` and ``__optional_keys__`` class attributes.



More information about the Python-checkins mailing list