8000 gh-104786: Remove kwargs-based TypedDict creation (#104891) · python/cpython@fea8632 · GitHub
[go: up one dir, main page]

Skip to content

Commit fea8632

Browse files
tomasr8AlexWaygood
andauthored
gh-104786: Remove kwargs-based TypedDict creation (#104891)
Deprecated since Python 3.11. Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent d086792 commit fea8632

File tree

5 files changed

+17
-63
lines changed

5 files changed

+17
-63
lines changed

Doc/library/typing.rst

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,25 +1780,14 @@ These are not used in annotations. They are building blocks for declaring types.
17801780

17811781
assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
17821782

1783-
To allow using this feature with older versions of Python that do not
1784-
support :pep:`526`, ``TypedDict`` supports two additional equivalent
1785-
syntactic forms:
1786-
1787-
* Using a literal :class:`dict` as the second argument::
1783+
An alternative way to create a ``TypedDict`` is by using
1784+
function-call syntax. The second argument must be a literal :class:`dict`::
17881785

17891786
Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
17901787

1791-
* Using keyword arguments::
1792-
1793-
Point2D = TypedDict('Point2D', x=int, y=int, label=str)
1794-
1795-
.. deprecated-removed:: 3.11 3.13
1796-
The keyword-argument syntax is deprecated in 3.11 and will be removed
1797-
in 3.13. It may also be unsupported by static type checkers.
1798-
1799-
The functional syntax should also be used when any of the keys are not valid
1800-
:ref:`identifiers <identifiers>`, for example because they are keywords or contain hyphens.
1801-
Example::
1788+
This functional syntax allows defining keys which are not valid
1789+
:ref:`identifiers <identifiers>`, for example because they are
1790+
keywords or contain hyphens::
18021791

18031792
# raises SyntaxError
18041793
class Point2D(TypedDict):
@@ -1955,6 +1944,9 @@ These are not used in annotations. They are building blocks for declaring types.
19551944
.. versionchanged:: 3.11
19561945
Added support for generic ``TypedDict``\ s.
19571946

1947+
.. versionchanged:: 3.13
1948+
Removed support for the keyword-argument method of creating ``TypedDict``\ s.
1949+
19581950
Generic concrete collections
19591951
----------------------------
19601952

Doc/whatsnew/3.13.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ Removed
230230
* :pep:`594`: Remove the :mod:`!chunk` module, deprecated in Python 3.11.
231231
(Contributed by Victor Stinner in :gh:`104773`.)
232232

233+
* Remove support for the keyword-argument method of creating
234+
:class:`typing.TypedDict` types, deprecated in Python 3.11.
235+
(Contributed by Tomas Roun in :gh:`104786`.)
233236

234237
Porting to Python 3.13
235238
======================

Lib/test/test_typing.py

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7018,35 +7018,6 @@ def test_basics_functional_syntax(self):
70187018
self.assertEqual(Emp.__annotations__, {'name': str, 'id': int})
70197019
self.assertEqual(Emp.__total__, True)
70207020

7021-
def test_basics_keywords_syntax(self):
7022-
with self.assertWarns(DeprecationWarning):
7023-
Emp = TypedDict('Emp', name=str, id=int)
7024-
self.assertIsSubclass(Emp, dict)
7025-
self.assertIsSubclass(Emp, typing.MutableMapping)
7026-
self.assertNotIsSubclass(Emp, collections.abc.Sequence)
7027-
jim = Emp(name='Jim', id=1)
7028-
self.assertIs(type(jim), dict)
7029-
self.assertEqual(jim['name'], 'Jim')
7030-
self.assertEqual(jim['id'], 1)
7031-
self.assertEqual(Emp.__name__, 'Emp')
7032-
self.assertEqual(Emp.__module__, __name__)
7033-
self.assertEqual(Emp.__bases__, (dict,))
7034-
self.assertEqual(Emp.__annotations__, {'name': str, 'id': int})
7035-
self.assertEqual(Emp.__total__, True)
7036-
7037-
def test_typeddict_special_keyword_names(self):
7038-
with self.assertWarns(DeprecationWarning):
7039-
TD = TypedDict("TD", cls=type, self=object, typename=str, _typename=int, fields=list, _fields=dict)
7040-
self.assertEqual(TD.__name__, 'TD')
7041-
self.assertEqual(TD.__annotations__, {'cls': type, 'self': object, 'typename': str, '_typename': int, 'fields': list, '_fields': dict})
7042-
a = TD(cls=str, self=42, typename='foo', _typename=53, fields=[('bar', tuple)], _fields={'baz', set})
7043-
self.assertEqual(a['cls'], str)
7044-
self.assertEqual(a['self'], 42)
7045-
self.assertEqual(a['typename'], 'foo')
7046-
self.assertEqual(a['_typename'], 53)
7047-
self.assertEqual(a['fields'], [('bar', tuple)])
7048-
self.assertEqual(a['_fields'], {'baz', set})
7049-
70507021
def test_typeddict_create_errors(self):
70517022
with self.assertRaises(TypeError):
70527023
TypedDict.__new__()
@@ -7055,7 +7026,9 @@ def test_typeddict_create_errors(self):
70557026
with self.assertRaises(TypeError):
70567027
TypedDict('Emp', [('name', str)], None)
70577028
with self.assertRaises(TypeError):
7058-
TypedDict(_typename='Emp', name=str, id=int)
7029+
TypedDict(_typename='Emp')
7030+
with self.assertRaises(TypeError):
7031+
TypedDict('Emp', name=str, id=int)
70597032

70607033
def test_typeddict_errors(self):
70617034
Emp = TypedDict('Emp', {'name': str, 'id': int})

Lib/typing.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import re as stdlib_re # Avoid confusion with the typing.re namespace on <=3.11
3030
import sys
3131
import types
32-
import warnings
3332
from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias
3433

3534
from _typing import (
@@ -2822,7 +2821,7 @@ def __subclasscheck__(cls, other):
28222821
__instancecheck__ = __subclasscheck__
28232822

28242823

2825-
def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
2824+
def TypedDict(typename, fields=None, /, *, total=True):
28262825
"""A simple typed namespace. At runtime it is equivalent to a plain dict.
28272826
28282827
TypedDict creates a dictionary type that expects all of its
@@ -2859,23 +2858,9 @@ class point2D(TypedDict, total=False):
28592858
checker is only expected to support a literal False or True as the value of
28602859
the total argument. True is the default, and makes all items defined in the
28612860
class body be required.
2862-
2863-
The class syntax is only supported in Python 3.6+, while the other
2864-
syntax form works for Python 2.7 and 3.2+
28652861
"""
28662862
if fields is None:
2867-
fields = kwargs
2868-
elif kwargs:
2869-
raise TypeError("TypedDict takes either a dict or keyword arguments,"
2870-
" but not both")
2871-
if kwargs:
2872-
warnings.warn(
2873-
"The kwargs-based syntax for TypedDict definitions is deprecated "
2874-
"in Python 3.11, will be removed in Python 3.13, and may not be "
2875-
"understood by third-party type checkers.",
2876-
DeprecationWarning,
2877-
stacklevel=2,
2878-
)
2863+
fields = {}
28792864

28802865
ns = {'__annotations__': dict(fields)}
28812866
module = _caller()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove kwargs-based :class:`typing.TypedDict` creation

0 commit comments

Comments
 (0)
0