Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 446e168

Browse files
committed
Issue #26141: Update docs for typing.py. Ivan Levkivskyi.
1 parent f7f8299 commit 446e168

1 file changed

Lines changed: 104 additions & 10 deletions

File tree

Doc/library/typing.rst

Lines changed: 104 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
--------------
1212

13-
This module supports type hints as specified by :pep:`484`. The most
13+
This module supports type hints as specified by :pep:`484` and :pep:`526`. The most
1414
fundamental support consists of the type :class:`Any`, :class:`Union`,
1515
:class:`Tuple`, :class:`Callable`, :class:`TypeVar`, and
1616
:class:`Generic`. For full specification please see :pep:`484`. For
@@ -62,10 +62,12 @@ Type aliases are useful for simplifying complex type signatures. For example::
6262
Note that ``None`` as a type hint is a special case and is replaced by
6363
``type(None)``.
6464

65+
.. _distinct:
66+
6567
NewType
6668
-------
6769

68-
Use the ``NewType`` helper function to create distinct types::
70+
Use the :func:`NewType` helper function to create distinct types::
6971

7072
from typing import NewType
7173

@@ -103,7 +105,7 @@ true at runtime.
103105

104106
This also means that it is not possible to create a subtype of ``Derived``
105107
since it is an identity function at runtime, not an actual type. Similarly, it
106-
is not possible to create another ``NewType`` based on a ``Derived`` type::
108+
is not possible to create another :func:`NewType` based on a ``Derived`` type::
107109

108110
from typing import NewType
109111

@@ -533,10 +535,10 @@ The module defines the following classes, functions and decorators:
533535
but should also allow constructor calls in subclasses that match the
534536
constructor calls in the indicated base class. How the type checker is
535537
required to handle this particular case may change in future revisions of
536-
PEP 484.
538+
:pep:`484`.
537539

538-
The only legal parameters for ``Type`` are classes, unions of classes, and
539-
``Any``. For example::
540+
The only legal parameters for :class:`Type` are classes, unions of classes, and
541+
:class:`Any`. For example::
540542

541543
def new_non_team_user(user_class: Type[Union[BaseUser, ProUser]]): ...
542544

@@ -577,23 +579,37 @@ The module defines the following classes, functions and decorators:
577579

578580
A generic version of :class:`collections.abc.Container`.
579581

580-
.. class:: AbstractSet(Sized, Iterable[T_co], Container[T_co])
582+
.. class:: Hashable
583+
584+
An alias to :class:`collections.abc.Hashable`
585+
586+
.. class:: Sized
587+
588+
An alias to :class:`collections.abc.Sized`
589+
590+
.. class:: Collection(Sized, Iterable[T_co], Container[T_co])
591+
592+
A generic version of :class:`collections.abc.Collection`
593+
594+
.. versionadded:: 3.6
595+
596+
.. class:: AbstractSet(Sized, Collection[T_co])
581597

582598
A generic version of :class:`collections.abc.Set`.
583599

584600
.. class:: MutableSet(AbstractSet[T])
585601

586602
A generic version of :class:`collections.abc.MutableSet`.
587603

588-
.. class:: Mapping(Sized, Iterable[KT], Container[KT], Generic[VT_co])
604+
.. class:: Mapping(Sized, Collection[KT], Generic[VT_co])
589605

590606
A generic version of :class:`collections.abc.Mapping`.
591607

592608
.. class:: MutableMapping(Mapping[KT, VT])
593609

594610
A generic version of :class:`collections.abc.MutableMapping`.
595611

596-
.. class:: Sequence(Sized, Reversible[T_co], Container[T_co])
612+
.. class:: Sequence(Reversible[T_co], Collection[T_co])
597613

598614
A generic version of :class:`collections.abc.Sequence`.
599615

@@ -674,6 +690,10 @@ The module defines the following classes, functions and decorators:
674690
def get_position_in_index(word_list: Dict[str, int], word: str) -> int:
675691
return word_list[word]
676692

