Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de6043e

Browse files
bpo-46066: Deprecate kwargs syntax for TypedDict definitions (GH-31126)
Closes python/typing#981 https://bugs.python.org/issue46066
1 parent 6f1efd1 commit de6043e

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

Doc/library/typing.rst

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,11 +1470,20 @@ These are not used in annotations. They are building blocks for declaring types.
14701470
``Point2D.__optional_keys__``.
14711471
To allow using this feature with older versions of Python that do not
14721472
support :pep:`526`, ``TypedDict`` supports two additional equivalent
1473-
syntactic forms::
1473+
syntactic forms:
1474+
1475+
* Using a literal :class:`dict` as the second argument::
14741476

1475-
Point2D = TypedDict('Point2D', x=int, y=int, label=str)
14761477
Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
14771478

1479+
* Using keyword arguments::
1480+
1481+
Point2D = TypedDict('Point2D', x=int, y=int, label=str)
1482+
1483+
.. deprecated-removed:: 3.11 3.13
1484+
The keyword-argument syntax is deprecated in 3.11 and will be removed
1485+
in 3.13. It may also be unsupported by static type checkers.
1486+
14781487
By default, all keys must be present in a ``TypedDict``. It is possible to
14791488
override this by specifying totality.
14801489
Usage::
@@ -1483,6 +1492,9 @@ These are not used in annotations. They are building blocks for declaring types.
14831492
x: int
14841493
y: int
14851494

1495+
# Alternative syntax
1496+
Point2D = TypedDict('Point2D', {'x': int, 'y': int}, total=False)
1497+
14861498
This means that a ``Point2D`` ``TypedDict`` can have any of the keys
14871499
omitted. A type checker is only expected to support a literal ``False`` or
14881500
``True`` as the value of the ``total`` argument. ``True`` is the default,

Lib/test/test_typing.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4380,7 +4380,8 @@ def test_basics_functional_syntax(self):
43804380
self.assertEqual(Emp.__total__, True)
43814381

43824382
def test_basics_keywords_syntax(self):
4383-
Emp = TypedDict('Emp', name=str, id=int)
4383+
with self.assertWarns(DeprecationWarning):
4384+
Emp = TypedDict('Emp', name=str, id=int)
43844385
self.assertIsSubclass(Emp, dict)
43854386
self.assertIsSubclass(Emp, typing.MutableMapping)
43864387
self.assertNotIsSubclass(Emp, collections.abc.Sequence)
@@ -4395,7 +4396,8 @@ def test_basics_keywords_syntax(self):
43954396
self.assertEqual(Emp.__total__, True)
43964397

43974398
def test_typeddict_special_keyword_names(self):
4398-
TD = TypedDict("TD", cls=type, self=object, typename=str, _typename=int, fields=list, _fields=dict)
4399+
with self.assertWarns(DeprecationWarning):
4400+
TD = TypedDict("TD", cls=type, self=object, typename=str, _typename=int, fields=list, _fields=dict)
43994401
self.assertEqual(TD.__name__, 'TD')
44004402
self.assertEqual(TD.__annotations__, {'cls': type, 'self': object, 'typename': str, '_typename': int, 'fields': list, '_fields': dict})
44014403
a = TD(cls=str, self=42, typename='foo', _typename=53, fields=[('bar', tuple)], _fields={'baz', set})
@@ -4451,7 +4453,7 @@ def test_py36_class_syntax_usage(self):
44514453

44524454
def test_pickle(self):
44534455
global EmpD # pickle wants to reference the class by name
4454-
EmpD = TypedDict('EmpD', name=str, id=int)
4456+
EmpD = TypedDict('EmpD', {'name': str, 'id': int})
44554457
jane = EmpD({'name': 'jane', 'id': 37})
44564458
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
44574459
z = pickle.dumps(jane, proto)
@@ -4463,7 +4465,7 @@ def test_pickle(self):
44634465
self.assertEqual(EmpDnew({'name': 'jane', 'id': 37}), jane)
44644466

44654467
def test_optional(self):
4466-
EmpD = TypedDict('EmpD', name=str, id=int)
4468+
EmpD = TypedDict('EmpD', {'name': str, 'id': int})
44674469

44684470
self.assertEqual(typing.Optional[EmpD], typing.Union[None, EmpD])
44694471
self.assertNotEqual(typing.List[EmpD], typing.Tuple[EmpD])

Lib/typing.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2569,9 +2569,8 @@ class Point2D(TypedDict):
25692569
25702570
The type info can be accessed via the Point2D.__annotations__ dict, and
25712571
the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
2572-
TypedDict supports two additional equivalent forms::
2572+
TypedDict supports an additional equivalent form::
25732573
2574-
Point2D = TypedDict('Point2D', x=int, y=int, label=str)
25752574
Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
25762575
25772576
By default, all keys must be present in a TypedDict. It is possible
@@ -2587,14 +2586,22 @@ class point2D(TypedDict, total=False):
25872586
the total argument. True is the default, and makes all items defined in the
25882587
class body be required.
25892588
2590-
The class syntax is only supported in Python 3.6+, while two other
2591-
syntax forms work for Python 2.7 and 3.2+
2589+
The class syntax is only supported in Python 3.6+, while the other
2590+
syntax form works for Python 2.7 and 3.2+
25922591
"""
25932592
if fields is None:
25942593
fields = kwargs
25952594
elif kwargs:
25962595
raise TypeError("TypedDict takes either a dict or keyword arguments,"
25972596
" but not both")
2597+
if kwargs:
2598+
warnings.warn(
2599+
"The kwargs-based syntax for TypedDict definitions is deprecated "
2600+
"in Python 3.11, will be removed in Python 3.13, and may not be "
2601+
"understood by third-party type checkers.",
2602+
DeprecationWarning,
2603+
stacklevel=2,
2604+
)
25982605

25992606
ns = {'__annotations__': dict(fields)}
26002607
module = _caller()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,6 +1974,7 @@ Arnon Yaari
19741974
Alakshendra Yadav
19751975
Hirokazu Yamamoto
19761976
Masayuki Yamamoto
1977+
Jingchen Ye
19771978
Ka-Ping Yee
19781979
Chi Hsuan Yen
19791980
Jason Yeo
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Deprecate kwargs-based syntax for :class:`typing.TypedDict` definitions.
2+
It had confusing semantics when specifying totality, and was largely unused.
3+
Patch by Jingchen Ye.

0 commit comments

Comments
 (0)