diff -r f889305d89a2 Doc/library/typing.rst --- a/Doc/library/typing.rst Sun Jan 08 00:37:32 2017 -0800 +++ b/Doc/library/typing.rst Sun Jan 08 16:11:44 2017 -0800 @@ -382,41 +382,41 @@ .. class:: TypeVar - Type variable. + Type variable. - Usage:: + Usage:: T = TypeVar('T') # Can be anything A = TypeVar('A', str, bytes) # Must be str or bytes - Type variables exist primarily for the benefit of static type - checkers. They serve as the parameters for generic types as well - as for generic function definitions. See class Generic for more - information on generic types. Generic functions work as follows:: + Type variables exist primarily for the benefit of static type + checkers. They serve as the parameters for generic types as well + as for generic function definitions. See class Generic for more + information on generic types. Generic functions work as follows:: - def repeat(x: T, n: int) -> Sequence[T]: - """Return a list containing n references to x.""" - return [x]*n + def repeat(x: T, n: int) -> Sequence[T]: + """Return a list containing n references to x.""" + return [x]*n - def longest(x: A, y: A) -> A: - """Return the longest of two strings.""" - return x if len(x) >= len(y) else y + def longest(x: A, y: A) -> A: + """Return the longest of two strings.""" + return x if len(x) >= len(y) else y - The latter example's signature is essentially the overloading - of ``(str, str) -> str`` and ``(bytes, bytes) -> bytes``. Also note - that if the arguments are instances of some subclass of :class:`str`, - the return type is still plain :class:`str`. + The latter example's signature is essentially the overloading + of ``(str, str) -> str`` and ``(bytes, bytes) -> bytes``. Also note + that if the arguments are instances of some subclass of :class:`str`, + the return type is still plain :class:`str`. - At runtime, ``isinstance(x, T)`` will raise :exc:`TypeError`. In general, - :func:`isinstance` and :func:`issubclass` should not be used with types. + At runtime, ``isinstance(x, T)`` will raise :exc:`TypeError`. In general, + :func:`isinstance` and :func:`issubclass` should not be used with types. - Type variables may be marked covariant or contravariant by passing - ``covariant=True`` or ``contravariant=True``. See :pep:`484` for more - details. By default type variables are invariant. Alternatively, - a type variable may specify an upper bound using ``bound=``. - This means that an actual type substituted (explicitly or implicitly) - for the type variable must be a subclass of the boundary type, - see :pep:`484`. + Type variables may be marked covariant or contravariant by passing + ``covariant=True`` or ``contravariant=True``. See :pep:`484` for more + details. By default type variables are invariant. Alternatively, + a type variable may specify an upper bound using ``bound=``. + This means that an actual type substituted (explicitly or implicitly) + for the type variable must be a subclass of the boundary type, + see :pep:`484`. .. class:: Generic @@ -483,37 +483,37 @@ .. class:: Iterable(Generic[T_co]) - A generic version of :class:`collections.abc.Iterable`. + A generic version of :class:`collections.abc.Iterable`. .. class:: Iterator(Iterable[T_co]) - A generic version of :class:`collections.abc.Iterator`. + A generic version of :class:`collections.abc.Iterator`. .. class:: Reversible(Iterable[T_co]) - A generic version of :class:`collections.abc.Reversible`. + A generic version of :class:`collections.abc.Reversible`. .. class:: SupportsInt - An ABC with one abstract method ``__int__``. + An ABC with one abstract method ``__int__``. .. class:: SupportsFloat - An ABC with one abstract method ``__float__``. + An ABC with one abstract method ``__float__``. .. class:: SupportsAbs - An ABC with one abstract method ``__abs__`` that is covariant - in its return type. + An ABC with one abstract method ``__abs__`` that is covariant + in its return type. .. class:: SupportsRound - An ABC with one abstract method ``__round__`` - that is covariant in its return type. + An ABC with one abstract method ``__round__`` + that is covariant in its return type. .. class:: Container(Generic[T_co]) - A generic version of :class:`collections.abc.Container`. + A generic version of :class:`collections.abc.Container`. .. class:: Hashable @@ -531,23 +531,23 @@ .. class:: AbstractSet(Sized, Collection[T_co]) - A generic version of :class:`collections.abc.Set`. + A generic version of :class:`collections.abc.Set`. .. class:: MutableSet(AbstractSet[T]) - A generic version of :class:`collections.abc.MutableSet`. + A generic version of :class:`collections.abc.MutableSet`. .. class:: Mapping(Sized, Collection[KT], Generic[VT_co]) - A generic version of :class:`collections.abc.Mapping`. + A generic version of :class:`collections.abc.Mapping`. .. class:: MutableMapping(Mapping[KT, VT]) - A generic version of :class:`collections.abc.MutableMapping`. + A generic version of :class:`collections.abc.MutableMapping`. .. class:: Sequence(Reversible[T_co], Collection[T_co]) - A generic version of :class:`collections.abc.Sequence`. + A generic version of :class:`collections.abc.Sequence`. .. class:: MutableSequence(Sequence[T]) @@ -627,7 +627,7 @@ .. class:: AsyncIterator(AsyncIterable[T_co]) - A generic version of :class:`collections.abc.AsyncIterator`. + A generic version of :class:`collections.abc.AsyncIterator`. .. class:: ContextManager(Generic[T_co]) @@ -716,13 +716,13 @@ Usage:: - class Employee(NamedTuple): - name: str - id: int + class Employee(NamedTuple): + name: str + id: int This is equivalent to:: - Employee = collections.namedtuple('Employee', ['name', 'id']) + Employee = collections.namedtuple('Employee', ['name', 'id']) The resulting class has one extra attribute: ``_field_types``, giving a dict mapping field names to types. (The field names