|
11 | 11 | -------------- |
12 | 12 |
|
13 | 13 | 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`, |
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 |
@@ -582,6 +598,19 @@ The module defines the following classes, functions and decorators: |
582 | 598 |
|
583 | 599 | A generic version of :class:`collections.abc.Awaitable`. |
584 | 600 |
|
| 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 | + |
585 | 614 | .. class:: AsyncIterable(Generic[T_co]) |
586 | 615 |
|
587 | 616 | A generic version of :class:`collections.abc.AsyncIterable`. |
@@ -700,13 +729,18 @@ The module defines the following classes, functions and decorators: |
700 | 729 | runtime we intentionally don't check anything (we want this |
701 | 730 | to be as fast as possible). |
702 | 731 |
|
703 | | -.. function:: get_type_hints(obj) |
| 732 | +.. function:: get_type_hints(obj[, globals[, locals]) |
704 | 733 |
|
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. |
706 | 736 |
|
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. |
710 | 744 |
|
711 | 745 | .. decorator:: overload |
712 | 746 |
|
@@ -809,24 +843,25 @@ The module defines the following classes, functions and decorators: |
809 | 843 |
|
810 | 844 | .. data:: Tuple |
811 | 845 |
|
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. |
814 | 848 |
|
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. |
818 | 852 |
|
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`. |
822 | 856 |
|
823 | 857 | .. data:: Callable |
824 | 858 |
|
825 | 859 | Callable type; ``Callable[[int], str]`` is a function of (int) -> str. |
826 | 860 |
|
827 | 861 | The subscription syntax must always be used with exactly two |
828 | 862 | 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. |
830 | 865 |
|
831 | 866 | There is no syntax to indicate optional or keyword arguments; |
832 | 867 | such function types are rarely used as callback types. |
|
0 commit comments