From 6c05b6ccee08f3c7e743c1f7fb13bc10f9e1e3f3 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Thu, 25 May 2023 18:27:34 +0300 Subject: [PATCH] Remove deprecated keyword argument syntax for constructing typing.TypedDict types --- Doc/library/typing.rst | 4 --- Doc/whatsnew/3.13.rst | 4 +++ Lib/test/test_typing.py | 29 ------------------- Lib/typing.py | 17 +---------- ...-05-25-18-27-04.gh-issue-104936.WOs0Cl.rst | 2 ++ 5 files changed, 7 insertions(+), 49 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-05-25-18-27-04.gh-issue-104936.WOs0Cl.rst diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index df16c31c43f53e..ff6bb5429fc114 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -1792,10 +1792,6 @@ These are not used in annotations. They are building blocks for declaring types. Point2D = TypedDict('Point2D', x=int, y=int, label=str) - .. deprecated-removed:: 3.11 3.13 - The keyword-argument syntax is deprecated in 3.11 and will be removed - in 3.13. It may also be unsupported by static type checkers. - The functional syntax should also be used when any of the keys are not valid :ref:`identifiers `, for example because they are keywords or contain hyphens. Example:: diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index f570e0354daf6b..64a108ad3c5cad 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -132,6 +132,10 @@ Removed (Contributed by Hugo van Kemenade in :gh:`104835`.) +* Remove the keyword argument syntax for constructing :data:`typing.TypedDict` + types, deprecated in Python 3.11. + (Contributed by Hugo van Kemenade in :gh:`104936`.) + * :pep:`594`: Remove the :mod:`!cgi`` and :mod:`!cgitb` modules, deprecated in Python 3.11. diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 46c8e7452004ed..7c62cc48e27f90 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -6976,35 +6976,6 @@ def test_basics_functional_syntax(self): self.assertEqual(Emp.__annotations__, {'name': str, 'id': int}) self.assertEqual(Emp.__total__, True) - def test_basics_keywords_syntax(self): - with self.assertWarns(DeprecationWarning): - Emp = TypedDict('Emp', name=str, id=int) - self.assertIsSubclass(Emp, dict) - self.assertIsSubclass(Emp, typing.MutableMapping) - self.assertNotIsSubclass(Emp, collections.abc.Sequence) - jim = Emp(name='Jim', id=1) - self.assertIs(type(jim), dict) - self.assertEqual(jim['name'], 'Jim') - self.assertEqual(jim['id'], 1) - self.assertEqual(Emp.__name__, 'Emp') - self.assertEqual(Emp.__module__, __name__) - self.assertEqual(Emp.__bases__, (dict,)) - self.assertEqual(Emp.__annotations__, {'name': str, 'id': int}) - self.assertEqual(Emp.__total__, True) - - def test_typeddict_special_keyword_names(self): - with self.assertWarns(DeprecationWarning): - TD = TypedDict("TD", cls=type, self=object, typename=str, _typename=int, fields=list, _fields=dict) - self.assertEqual(TD.__name__, 'TD') - self.assertEqual(TD.__annotations__, {'cls': type, 'self': object, 'typename': str, '_typename': int, 'fields': list, '_fields': dict}) - a = TD(cls=str, self=42, typename='foo', _typename=53, fields=[('bar', tuple)], _fields={'baz', set}) - self.assertEqual(a['cls'], str) - self.assertEqual(a['self'], 42) - self.assertEqual(a['typename'], 'foo') - self.assertEqual(a['_typename'], 53) - self.assertEqual(a['fields'], [('bar', tuple)]) - self.assertEqual(a['_fields'], {'baz', set}) - def test_typeddict_create_errors(self): with self.assertRaises(TypeError): TypedDict.__new__() diff --git a/Lib/typing.py b/Lib/typing.py index b32ff0c6ba4e25..fff9567b063cbc 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -29,7 +29,6 @@ import re as stdlib_re # Avoid confusion with the typing.re namespace on <=3.11 import sys import types -import warnings from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias from _typing import ( @@ -2822,7 +2821,7 @@ def __subclasscheck__(cls, other): __instancecheck__ = __subclasscheck__ -def TypedDict(typename, fields=None, /, *, total=True, **kwargs): +def TypedDict(typename, fields=None, /, *, total=True): """A simple typed namespace. At runtime it is equivalent to a plain dict. TypedDict creates a dictionary type that expects all of its @@ -2863,20 +2862,6 @@ class body be required. The class syntax is only supported in Python 3.6+, while the other syntax form works for Python 2.7 and 3.2+ """ - if fields is None: - fields = kwargs - elif kwargs: - raise TypeError("TypedDict takes either a dict or keyword arguments," - " but not both") - if kwargs: - warnings.warn( - "The kwargs-based syntax for TypedDict definitions is deprecated " - "in Python 3.11, will be removed in Python 3.13, and may not be " - "understood by third-party type checkers.", - DeprecationWarning, - stacklevel=2, - ) - ns = {'__annotations__': dict(fields)} module = _caller() if module is not None: diff --git a/Misc/NEWS.d/next/Library/2023-05-25-18-27-04.gh-issue-104936.WOs0Cl.rst b/Misc/NEWS.d/next/Library/2023-05-25-18-27-04.gh-issue-104936.WOs0Cl.rst new file mode 100644 index 00000000000000..d7a532a5503080 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-05-25-18-27-04.gh-issue-104936.WOs0Cl.rst @@ -0,0 +1,2 @@ +Remove the keyword argument syntax for constructing :data:`typing.TypedDict` +types, deprecated in Python 3.11. Patch by Hugo van Kemenade.