Skip to content

Commit 83d8a4c

Browse files
committed
Document supported TypedDict operations and methods
Follow-up to #6011 (this must be merged after it).
1 parent 2b1bb6e commit 83d8a4c

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

docs/source/more_types.rst

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ Our first attempt at writing this function might look like this:
200200
201201
from typing import Union, Optional
202202
203-
def mouse_event(x1: int,
203+
def mouse_event(x1: int,
204204
y1: int,
205205
x2: Optional[int] = None,
206206
y2: Optional[int] = None) -> Union[ClickEvent, DragEvent]:
@@ -245,7 +245,7 @@ to more accurately describe the function's behavior:
245245
# Mypy will also check and make sure the signature is
246246
# consistent with the provided variants.
247247
248-
def mouse_event(x1: int,
248+
def mouse_event(x1: int,
249249
y1: int,
250250
x2: Optional[int] = None,
251251
y2: Optional[int] = None) -> Union[ClickEvent, DragEvent]:
@@ -374,7 +374,7 @@ First, if multiple variants match due to an argument being of type
374374
output2 = summarize(dynamic_var)
375375
376376
Second, if multiple variants match due to one or more of the arguments
377-
being a union, mypy will make the inferred type be the union of the
377+
being a union, mypy will make the inferred type be the union of the
378378
matching variant returns:
379379

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

392392
To minimize potential issues, we recommend that you:
393-
393+
394394
1. Make sure your overload variants are listed in the same order as
395-
the runtime checks (e.g. ``isinstance`` checks) in your implementation.
395+
the runtime checks (e.g. ``isinstance`` checks) in your implementation.
396396
2. Order your variants and runtime checks from most to least specific.
397397
(See the following section for an example).
398398

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

882+
Supported operations
883+
--------------------
884+
885+
TypedDict objects support a subset of dictionary operations and methods:
886+
887+
* Anything included in the mapping protocol, such as indexing,
888+
``get()``, ``keys()`` and ``items()``
889+
* ``copy()``
890+
* ``setdefault(k, default)``
891+
* ``update(d)``
892+
* ``pop(k[, default])`` (partial TypedDicts only)
893+
* ``del d[k]`` (partial TypedDicts only)
894+
895+
These methods are also supported in Python 2 code:
896+
897+
* ``has_key(k)``
898+
* ``viewitems()``
899+
* ``viewkeys()``
900+
* ``viervalues()``
901+
902+
You must use string literals as keys when calling most of the above methods,
903+
as otherwise mypy won't be able to check that the key is valid.
904+
905+
``clear()`` and ``popitem()`` are not supported since they are unsafe
906+
-- they could delete required TypedDict items that are not visible to
907+
mypy because of structural subtyping.
908+
882909
Class-based syntax
883910
------------------
884911

0 commit comments

Comments
 (0)