@@ -200,7 +200,7 @@ Our first attempt at writing this function might look like this:
200
200
201
201
from typing import Union, Optional
202
202
203
- def mouse_event (x1 : int ,
203
+ def mouse_event (x1 : int ,
204
204
y1 : int ,
205
205
x2 : Optional[int ] = None ,
206
206
y2 : Optional[int ] = None ) -> Union[ClickEvent, DragEvent]:
@@ -245,7 +245,7 @@ to more accurately describe the function's behavior:
245
245
# Mypy will also check and make sure the signature is
246
246
# consistent with the provided variants.
247
247
248
- def mouse_event (x1 : int ,
248
+ def mouse_event (x1 : int ,
249
249
y1 : int ,
250
250
x2 : Optional[int ] = None ,
251
251
y2 : Optional[int ] = None ) -> Union[ClickEvent, DragEvent]:
@@ -374,7 +374,7 @@ First, if multiple variants match due to an argument being of type
374
374
output2 = summarize(dynamic_var)
375
375
376
376
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
378
378
matching variant returns:
379
379
380
380
.. code-block :: python
@@ -390,9 +390,9 @@ matching variant returns:
390
390
overload variants can change how mypy type checks your program.
391
391
392
392
To minimize potential issues, we recommend that you:
393
-
393
+
394
394
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.
396
396
2. Order your variants and runtime checks from most to least specific.
397
397
(See the following section for an example).
398
398
@@ -533,7 +533,7 @@ Type checking the implementation
533
533
The body of an implementation is type-checked against the
534
534
type hints provided on the implementation. For example, in the
535
535
``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
537
537
``Union[T, Sequence[T]] ``. If there are no annotations on the
538
538
implementation, then the body is not type checked. If you want to
539
539
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
879
879
TypedDict when a total one is expected. Also, a total TypedDict is not
880
880
valid when a partial one is expected.
881
881
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
+
882
909
Class-based syntax
883
910
------------------
884
911
0 commit comments