Skip to content

Commit ab6423f

Browse files
authored
bpo-39572: Document ’total’ flag of TypedDict (GH-18554)
1 parent a4ba8a3 commit ab6423f

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

Doc/library/typing.rst

+14-2
Original file line numberDiff line numberDiff line change
@@ -996,8 +996,20 @@ The module defines the following classes, functions and decorators:
996996
Point2D = TypedDict('Point2D', x=int, y=int, label=str)
997997
Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
998998

999-
See :pep:`589` for more examples and detailed rules of using ``TypedDict``
1000-
with type checkers.
999+
By default, all keys must be present in a TypedDict. It is possible
1000+
to override this by specifying totality.
1001+
Usage::
1002+
1003+
class point2D(TypedDict, total=False):
1004+
x: int
1005+
y: int
1006+
1007+
This means that a point2D TypedDict can have any of the keys omitted.A type
1008+
checker is only expected to support a literal False or True as the value of
1009+
the total argument. True is the default, and makes all items defined in the
1010+
class body be required.
1011+
1012+
See :pep:`589` for more examples and detailed rules of using ``TypedDict``.
10011013

10021014
.. versionadded:: 3.8
10031015

Lib/typing.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Public helper functions: get_type_hints, overload, cast, no_type_check,
1414
no_type_check_decorator.
1515
* Generic aliases for collections.abc ABCs and few additional protocols.
16-
* Special types: NewType, NamedTuple, TypedDict (may be added soon).
16+
* Special types: NewType, NamedTuple, TypedDict.
1717
* Wrapper submodules for re and io related types.
1818
"""
1919

@@ -1885,6 +1885,19 @@ class Point2D(TypedDict):
18851885
Point2D = TypedDict('Point2D', x=int, y=int, label=str)
18861886
Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
18871887
1888+
By default, all keys must be present in a TypedDict. It is possible
1889+
to override this by specifying totality.
1890+
Usage::
1891+
1892+
class point2D(TypedDict, total=False):
1893+
x: int
1894+
y: int
1895+
1896+
This means that a point2D TypedDict can have any of the keys omitted.A type
1897+
checker is only expected to support a literal False or True as the value of
1898+
the total argument. True is the default, and makes all items defined in the
1899+
class body be required.
1900+
18881901
The class syntax is only supported in Python 3.6+, while two other
18891902
syntax forms work for Python 2.7 and 3.2+
18901903
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Updated documentation of ``total`` flag of TypeDict.

0 commit comments

Comments
 (0)