Skip to content

Commit dc3f975

Browse files
gh-103629: Update typing.Unpack docs in compliance with PEP 692 (#103894)
1 parent a3a5b4b commit dc3f975

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

Doc/library/typing.rst

+22-2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ annotations. These include:
9898
*Introducing* :data:`LiteralString`
9999
* :pep:`681`: Data Class Transforms
100100
*Introducing* the :func:`@dataclass_transform<dataclass_transform>` decorator
101+
* :pep:`692`: Using ``TypedDict`` for more precise ``**kwargs`` typing
102+
*Introducing* a new way of typing ``**kwargs`` with :data:`Unpack` and
103+
:data:`TypedDict`
101104
* :pep:`698`: Adding an override decorator to typing
102105
*Introducing* the :func:`@override<override>` decorator
103106

@@ -1417,8 +1420,10 @@ These are not used in annotations. They are building blocks for creating generic
14171420
tup: tuple[Unpack[Ts]]
14181421

14191422
In fact, ``Unpack`` can be used interchangeably with ``*`` in the context
1420-
of types. You might see ``Unpack`` being used explicitly in older versions
1421-
of Python, where ``*`` couldn't be used in certain places::
1423+
of :class:`typing.TypeVarTuple <TypeVarTuple>` and
1424+
:class:`builtins.tuple <tuple>` types. You might see ``Unpack`` being used
1425+
explicitly in older versions of Python, where ``*`` couldn't be used in
1426+
certain places::
14221427

14231428
# In older versions of Python, TypeVarTuple and Unpack
14241429
# are located in the `typing_extensions` backports package.
@@ -1428,6 +1433,21 @@ These are not used in annotations. They are building blocks for creating generic
14281433
tup: tuple[*Ts] # Syntax error on Python <= 3.10!
14291434
tup: tuple[Unpack[Ts]] # Semantically equivalent, and backwards-compatible
14301435

1436+
``Unpack`` can also be used along with :class:`typing.TypedDict` for typing
1437+
``**kwargs`` in a function signature::
1438+
1439+
from typing import TypedDict, Unpack
1440+
1441+
class Movie(TypedDict):
1442+
name: str
1443+
year: int
1444+
1445+
# This function expects two keyword arguments - `name` of type `str`
1446+
# and `year` of type `int`.
1447+
def foo(**kwargs: Unpack[Movie]): ...
1448+
1449+
See :pep:`692` for more details on using ``Unpack`` for ``**kwargs`` typing.
1450+
14311451
.. versionadded:: 3.11
14321452

14331453
.. class:: ParamSpec(name, *, bound=None, covariant=False, contravariant=False)

Doc/whatsnew/3.12.rst

+34
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ Summary -- Release highlights
6666
6767
.. PEP-sized items next.
6868
69+
New typing features:
70+
71+
* :ref:`whatsnew312-pep692`
72+
6973
Important deprecations, removals or restrictions:
7074

7175
* :pep:`623`, Remove wstr from Unicode
@@ -145,6 +149,36 @@ New Features
145149
In Python 3.14, the default will switch to ``'data'``.
146150
(Contributed by Petr Viktorin in :pep:`706`.)
147151

152+
New Features Related to Type Hints
153+
==================================
154+
155+
This section covers major changes affecting :pep:`484` type hints and
156+
the :mod:`typing` module.
157+
158+
.. _whatsnew312-pep692:
159+
160+
PEP 692: Using ``TypedDict`` for more precise ``**kwargs`` typing
161+
-----------------------------------------------------------------
162+
163+
Typing ``**kwargs`` in a function signature as introduced by :pep:`484` allowed
164+
for valid annotations only in cases where all of the ``**kwargs`` were of the
165+
same type.
166+
167+
This PEP specifies a more precise way of typing ``**kwargs`` by relying on
168+
typed dictionaries::
169+
170+
from typing import TypedDict, Unpack
171+
172+
class Movie(TypedDict):
173+
name: str
174+
year: int
175+
176+
def foo(**kwargs: Unpack[Movie]): ...
177+
178+
See :pep:`692` for more details.
179+
180+
(PEP written by Franek Magiera)
181+
148182

149183
Other Language Changes
150184
======================
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Mention the new way of typing ``**kwargs`` with ``Unpack`` and ``TypedDict``
2+
introduced in :pep:`692`.

0 commit comments

Comments
 (0)