Skip to content

Commit 44c6901

Browse files
bpo-39572: Document ’total’ flag of TypedDict (GH-18554)
(cherry picked from commit ab6423f) Co-authored-by: ananthan-123 <[email protected]>
1 parent d77e771 commit 44c6901

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

Doc/library/typing.rst

Lines changed: 14 additions & 2 deletions
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

Lines changed: 14 additions & 1 deletion
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

@@ -1779,6 +1779,19 @@ class Point2D(TypedDict):
17791779
Point2D = TypedDict('Point2D', x=int, y=int, label=str)
17801780
Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
17811781
1782+
By default, all keys must be present in a TypedDict. It is possible
1783+
to override this by specifying totality.
1784+
Usage::
1785+
1786+
class point2D(TypedDict, total=False):
1787+
x: int
1788+
y: int
1789+
1790+
This means that a point2D TypedDict can have any of the keys omitted.A type
1791+
checker is only expected to support a literal False or True as the value of
1792+
the total argument. True is the default, and makes all items defined in the
1793+
class body be required.
1794+
17821795
The class syntax is only supported in Python 3.6+, while two other
17831796
syntax forms work for Python 2.7 and 3.2+
17841797
"""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Updated documentation of ``total`` flag of TypeDict.

0 commit comments

Comments
 (0)