693+
.. class:: DefaultDict(collections.defaultdict, MutableMapping[KT, VT])
694+
695+
A generic version of :class:`collections.defaultdict`
696+
677697
.. class:: Generator(Iterator[T_co], Generic[T_co, T_contra, V_co])
678698

679699
A generator can be annotated by the generic type
@@ -769,6 +789,15 @@ The module defines the following classes, functions and decorators:
769789
are in the _fields attribute, which is part of the namedtuple
770790
API.)
771791

792+
.. function:: NewType(typ)
793+
794+
A helper function to indicate a distinct types to a typechecker,
795+
see :ref:`distinct`. At runtime it returns a function that returns
796+
its argument. Usage::
797+
798+
UserId = NewType('UserId', int)
799+
first_user = UserId(1)
800+
772801
.. function:: cast(typ, val)
773802

774803
Cast a value to a type.
@@ -780,12 +809,40 @@ The module defines the following classes, functions and decorators:
780809

781810
.. function:: get_type_hints(obj)
782811

783-
Return type hints for a function or method object.
812+
Return type hints for a class, module, function or method object.
784813

785814
This is often the same as ``obj.__annotations__``, but it handles
786815
forward references encoded as string literals, and if necessary
787816
adds ``Optional[t]`` if a default value equal to None is set.
788817

818+
.. decorator:: overload
819+
820+
The ``@overload`` decorator allows describing functions and methods
821+
that support multiple different combinations of argument types. A series
822+
of ``@overload``-decorated definitions must be followed by exactly one
823+
non-``@overload``-decorated definition (for the same function/method).
824+
The ``@overload``-decorated definitions are for the benefit of the
825+
type checker only, since they will be overwritten by the
826+
non-``@overload``-decorated definition, while the latter is used at
827+
runtime but should be ignored by a type checker. At runtime, calling
828+
a ``@overload``-decorated function directly will raise
829+
``NotImplementedError``. An example of overload that gives a more
830+
precise type than can be expressed using a union or a type variable::
831+
832+
@overload
833+
def process(response: None) -> None:
834+
...
835+
@overload
836+
def process(response: int) -> Tuple[int, str]:
837+
...
838+
@overload
839+
def process(response: bytes) -> str:
840+
...
841+
def process(response):
842+
<actual implementation>
843+
844+
See :pep:`484` for details and comparison with other typing semantics.
845+
789846
.. decorator:: no_type_check(arg)
790847

791848
Decorator to indicate that annotations are not type hints.
@@ -802,3 +859,40 @@ The module defines the following classes, functions and decorators:
802859

803860
This wraps the decorator with something that wraps the decorated
804861
function in :func:`no_type_check`.
862+
863+
.. data:: ClassVar
864+
865+
Special type construct to mark class variables.
866+
867+
As introduced in :pep:`526`, a variable annotation wrapped in ClassVar
868+
indicates that a given attribute is intended to be used as a class variable
869+
and should not be set on instances of that class. Usage::
870+
871+
class Starship:
872+
stats: ClassVar[Dict[str, int]] = {} # class variable
873+
damage: int = 10 # instance variable
874+
875+
ClassVar accepts only types and cannot be further subscribed.
876+
877+
ClassVar is not a class itself, and should not
878+
be used with isinstance() or issubclass(). Note that ClassVar
879+
does not change Python runtime behavior, it can be used by
880+
3rd party type checkers, so that the following code will
881+
flagged as an error by those::
882+
883+
enterprise_d = Starship(3000)
884+
enterprise_d.stats = {} # Error, setting class variable on instance
885+
Starship.stats = {} # This is OK
886+
887+
.. versionadded:: 3.6
888+
889+
.. data:: TYPE_CHECKING
890+
891+
A special constant that is assumed to be ``True`` by 3rd party static
892+
type checkers. It is ``False`` at runtime. Usage::
893+
894+
if TYPE_CHECKING:
895+
import expensive_mod
896+
897+
def fun():
898+
local_var: expensive_mod.some_type = other_fun()

0 commit comments

Comments
 (0)