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

Skip to content

Commit ae08687

Browse files
committed
Issue 28644: Document recent changes in typing.py (Ivan L)
1 parent 5adc22b commit ae08687

1 file changed

Lines changed: 50 additions & 15 deletions

File tree

Doc/library/typing.rst

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
--------------
1212

1313
This module supports type hints as specified by :pep:`484`. The most
14-
fundamental support consists of the type :data:`Any`, :data:`Union`,
14+
fundamental support consists of the types :data:`Any`, :data:`Union`,
1515
:data:`Tuple`, :data:`Callable`, :class:`TypeVar`, and
1616
:class:`Generic`. For full specification please see :pep:`484`. For
1717
a simplified introduction to type hints see :pep:`483`.
@@ -274,6 +274,22 @@ not generic but implicitly inherits from ``Iterable[Any]``::
274274

275275
class MyIterable(Iterable): # Same as Iterable[Any]
276276

277+
User defined generic type aliases are also supported. Examples::
278+
279+
from typing import TypeVar, Iterable, Tuple, Union
280+
S = TypeVar('S')
281+
Response = Union[Iterable[S], int]
282+
283+
# Return type here is same as Union[Iterable[str], int]
284+
def response(query: str) -> Response[str]:
285+
...
286+
287+
T = TypeVar('T', int, float, complex)
288+
Vec = Iterable[Tuple[T, T]]
289+
290+
def inproduct(v: Vec[T]) -> T: # Same as Iterable[Tuple[T, T]]
291+
return sum(x*y for x, y in v)
292+
277293
The metaclass used by :class:`Generic` is a subclass of :class:`abc.ABCMeta`.
278294
A generic class can be an ABC by including abstract methods or properties,
279295
and generic classes can also have ABCs as base classes without a metaclass
@@ -582,6 +598,19 @@ The module defines the following classes, functions and decorators:
582598

583599
A generic version of :class:`collections.abc.Awaitable`.
584600

601+
.. class:: Coroutine(Awaitable[V_co], Generic[T_co T_contra, V_co])
602+
603+
A generic version of :class:`collections.abc.Coroutine`.
604+
The variance and order of type variables
605+
correspond to those of :class:`Generator`, for example::
606+
607+
from typing import List, Coroutine
608+
c = None # type: Coroutine[List[str], str, int]
609+
...
610+
x = c.send('hi') # type: List[str]
611+
async def bar() -> None:
612+
x = await c # type: int
613+
585614
.. class:: AsyncIterable(Generic[T_co])
586615

587616
A generic version of :class:`collections.abc.AsyncIterable`.
@@ -700,13 +729,18 @@ The module defines the following classes, functions and decorators:
700729
runtime we intentionally don't check anything (we want this
701730
to be as fast as possible).
702731

703-
.. function:: get_type_hints(obj)
732+
.. function:: get_type_hints(obj[, globals[, locals])
704733

705-
Return type hints for a function or method object.
734+
Return a dictionary containing type hints for a function, method, module
735+
or class object.
706736

707-
This is often the same as ``obj.__annotations__``, but it handles
708-
forward references encoded as string literals, and if necessary
709-
adds ``Optional[t]`` if a default value equal to ``None`` is set.
737+
This is often the same as ``obj.__annotations__``. In addition,
738+
forward references encoded as string literals are handled by evaluating
739+
them in ``globals`` and ``locals`` namespaces. If necessary,
740+
``Optional[t]`` is added for function and method annotations if a default
741+
value equal to ``None`` is set. For a class ``C``, return
742+
a dictionary constructed by merging all the ``__annotations__`` along
743+
``C.__mro__`` in reverse order.
710744

711745
.. decorator:: overload
712746

@@ -809,24 +843,25 @@ The module defines the following classes, functions and decorators:
809843

810844
.. data:: Tuple
811845

812-
Tuple type; ``Tuple[X, Y]`` is the type of a tuple of two items
813-
with the first item of type X and the second of type Y.
846+
Tuple type; ``Tuple[X, Y]`` is the type of a tuple of two items
847+
with the first item of type X and the second of type Y.
814848

815-
Example: ``Tuple[T1, T2]`` is a tuple of two elements corresponding
816-
to type variables T1 and T2. ``Tuple[int, float, str]`` is a tuple
817-
of an int, a float and a string.
849+
Example: ``Tuple[T1, T2]`` is a tuple of two elements corresponding
850+
to type variables T1 and T2. ``Tuple[int, float, str]`` is a tuple
851+
of an int, a float and a string.
818852

819-
To specify a variable-length tuple of homogeneous type,
820-
use literal ellipsis, e.g. ``Tuple[int, ...]``. A plain :data:`Tuple`
821-
is equivalent to ``Tuple[Any, ...]``, and in turn to :data:`tuple`.
853+
To specify a variable-length tuple of homogeneous type,
854+
use literal ellipsis, e.g. ``Tuple[int, ...]``. A plain :data:`Tuple`
855+
is equivalent to ``Tuple[Any, ...]``, and in turn to :class:`tuple`.
822856

823857
.. data:: Callable
824858

825859
Callable type; ``Callable[[int], str]`` is a function of (int) -> str.
826860

827861
The subscription syntax must always be used with exactly two
828862
values: the argument list and the return type. The argument list
829-
must be a list of types; the return type must be a single type.
863+
must be a list of types or an ellipsis; the return type must be
864+
a single type.
830865

831866
There is no syntax to indicate optional or keyword arguments;
832867
such function types are rarely used as callback types.

0 commit comments

Comments
 (0)