Skip to content

Support TypedDict update() with keyword arguments #6019

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

Open
JukkaL opened this issue Dec 6, 2018 · 2 comments
Open

Support TypedDict update() with keyword arguments #6019

JukkaL opened this issue Dec 6, 2018 · 2 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 6, 2018

Things like d.update(x=1, y=2) should be accepted if d is a TypedDict and the keys and values are compatible.

JukkaL added a commit that referenced this issue Dec 6, 2018
This adds support for these methods through additional plugin hooks:

* `pop`
* `setdefault`
* `update` (positional argument only)
* `__delitem__`

These methods also work and don't need plugin support:

* `copy`
* `has_key` (Python 2 only)
* `viewitems` (Python 2 only)
* `viewkeys` (Python 2 only)
* `viewvalues` (Python 2 only)

The base signatures for all of these methods are defined in 
`mypy_extensions._TypedDict`, which is a stub-only class
only used internally by mypy. It becomes the new fallback
type of all TypedDicts.

Fixes #3843. Fixes #3550.

There's some possible follow-up work that I'm leaving to other PRs,
such as optimizing hook lookup through dictionaries in the default
plugin, documenting the supported methods, and `update` with
keyword arguments (#6019).
@emmatyping
Copy link
Member

emmatyping commented Dec 28, 2024

I think this would need to be added as a specification of the standard. I do not see this as allowed anywhere, and currently the update method is annotated as accepting only a TypedDict in typeshed.

Perhaps this should be moved to the typing repo (or I guess CPython)?

@JelleZijlstra
Copy link
Member

I don't think this would require a change to the spec. The spec is mostly silent on the update() method (there is some discussion from the ReadOnly PEP at https://typing.readthedocs.io/en/latest/spec/typeddict.html#update-method), but it wouldn't be against the spec to allow update() calls that preserve the type. Pyright also accepts it: https://pyright-play.net/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoAqiApgCYAiSAxjAFC1UA2AhgM6tQAaAFEQmZRoBKAFy0oEqMxGYUdWqWLAowbgA8ZnUeMlqAdAFcEpZjGLdmAXgCMQ2kA

Perhaps this should be moved to the typing repo (or I guess CPython)?

The CPython issue tracker should only be used for tracking changes to the typing.py runtime. Spec changes should go to the python/typing issue tracker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants