Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1391,8 +1391,12 @@ These are not used in annotations. They are building blocks for declaring types.
support :pep:`526`, ``TypedDict`` supports two additional equivalent
syntactic forms::

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

.. deprecated-removed:: 3.11 3.13
The keyword arguments syntax is deprecated in 3.11 and will be removed
in 3.13. Currently it might not be supported by typecheckers like ``mypy``.

By default, all keys must be present in a ``TypedDict``. It is possible to
override this by specifying totality.
Expand All @@ -1402,6 +1406,9 @@ These are not used in annotations. They are building blocks for declaring types.
x: int
y: int

# Alternative syntax
Point2D = TypedDict('Point2D', {'x': int, 'y': int}, total=False)

This means that a ``Point2D`` ``TypedDict`` can have any of the keys
omitted. A type checker is only expected to support a literal ``False`` or
``True`` as the value of the ``total`` argument. ``True`` is the default,
Expand Down
33 changes: 17 additions & 16 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4300,22 +4300,23 @@ def test_basics_functional_syntax(self):
self.assertEqual(Emp.__total__, True)

def test_basics_keywords_syntax(self):
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)
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):
TD = TypedDict("TD", cls=type, self=object, typename=str, _typename=int, fields=list, _fields=dict)
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})
Expand Down Expand Up @@ -4371,7 +4372,7 @@ def test_py36_class_syntax_usage(self):

def test_pickle(self):
global EmpD # pickle wants to reference the class by name
EmpD = TypedDict('EmpD', name=str, id=int)
EmpD = TypedDict('EmpD', {'name': str, 'id': int})
jane = EmpD({'name': 'jane', 'id': 37})
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
z = pickle.dumps(jane, proto)
Expand All @@ -4383,7 +4384,7 @@ def test_pickle(self):
self.assertEqual(EmpDnew({'name': 'jane', 'id': 37}), jane)

def test_optional(self):
EmpD = TypedDict('EmpD', name=str, id=int)
EmpD = TypedDict('EmpD', {'name': str, 'id': int})

self.assertEqual(typing.Optional[EmpD], typing.Union[None, EmpD])
self.assertNotEqual(typing.List[EmpD], typing.Tuple[EmpD])
Expand Down
10 changes: 8 additions & 2 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2399,9 +2399,8 @@ class Point2D(TypedDict):

The type info can be accessed via the Point2D.__annotations__ dict, and
the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
TypedDict supports two additional equivalent forms::
TypedDict supports an additional equivalent form::

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

By default, all keys must be present in a TypedDict. It is possible
Expand All @@ -2425,6 +2424,13 @@ class body be required.
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 definition is deprecated "
" in Python 3.11 and will be removed in Python 3.13.",
DeprecationWarning,
stacklevel=2,
)

ns = {'__annotations__': dict(fields)}
module = _caller()
Expand Down