Skip to content

Commit 45d22c2

Browse files
JelleZijlstraberkerpeksag
authored andcommitted
Improvements to typing documentation (#967)
Documents a few omitted classes and adds NamedTuple methods.
1 parent b785396 commit 45d22c2

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

Doc/library/typing.rst

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,14 @@ The module defines the following classes, functions and decorators:
508508

509509
An ABC with one abstract method ``__float__``.
510510

511+
.. class:: SupportsComplex
512+
513+
An ABC with one abstract method ``__complex__``.
514+
515+
.. class:: SupportsBytes
516+
517+
An ABC with one abstract method ``__bytes__``.
518+
511519
.. class:: SupportsAbs
512520

513521
An ABC with one abstract method ``__abs__`` that is covariant
@@ -658,7 +666,19 @@ The module defines the following classes, functions and decorators:
658666

659667
.. class:: DefaultDict(collections.defaultdict, MutableMapping[KT, VT])
660668

661-
A generic version of :class:`collections.defaultdict`
669+
A generic version of :class:`collections.defaultdict`.
670+
671+
.. class:: Counter(collections.Counter, Dict[T, int])
672+
673+
A generic version of :class:`collections.Counter`.
674+
675+
.. versionadded:: 3.6.1
676+
677+
.. class:: ChainMap(collections.ChainMap, MutableMapping[KT, VT])
678+
679+
A generic version of :class:`collections.ChainMap`.
680+
681+
.. versionadded:: 3.6.1
662682

663683
.. class:: Generator(Iterator[T_co], Generic[T_co, T_contra, V_co])
664684

@@ -742,9 +762,12 @@ The module defines the following classes, functions and decorators:
742762

743763
This defines the generic type ``IO[AnyStr]`` and aliases ``TextIO``
744764
and ``BinaryIO`` for respectively ``IO[str]`` and ``IO[bytes]``.
745-
These representing the types of I/O streams such as returned by
765+
These represent the types of I/O streams such as returned by
746766
:func:`open`.
747767

768+
These types are also accessible directly as ``typing.IO``,
769+
``typing.TextIO``, and ``typing.BinaryIO``.
770+
748771
.. class:: re
749772

750773
Wrapper namespace for regular expression matching types.
@@ -756,6 +779,9 @@ The module defines the following classes, functions and decorators:
756779
``Pattern[str]``, ``Pattern[bytes]``, ``Match[str]``, or
757780
``Match[bytes]``.
758781

782+
These types are also accessible directly as ``typing.Pattern``
783+
and ``typing.Match``.
784+
759785
.. class:: NamedTuple
760786

761787
Typed version of namedtuple.
@@ -782,10 +808,20 @@ The module defines the following classes, functions and decorators:
782808
Fields with a default value must come after any fields without a default.
783809

784810
The resulting class has two extra attributes: ``_field_types``,
785-
giving a dict mapping field names to types, and ``field_defaults``, a dict
811+
giving a dict mapping field names to types, and ``_field_defaults``, a dict
786812
mapping field names to default values. (The field names are in the
787813
``_fields`` attribute, which is part of the namedtuple API.)
788814

815+
``NamedTuple`` subclasses can also have docstrings and methods::
816+
817+
class Employee(NamedTuple):
818+
"""Represents an employee."""
819+
name: str
820+
id: int = 3
821+
822+
def __repr__(self) -> str:
823+
return f'<Employee {self.name}, id={self.id}>'
824+
789825
Backward-compatible usage::
790826

791827
Employee = NamedTuple('Employee', [('name', str), ('id', int)])
@@ -794,7 +830,7 @@ The module defines the following classes, functions and decorators:
794830
Added support for :pep:`526` variable annotation syntax.
795831

796832
.. versionchanged:: 3.6.1
797-
Added support for default values.
833+
Added support for default values, methods, and docstrings.
798834

799835
.. function:: NewType(typ)
800836

@@ -972,9 +1008,9 @@ The module defines the following classes, functions and decorators:
9721008

9731009
:data:`ClassVar` is not a class itself, and should not
9741010
be used with :func:`isinstance` or :func:`issubclass`.
975-
Note that :data:`ClassVar` does not change Python runtime behavior;
976-
it can be used by 3rd party type checkers, so that the following
977-
code might flagged as an error by those::
1011+
:data:`ClassVar` does not change Python runtime behavior, but
1012+
it can be used by third-party type checkers. For example, a type checker
1013+
might flag the following code as an error::
9781014

9791015
enterprise_d = Starship(3000)
9801016
enterprise_d.stats = {} # Error, setting class variable on instance

0 commit comments

Comments
 (0)