From bfbbc29e112e4bcd24040b075fd7fbb148257b02 Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Wed, 24 May 2023 20:03:58 +0200 Subject: [PATCH 1/9] Remove kwargs-based TypedDict creation --- Lib/test/test_typing.py | 33 +++------------------------------ Lib/typing.py | 15 ++------------- 2 files changed, 5 insertions(+), 43 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 098933b7cb434f..fcbbc62cd7cd8c 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -6951,35 +6951,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__() @@ -6988,7 +6959,9 @@ def test_typeddict_create_errors(self): with self.assertRaises(TypeError): TypedDict('Emp', [('name', str)], None) with self.assertRaises(TypeError): - TypedDict(_typename='Emp', name=str, id=int) + TypedDict(_typename='Emp') + with self.assertRaises(TypeError): + TypedDict('Emp', name=str, id=int) def test_typeddict_errors(self): Emp = TypedDict('Emp', {'name': str, 'id': int}) diff --git a/Lib/typing.py b/Lib/typing.py index b32ff0c6ba4e25..7d932ed9d42a7e 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -2822,7 +2822,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 @@ -2864,18 +2864,7 @@ class body be required. 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, - ) + fields = {} ns = {'__annotations__': dict(fields)} module = _caller() From bbbbfa94e2f2da5300ecf04e8c290fabe731df93 Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Wed, 24 May 2023 20:25:31 +0200 Subject: [PATCH 2/9] Add news entry --- .../next/Library/2023-05-24-20-21-27.gh-issue-104786.SmgT5_.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-05-24-20-21-27.gh-issue-104786.SmgT5_.rst diff --git a/Misc/NEWS.d/next/Library/2023-05-24-20-21-27.gh-issue-104786.SmgT5_.rst b/Misc/NEWS.d/next/Library/2023-05-24-20-21-27.gh-issue-104786.SmgT5_.rst new file mode 100644 index 00000000000000..aac669434ec266 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-05-24-20-21-27.gh-issue-104786.SmgT5_.rst @@ -0,0 +1 @@ +Remove kwargs-based :class:`typing.TypedDict` creation From 31dde58296df6b68f07fc80ba50aa94ae151d011 Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Wed, 24 May 2023 21:47:05 +0200 Subject: [PATCH 3/9] Update docs --- Doc/library/typing.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 92943c46ef5132..4fd7d9c479ae48 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -1780,9 +1780,8 @@ 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. + .. versionchanged:: 3.13 + Removed support for the keyword-argument method of creating ``TypedDict``. 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. From 6d1fa5e769686308b987c7ef899339f2916e1a2c Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Wed, 24 May 2023 21:48:13 +0200 Subject: [PATCH 4/9] Update whatsnew --- Doc/whatsnew/3.13.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 1102225e50b658..12da1c7199cd8d 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -168,6 +168,9 @@ Removed The :mod:`mimetypes` module provides an alternative. (Contributed by Victor Stinner in :gh:`104773`.) +* Remove support for the keyword-argument method of creating + :class:`typing.TypedDict`, deprecated in Python 3.11. + (Contributed by Tomas Roun in :gh:`104786`.) Porting to Python 3.13 ====================== From 6d2ae81f387b7ecc94ae95b2202ca62b0c52d674 Mon Sep 17 00:00:00 2001 From: Tomas R Date: Wed, 24 May 2023 21:54:07 +0200 Subject: [PATCH 5/9] Update Doc/whatsnew/3.13.rst Co-authored-by: Alex Waygood --- Doc/whatsnew/3.13.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 12da1c7199cd8d..c1be1a29e3f007 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -169,7 +169,7 @@ Removed (Contributed by Victor Stinner in :gh:`104773`.) * Remove support for the keyword-argument method of creating - :class:`typing.TypedDict`, deprecated in Python 3.11. + :class:`typing.TypedDict` types, deprecated in Python 3.11. (Contributed by Tomas Roun in :gh:`104786`.) Porting to Python 3.13 From b8f07d07504627583c32768742fef67f649632f2 Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Wed, 24 May 2023 22:03:10 +0200 Subject: [PATCH 6/9] Update docs --- Doc/library/typing.rst | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 4fd7d9c479ae48..2124d782930838 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -1769,20 +1769,11 @@ These are not used in annotations. They are building blocks for declaring types. assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first') To allow using this feature with older versions of Python that do not - support :pep:`526`, ``TypedDict`` supports two additional equivalent - syntactic forms: - - * Using a literal :class:`dict` as the second argument:: + support :pep:`526`, ``TypedDict`` supports an additional syntactic + form using a literal :class:`dict` as the second argument:: Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str}) - * Using keyword arguments:: - - Point2D = TypedDict('Point2D', x=int, y=int, label=str) - - .. versionchanged:: 3.13 - Removed support for the keyword-argument method of creating ``TypedDict``. - 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:: @@ -1942,6 +1933,9 @@ These are not used in annotations. They are building blocks for declaring types. .. versionchanged:: 3.11 Added support for generic ``TypedDict``\ s. + .. versionchanged:: 3.13 + Removed support for the keyword-argument method of creating ``TypedDict``\ s. + Generic concrete collections ---------------------------- From ab2cdda116bf2c381b76bfd664541595a37a0b2f Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Thu, 25 May 2023 21:27:15 +0200 Subject: [PATCH 7/9] Remove reference to pep526 --- Doc/library/typing.rst | 10 +++++----- Lib/typing.py | 1 - 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 2124d782930838..809c29adebac9b 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -1768,14 +1768,14 @@ These are not used in annotations. They are building blocks for declaring types. assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first') - To allow using this feature with older versions of Python that do not - support :pep:`526`, ``TypedDict`` supports an additional syntactic - form using a literal :class:`dict` as the second argument:: + There is an alternative way to create a ``TypedDict`` + using a literal :class:`dict` as the second argument:: Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str}) - 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. + This functional syntax allows defining keys which are not valid + :ref:`identifiers `, for example because they are + keywords or contain hyphens. Example:: # raises SyntaxError diff --git a/Lib/typing.py b/Lib/typing.py index 7d932ed9d42a7e..620962cdcbf9a3 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 ( From 7dae0e71a5d16b5cff93181cc74c4925406724f2 Mon Sep 17 00:00:00 2001 From: Tomas R Date: Thu, 25 May 2023 21:37:58 +0200 Subject: [PATCH 8/9] Improve wording Co-authored-by: Alex Waygood --- Doc/library/typing.rst | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 4ead1783e99e0a..eb7ee5a03b6b92 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -1780,15 +1780,14 @@ These are not used in annotations. They are building blocks for declaring types. assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first') - There is an alternative way to create a ``TypedDict`` - using a literal :class:`dict` as the second argument:: + An alternative way to create a ``TypedDict`` is by using + function-call syntax. The second argument must be a literal :class:`dict`:: Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str}) This functional syntax allows defining keys which are not valid :ref:`identifiers `, for example because they are - keywords or contain hyphens. - Example:: + keywords or contain hyphens:: # raises SyntaxError class Point2D(TypedDict): From eb94f7bb6e206a8b57fa9d3beb9afb0f9e9ca707 Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Thu, 25 May 2023 21:40:42 +0200 Subject: [PATCH 9/9] Remove mention of Python 3.6 --- Lib/typing.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/Lib/typing.py b/Lib/typing.py index e05de6dc74a4da..13f0883e3bfcc6 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -2858,9 +2858,6 @@ class point2D(TypedDict, total=False): checker is only expected to support a literal False or True as the value of the total argument. True is the default, and makes all items defined in the 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 = {}