8000 gh-103629: Update typing.Unpack docs in compliance with PEP 692 by franekmagiera · Pull Request #103894 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-103629: Update typing.Unpack docs in compliance with PEP 692 #103894

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

Merged
Changes from 1 commit
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
Next Next commit
GH-103629: Update typing.Unpack docs in compliance with PEP 692
  • Loading branch information
franekmagiera committed Apr 26, 2023
commit 9529f871de63c265e4b194c0f38662ed010c2c9e
24 changes: 22 additions & 2 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ annotations. These include:
*Introducing* :data:`LiteralString`
* :pep:`681`: Data Class Transforms
*Introducing* the :func:`@dataclass_transform<dataclass_transform>` decorator
* :pep:`692`: Using ``TypedDict`` for more precise ``**kwargs`` typing
*Introducing* a new way of typing ``**kwargs`` with :data:`Unpack` and
:data:`TypedDict`
* :pep:`698`: Adding an override decorator to typing
*Introducing* the :func:`@override<override>` decorator

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

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

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

``Unpack`` can also be used along with :class:`typing.TypedDict` for typing
``**kwargs`` in a function signature::

from typing import TypedDict, Unpack

class Movie(TypedDict):

Choose a reason for hiding this comment

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

4 space tab would've been more consistent.

Copy link
Member

Choose a reason for hiding this comment

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

3 spaces are the convention in the docs.

name: str
year: int

# This function expects two keyword arguments - `name` of type `str`
# and `year` of type `int`.
def foo(**kwargs: Unpack[Movie]): ...

See :pep:`692` for more details on using ``Unpack`` for ``**kwargs`` typing.

.. versionadded:: 3.11

.. class:: ParamSpec(name, *, bound=None, covariant=False, contravariant=False)
Expand Down
0