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
Merged
Changes from all commits
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
50 changes: 44 additions & 6 deletions docs/source/more_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ Our first attempt at writing this function might look like this:

from typing import Union, Optional

def mouse_event(x1: int,
def mouse_event(x1: int,
y1: int,
x2: Optional[int] = None,
y2: Optional[int] = None) -> Union[ClickEvent, DragEvent]:
Expand Down Expand Up @@ -245,7 +245,7 @@ to more accurately describe the function's behavior:
# Mypy will also check and make sure the signature is
# consistent with the provided variants.

def mouse_event(x1: int,
def mouse_event(x1: int,
y1: int,
x2: Optional[int] = None,
y2: Optional[int] = None) -> Union[ClickEvent, DragEvent]:
Expand Down Expand Up @@ -374,7 +374,7 @@ First, if multiple variants match due to an argument being of type
output2 = summarize(dynamic_var)

Second, if multiple variants match due to one or more of the arguments
being a union, mypy will make the inferred type be the union of the
being a union, mypy will make the inferred type be the union of the
matching variant returns:

.. code-block:: python
Expand All @@ -390,9 +390,9 @@ matching variant returns:
overload variants can change how mypy type checks your program.

To minimize potential issues, we recommend that you:

1. Make sure your overload variants are listed in the same order as
the runtime checks (e.g. ``isinstance`` checks) in your implementation.
the runtime checks (e.g. ``isinstance`` checks) in your implementation.
2. Order your variants and runtime checks from most to least specific.
(See the following section for an example).

Expand Down Expand Up @@ -533,7 +533,7 @@ Type checking the implementation
The body of an implementation is type-checked against the
type hints provided on the implementation. For example, in the
``MyList`` example up above, the code in the body is checked with
argument list ``index: Union[int, slice]`` and a return type of
argument list ``index: Union[int, slice]`` and a return type of
``Union[T, Sequence[T]]``. If there are no annotations on the
implementation, then the body is not type checked. If you want to
force mypy to check the body anyways, use the ``--check-untyped-defs``
Expand Down Expand Up @@ -879,6 +879,44 @@ Totality also affects structural compatibility. You can't use a partial
TypedDict when a total one is expected. Also, a total TypedDict is not
valid when a partial one is expected.

Supported operations
--------------------

TypedDict objects support a subset of dictionary operations and methods.
You must use string literals as keys when calling most of the methods,
as otherwise mypy won't be able to check that the key is valid. List
of supported operations:

* Anything included in ``typing.Mapping``:

* ``d[key]``
* ``key in d``
* ``len(d)``
* ``for key in d`` (iteration)
* ``d.get(key[, default])``
* ``d.keys()``
* ``d.values()``
* ``d.items()``

* ``d.copy()``
* ``d.setdefault(key, default)``
* ``d1.update(d2)``
* ``d.pop(key[, default])`` (partial TypedDicts only)
* ``del d[key]`` (partial TypedDicts only)

In Python 2 code, these methods are also supported:

* ``has_key(key)``
* ``viewitems()``
* ``viewkeys()``
* ``viervalues()``

.. note::

``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.

Class-based syntax
------------------

Expand Down