Skip to content

Commit 58e92fe

Browse files
authored
Document supported TypedDict operations and methods (#6014)
Follow-up to #6011.
1 parent d34d285 commit 58e92fe

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

docs/source/more_types.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,44 @@ Totality also affects structural compatibility. You can't use a partial
893893
TypedDict when a total one is expected. Also, a total TypedDict is not
894894
valid when a partial one is expected.
895895

896+
Supported operations
897+
--------------------
898+
899+
TypedDict objects support a subset of dictionary operations and methods.
900+
You must use string literals as keys when calling most of the methods,
901+
as otherwise mypy won't be able to check that the key is valid. List
902+
of supported operations:
903+
904+
* Anything included in ``typing.Mapping``:
905+
906+
* ``d[key]``
907+
* ``key in d``
908+
* ``len(d)``
909+
* ``for key in d`` (iteration)
910+
* ``d.get(key[, default])``
911+
* ``d.keys()``
912+
* ``d.values()``
913+
* ``d.items()``
914+
915+
* ``d.copy()``
916+
* ``d.setdefault(key, default)``
917+
* ``d1.update(d2)``
918+
* ``d.pop(key[, default])`` (partial TypedDicts only)
919+
* ``del d[key]`` (partial TypedDicts only)
920+
921+
In Python 2 code, these methods are also supported:
922+
923+
* ``has_key(key)``
924+
* ``viewitems()``
925+
* ``viewkeys()``
926+
* ``viervalues()``
927+
928+
.. note::
929+
930+
``clear()`` and ``popitem()`` are not supported since they are unsafe
931+
-- they could delete required TypedDict items that are not visible to
932+
mypy because of structural subtyping.
933+
896934
Class-based syntax
897935
------------------
898936

0 commit comments

Comments
 (0)