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

Skip to content

Commit 2d6c179

Browse files
committed
Issue 28644: Document recent changes in typing.py (Ivan L) (3.5->3.6)
2 parents e0e9d5f + ae08687 commit 2d6c179

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` and :pep:`526`.
14-
The most fundamental support consists of the type :data:`Any`, :data:`Union`,
14+
The most 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
@@ -588,6 +604,19 @@ The module defines the following classes, functions and decorators:
588604

589605
A generic version of :class:`collections.abc.Awaitable`.
590606

607+
.. class:: Coroutine(Awaitable[V_co], Generic[T_co T_contra, V_co])
608+
609+
A generic version of :class:`collections.abc.Coroutine`.
610+
The variance and order of type variables
611+
correspond to those of :class:`Generator`, for example::
612+
613+
from typing import List, Coroutine
614+
c = None # type: Coroutine[List[str], str, int]
615+
...
616+
x = c.send('hi') # type: List[str]
617+
async def bar() -> None:
618+
x = await c # type: int
619+
591620
.. class:: AsyncIterable(Generic[T_co])
592621

593622
A generic version of :class:`collections.abc.AsyncIterable`.
@@ -721,13 +750,18 @@ The module defines the following classes, functions and decorators:
721750
runtime we intentionally don't check anything (we want this
722751
to be as fast as possible).
723752

724-
.. function:: get_type_hints(obj)
753+
.. function:: get_type_hints(obj[, globals[, locals])
725754

726-
Return type hints for a class, module, function or method object.
755+
Return a dictionary containing type hints for a function, method, module
756+
or class object.
727757

728-
This is often the same as ``obj.__annotations__``, but it handles
729-
forward references encoded as string literals, and if necessary
730-
adds ``Optional[t]`` if a default value equal to ``None`` is set.
758+
This is often the same as ``obj.__annotations__``. In addition,
759+
forward references encoded as string literals are handled by evaluating
760+
them in ``globals`` and ``locals`` namespaces. If necessary,
761+
``Optional[t]`` is added for function and method annotations if a default
762+
value equal to ``None`` is set. For a class ``C``, return
763+
a dictionary constructed by merging all the ``__annotations__`` along
764+
``C.__mro__`` in reverse order.
731765

732766
.. decorator:: overload
733767

@@ -830,24 +864,25 @@ The module defines the following classes, functions and decorators:
830864

831865
.. data:: Tuple
832866

833-
Tuple type; ``Tuple[X, Y]`` is the type of a tuple of two items
834-
with the first item of type X and the second of type Y.
867+
Tuple type; ``Tuple[X, Y]`` is the type of a tuple of two items
868+
with the first item of type X and the second of type Y.
835869

836-
Example: ``Tuple[T1, T2]`` is a tuple of two elements corresponding
837-
to type variables T1 and T2. ``Tuple[int, float, str]`` is a tuple
838-
of an int, a float and a string.
870+
Example: ``Tuple[T1, T2]`` is a tuple of two elements corresponding
871+
to type variables T1 and T2. ``Tuple[int, float, str]`` is a tuple
872+
of an int, a float and a string.
839873

840-
To specify a variable-length tuple of homogeneous type,
841-
use literal ellipsis, e.g. ``Tuple[int, ...]``. A plain :data:`Tuple`
842-
is equivalent to ``Tuple[Any, ...]``, and in turn to :data:`tuple`.
874+
To specify a variable-length tuple of homogeneous type,
875+
use literal ellipsis, e.g. ``Tuple[int, ...]``. A plain :data:`Tuple`
876+
is equivalent to ``Tuple[Any, ...]``, and in turn to :class:`tuple`.
843877

844878
.. data:: Callable
845879

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

848882
The subscription syntax must always be used with exactly two
849883
values: the argument list and the return type. The argument list
850-
must be a list of types; the return type must be a single type.
884+
must be a list of types or an ellipsis; the return type must be
885+
a single type.
851886

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

0 commit comments

Comments
 (0)