10000 PEP 692: Introduce proposed enhancements and expand Motivation section by franekmagiera · Pull Request #2732 · python/peps · GitHub
[go: up one dir, main page]

Skip to content

PEP 692: Introduce proposed enhancements and expand Motivation section #2732

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
merged 15 commits into from
Aug 23, 2022
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
Introduce proposed PEP 692 enhancements
  • Loading branch information
franekmagiera committed Jul 24, 2022
commit 01c4ead4ea64dac2cba3fe0cfb353d5ce03b43f7
28 changes: 16 additions & 12 deletions pep-0692.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@ PEP: 692
Title: Using TypedDict for more precise \*\*kwargs typing
Author: Franek Magiera <framagie@gmail.com>
Sponsor: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Discussions-To: https://mail.python.org/archives/list/typing-sig@python.org/thread/U42MJE6QZYWPVIFHJIGIT7OE52ZGIQV3/
Discussions-To: https://discuss.python.org/t/pep-692-using-typeddict-for-more-precise-kwargs-typing/17314
Status: Draft
Type: Standards Track
Content-Type: text/x-rst
Created: 29-May-2022
Python-Version: 3.12
Post-History: `29-May-2022 <https://mail.python.org/archives/list/typing-sig@python.org/thread/U42MJE6QZYWPVIFHJIGIT7OE52ZGIQV3/>`__,
Post-History: `12-Jul-2022 <https://discuss.python.org/t/pep-692-using-typeddict-for-more-precise-kwargs-typing/17314>`__,
`12-Jul-2022 <https://mail.python.org/archives/list/python-dev@python.org/thread/PLCNW2XR4OOKAKHEZQM7R2AYVYUXPZGW/>`__,
`29-May-2022 <https://mail.python.org/archives/list/typing-sig@python.org/thread/U42MJE6QZYWPVIFHJIGIT7OE52ZGIQV3/>`__


Abstract
========

Currently ``**kwargs`` can be type hinted as long as all of the keyword
arguments specified by them are of the same type. However, that behaviour can
be very limiting. Therefore, in this PEP we propose a new way to enable more
precise ``**kwargs`` typing. The new approach revolves around using
``TypedDict`` to type ``**kwargs`` that comprise keyword arguments of different
types. It also involves intro E936 ducing a grammar change and a new dunder
``__unpack__``.
``__typing_kwargs_unpack__``. The scope of a new dunder will be limited to the
``TypedDict`` type only.

Motivation
==========
Expand Down Expand Up @@ -420,7 +422,7 @@ After:
| '**' param_no_default

param_no_default_double_star_annotation:
| param_double_star_annotation & ')'
| param_double_star_annotation ','? &')'

param_double_star_annotation: NAME double_star_annotation

Expand Down Expand Up @@ -463,13 +465,15 @@ previous example::
>>> def foo(**kwargs: **Movie): ...
...
>>> foo.__annotations__
{'kwargs': **Movie}

The double asterisk syntax should call the ``__unpack__`` special method on
the object it was used on. This means that ``def foo(**kwargs: **T): ...`` is
equivalent to ``def foo(**kwargs: T.__unpack__()): ...``. In addition,
``**Movie`` in the example above is the ``repr`` of the object that
``__unpack__()`` returns.
{'kwargs': Unpack[Movie]}

To accomplish this, we propose a new dunder called ``__typing_kwargs_unpack__``.
The double asterisk syntax should result in a call to the
``__typing_kwargs_unpack__`` special method on an object it was used on.
This means that ``def foo(**kwargs: **T): ...`` is equivalent to
``def foo(**kwargs: T.__typing_kwargs_unpack__()): ...``.
``TypedDict`` is the only type in the standard library that will implement
``__typing_kwargs_unpack`` and it should return ``Unpack[self]``.

Backwards Compatibility
-----------------------
Expand Down
0