Skip to content

bpo-39572:documented _total_ attribute of TypedDict #18554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -996,8 +996,20 @@ The module defines the following classes, functions and decorators:
Point2D = TypedDict('Point2D', x=int, y=int, label=str)
Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})

See :pep:`589` for more examples and detailed rules of using ``TypedDict``
with type checkers.
By default, all keys must be present in a TypedDict. It is possible
to override this by specifying totality.
Usage::

class point2D(TypedDict, total=False):
x: int
y: int

This means that a point2D TypedDict can have any of the keys omitted.A type
checker is only expected to support a literal False or True as the value of
the total argument. True is the default, and makes all items defined in the
class body be required.

See :pep:`589` for more examples and detailed rules of using ``TypedDict``.

.. versionadded:: 3.8

Expand Down
15 changes: 14 additions & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* Public helper functions: get_type_hints, overload, cast, no_type_check,
no_type_check_decorator.
* Generic aliases for collections.abc ABCs and few additional protocols.
* Special types: NewType, NamedTuple, TypedDict (may be added soon).
* Special types: NewType, NamedTuple, TypedDict.
* Wrapper submodules for re and io related types.
"""

Expand Down Expand Up @@ -1878,6 +1878,19 @@ class Point2D(TypedDict):
Point2D = TypedDict('Point2D', x=int, y=int, label=str)
Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})

By default, all keys must be present in a TypedDict. It is possible
to override this by specifying totality.
Usage::

class point2D(TypedDict, total=False):
x: int
y: int

This means that a point2D TypedDict can have any of the keys omitted.A type
checker is only expected to support a literal False or True as the value of
the total argument. True is the default, and makes all items defined in the
class body be required.

The class syntax is only supported in Python 3.6+, while two other
syntax forms work for Python 2.7 and 3.2+
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Updated documentation of ``total`` flag of TypeDict.