|
11 | 11 | -------------- |
12 | 12 |
|
13 | 13 | 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`, |
15 | 15 | :data:`Tuple`, :data:`Callable`, :class:`TypeVar`, and |
16 | 16 | :class:`Generic`. For full specification please see :pep:`484`. For |
17 | 17 | a simplified introduction to type hints see :pep:`483`. |
@@ -274,6 +274,22 @@ not generic but implicitly inherits from ``Iterable[Any]``:: |
274 | 274 |
|
275 | 275 | class MyIterable(Iterable): # Same as Iterable[Any] |
276 | 276 |
|
| 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 | + |
277 | 293 | The metaclass used by :class:`Generic` is a subclass of :class:`abc.ABCMeta`. |
278 | 294 | A generic class can be an ABC by including abstract methods or properties, |
279 | 295 | 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: |
588 | 604 |
|
589 | 605 | A generic version of :class:`collections.abc.Awaitable`. |
590 | 606 |
|
| 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 | + |
591 | 620 | .. class:: AsyncIterable(Generic[T_co]) |
592 | 621 |
|
593 | 622 | A generic version of :class:`collections.abc.AsyncIterable`. |
@@ -721,13 +750,18 @@ The module defines the following classes, functions and decorators: |
721 | 750 | runtime we intentionally don't check anything (we want this |
722 | 751 | to be as fast as possible). |
723 | 752 |
|
724 | | -.. function:: get_type_hints(obj) |
| 753 | +.. function:: get_type_hints(obj[, globals[, locals]) |
725 | 754 |
|
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. |
727 | 757 |
|
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. |
731 | 765 |
|
732 | 766 | .. decorator:: overload |
733 | 767 |
|
@@ -830,24 +864,25 @@ The module defines the following classes, functions and decorators: |
830 | 864 |
|
831 | 865 | .. data:: Tuple |
832 | 866 |
|
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. |
835 | 869 |
|
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. |
839 | 873 |
|
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`. |
843 | 877 |
|
844 | 878 | .. data:: Callable |
845 | 879 |
|
846 | 880 | Callable type; ``Callable[[int], str]`` is a function of (int) -> str. |
847 | 881 |
|
848 | 882 | The subscription syntax must always be used with exactly two |
849 | 883 | 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. |
851 | 886 |
|
852 | 887 | There is no syntax to indicate optional or keyword arguments; |
853 | 888 | such function types are rarely used as callback types. |
|
0 commit comments