Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-46066: Deprecate kwargs syntax for TypedDict definitions #31126

Merged
merged 11 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
15 changes: 13 additions & 2 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1389,10 +1389,18 @@ These are not used in annotations. They are building blocks for declaring types.
``Point2D.__optional_keys__``.
To allow using this feature with older versions of Python that do not
support :pep:`526`, ``TypedDict`` supports two additional equivalent
syntactic forms::
syntactic forms. Firstly, using a literal :class:`dict` as the
second argument::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest just writing "a TypedDict may be created using a functional form". This parallels https://docs.python.org/3.10/library/enum.html#functional-api

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!


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

Secondly, using keyword arguments::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Secondly, using keyword arguments::
Keyword arguments may also be used::


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

.. deprecated-removed:: 3.11 3.13
97littleleaf11 marked this conversation as resolved.
Show resolved Hide resolved
The keyword-argument syntax is deprecated in 3.11 and will be removed
in 3.13. It may also be unsupported by third-party type-checking tools.
97littleleaf11 marked this conversation as resolved.
Show resolved Hide resolved

By default, all keys must be present in a ``TypedDict``. It is possible to
override this by specifying totality.
Expand All @@ -1402,6 +1410,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
9 changes: 5 additions & 4 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4300,7 +4300,8 @@ def test_basics_functional_syntax(self):
self.assertEqual(Emp.__total__, True)

def test_basics_keywords_syntax(self):
Emp = TypedDict('Emp', name=str, id=int)
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)
Expand All @@ -4315,7 +4316,7 @@ def test_basics_keywords_syntax(self):
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})
97littleleaf11 marked this conversation as resolved.
Show resolved Hide resolved
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
15 changes: 11 additions & 4 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 @@ -2417,14 +2416,22 @@ class point2D(TypedDict, total=False):
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 two other
syntax forms work for Python 2.7 and 3.2+
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:
97littleleaf11 marked this conversation as resolved.
Show resolved Hide resolved
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()
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1976,6 +1976,7 @@ Masayuki Yamamoto
Ka-Ping Yee
Chi Hsuan Yen
Jason Yeo
Jingchen Ye
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels absurd to bring this up (sorry!!), but: I think "Jingchen Ye" should go before "Ka-Ping Yee", if we're keeping this list alphabetised.

EungJun Yi
Bob Yodlowski
Danny Yoo
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Deprecate kwargs-based syntax for :class:`typing.TypedDict` definitions.
It was unsupported by type checkers. Patch by Jingchen Ye.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
It was unsupported by type checkers. Patch by Jingchen Ye.
Patch by Jingchen Ye.

Pyright supports it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's good to give a short summary of why this change is being made. Maybe It had confusing semantics, and was largely unused.?