mirror of
https://github.com/python/cpython.git
synced 2025-11-10 14:31:24 +00:00
bpo-43345: Enhance TypedDict documentation. (#24668)
This commit is contained in:
parent
bf9de7ab24
commit
1e3c68246e
1 changed files with 13 additions and 12 deletions
|
|
@ -1247,26 +1247,28 @@ These are not used in annotations. They are building blocks for declaring types.
|
||||||
|
|
||||||
assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
|
assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
|
||||||
|
|
||||||
The type info for introspection can be accessed via ``Point2D.__annotations__``
|
The type info for introspection can be accessed via ``Point2D.__annotations__``,
|
||||||
and ``Point2D.__total__``. To allow using this feature with older versions
|
``Point2D.__total__``, ``Point2D.__required_keys__``, and
|
||||||
of Python that do not support :pep:`526`, ``TypedDict`` supports two additional
|
``Point2D.__optional_keys__``.
|
||||||
equivalent syntactic forms::
|
To allow using this feature with older versions of Python that do not
|
||||||
|
support :pep:`526`, ``TypedDict`` supports two additional equivalent
|
||||||
|
syntactic forms::
|
||||||
|
|
||||||
Point2D = TypedDict('Point2D', x=int, y=int, label=str)
|
Point2D = TypedDict('Point2D', x=int, y=int, label=str)
|
||||||
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
|
By default, all keys must be present in a ``TypedDict``. It is possible to
|
||||||
to override this by specifying totality.
|
override this by specifying totality.
|
||||||
Usage::
|
Usage::
|
||||||
|
|
||||||
class point2D(TypedDict, total=False):
|
class Point2D(TypedDict, total=False):
|
||||||
x: int
|
x: int
|
||||||
y: int
|
y: int
|
||||||
|
|
||||||
This means that a point2D TypedDict can have any of the keys omitted. A type
|
This means that a ``Point2D`` ``TypedDict`` can have any of the keys
|
||||||
checker is only expected to support a literal False or True as the value of
|
omitted. A type checker is only expected to support a literal ``False`` or
|
||||||
the total argument. True is the default, and makes all items defined in the
|
``True`` as the value of the ``total`` argument. ``True`` is the default,
|
||||||
class body be required.
|
and makes all items defined in the class body required.
|
||||||
|
|
||||||
See :pep:`589` for more examples and detailed rules of using ``TypedDict``.
|
See :pep:`589` for more examples and detailed rules of using ``TypedDict``.
|
||||||
|
|
||||||
|
|
@ -1980,4 +1982,3 @@ Constant
|
||||||
(see :pep:`563`).
|
(see :pep:`563`).
|
||||||
|
|
||||||
.. versionadded:: 3.5.2
|
.. versionadded:: 3.5.2
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue