Repository navigation
Update the cheatsheet for functions and keyword argument typing - #15163
franekmagiera wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
Thanks for the PR and all your work on this! Could you add documentation to the TypedDict page: https://mypy.readthedocs.io/en/stable/typed_dict.html ?
| request = make_request(*args, **kwargs) | ||
| return self.do_api_query(request) | ||
|
|
||
| # For more preise keyword typing, you can use `Unpack` along with a |
There was a problem hiding this comment.
| # For more preise keyword typing, you can use `Unpack` along with a | |
| # For more precise keyword typing, you can use `Unpack` along with a |
|
I don't think mypy enables PEP 692 by default yet, so maybe we should do that first? |
Updated and extended the docs section on mixing required non-required items in a TypedDict. Introduced the use of Required and NotRequired type qualifiers.
|
Thanks for the comments. I added the section to the TypedDict docs, also updated the section on mixing required and not required items. As for enabling |
| As a rule of thumb, if more keys of a particular ``TypedDict`` are required | ||
| than not, construct the new type with the ``total`` parameter set to ``True`` | ||
| and qualify the non required keys using ``NotRequired``. Otherwise, construct | ||
| the new type with the ``total`` parameter set to ``False`` and qualify the | ||
| required keys with ``Required``. | ||
|
|
||
| Using ``Required`` with a total ``TypedDict`` and using ``NotRequired`` with a | ||
| partial ``TypedDict`` is redundant. However, it is allowed and can be used to | ||
| qualify the required and not required keys explicitly. | ||
|
|
||
| If a particular key can accept ``None``, it is recommended to avoid mixing | ||
| ``Optional`` with either ``Required`` or ``NotRequired`` and use the | ||
| ``TYPE|None`` notation instead: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| # Preferred approach. | ||
| class Car(TypedDict): | ||
| model: str | ||
| owner: NotRequired[str | None] | ||
|
|
||
| # Not recommended. | ||
| class Car(TypedDict): | ||
| model: str | ||
| owner: NotRequired[Optional[str]] |
There was a problem hiding this comment.
Let me know if this is too long. Maybe we could just link to the "How to teach this" section of the PEP 655?
|
Hey, sorry for no updates on this (the weather was too nice not to go outside). I started working on enabling the |
|
I've opened another PR #15612 that's adding tests from PEP 692, still have trouble with:
|
PEP 692 has been accepted and issue #4441 is fixed - added an entry to the cheatsheet mentioning a new way of annotating
**kwargsin a function signature.