Skip to content

Document supported TypedDict operations and methods #6014

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 2 commits into from
Dec 6, 2018

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Dec 5, 2018

Follow-up to #6011 (this must be merged after it).

Follow-up to #6011 (this must be merged after it).
@JukkaL JukkaL requested a review from ilevkivskyi December 5, 2018 17:04
Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

Thanks! Just one small suggestion.


TypedDict objects support a subset of dictionary operations and methods:

* Anything included in the mapping protocol, such as indexing,
Copy link
Member

Choose a reason for hiding this comment

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

I would rather just say in collections.abc.Mapping, since it is not a protocol :-)

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

(Some comments I failed to send before.)

TypedDict objects support a subset of dictionary operations and methods:

* Anything included in the mapping protocol, such as indexing,
``get()``, ``keys()`` and ``items()``
Copy link
Member

Choose a reason for hiding this comment

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

Why not provide the full list here.

* ``pop(k[, default])`` (partial TypedDicts only)
* ``del d[k]`` (partial TypedDicts only)

These methods are also supported in Python 2 code:
Copy link
Member

Choose a reason for hiding this comment

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

I'd lead with "In Python 2 code,".

* ``viervalues()``

You must use string literals as keys when calling most of the above methods,
as otherwise mypy won't be able to check that the key is valid.
Copy link
Member

Choose a reason for hiding this comment

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

Wonder if this doesn't deserve placement at the top of this section. It's pretty important.


``clear()`` and ``popitem()`` are not supported since they are unsafe
-- they could delete required TypedDict items that are not visible to
mypy because of structural subtyping.
Copy link
Member

Choose a reason for hiding this comment

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

Make this a note?

@JukkaL JukkaL merged commit 58e92fe into master Dec 6, 2018
@gvanrossum gvanrossum deleted the doc-typeddict-methods branch December 7, 2018 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants