diff -r 2c124e30a324 Doc/library/typing.rst --- a/Doc/library/typing.rst Fri Aug 07 00:43:39 2015 -0700 +++ b/Doc/library/typing.rst Fri Aug 07 16:34:06 2015 +0200 @@ -67,7 +67,7 @@ overrides: Mapping[str, str]) -> None: ... Generics can be parametrized by using a new factory available in typing -called TypeVar. +called :class:`TypeVar`. .. code-block:: python @@ -111,7 +111,7 @@ `Generic[T]` as a base class defines that the class `LoggedVar` takes a single type parameter `T` . This also makes `T` valid as a type within the class body. -The `Generic` base class uses a metaclass that defines `__getitem__` so that +The :class:`Generic` base class uses a metaclass that defines `__getitem__` so that `LoggedVar[t]` is valid as a type:: from typing import Iterable @@ -132,7 +132,7 @@ class StrangePair(Generic[T, S]): ... -Each type variable argument to `Generic` must be distinct. +Each type variable argument to :class:`Generic` must be distinct. This is thus invalid:: from typing import TypeVar, Generic @@ -143,7 +143,7 @@ class Pair(Generic[T, T]): # INVALID ... -You can use multiple inheritance with `Generic`:: +You can use multiple inheritance with :class:`Generic`:: from typing import TypeVar, Generic, Sized @@ -152,6 +152,16 @@ class LinkedList(Sized, Generic[T]): ... +When inheriting from generic classes, some type variables could fixed:: + + from typing import TypeVar, Mapping + + T = TypeVar('T') + + class MyDict(Mapping[str, T]): + ... + +In this case `MyDict` has a single parameter, `T`. Subclassing a generic class without specifying type parameters assumes `Any` for each position. In the following example, `MyIterable` is not generic but implicitly inherits from `Iterable[Any]`:: @@ -160,32 +170,25 @@ class MyIterable(Iterable): # Same as Iterable[Any] -Generic metaclasses are not supported. +The metaclass used by :class:`Generic` is a subclass of :class:`abc.ABCMeta`. +A generic class can be an ABC by including abstract methods or properties, +and generic classes can also have ABCs as base classes without a metaclass +conflict. Generic metaclasses are not supported. The `Any` type -------------- -A special kind of type is `Any`. Every type is a subtype of `Any`. +A special kind of type is :class:`Any`. Every type is a subtype of :class:`Any`. This is also true for the builtin type object. However, to the static type checker these are completely different. -When the type of a value is `object`, the type checker will reject almost all +When the type of a value is :class:`object`, the type checker will reject almost all operations on it, and assigning it to a variable (or using it as a return value) of a more specialized type is a type error. On the other hand, when a value has -type `Any`, the type checker will allow all operations on it, and a value of -type `Any` can be assigned to a variable (or used as a return value) of a more +type :class:`Any`, the type checker will allow all operations on it, and a value of +type :class:`Any` can be assigned to a variable (or used as a return value) of a more constrained type. -Default argument values ------------------------ - -Use a literal ellipsis `...` to declare an argument as having a default value:: - - from typing import AnyStr - - def foo(x: AnyStr, y: AnyStr = ...) -> AnyStr: ... - - Classes, functions, and decorators ---------------------------------- @@ -233,7 +236,11 @@ 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. + 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 implictly) + for the type variable must be a subclass of the boundary type, + see :pep:`484`. .. class:: Union @@ -326,30 +333,59 @@ .. class:: Iterable(Generic[T_co]) + A generic version of the :class:`collections.abc.Iterable`. + .. class:: Iterator(Iterable[T_co]) + A generic version of the :class:`collections.abc.Iterator`. + .. class:: SupportsInt + An ABC with one abstract method `__int__`. + .. class:: SupportsFloat + An ABC with one abstract method `__float__`. + .. class:: SupportsAbs + 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. + .. class:: Reversible + An ABC with one abstract method `__reversed__` returning + an `Iterator[T_co]`. + .. class:: Container(Generic[T_co]) + A generic version of :class:`collections.abc.Container`. + .. class:: AbstractSet(Sized, Iterable[T_co], Container[T_co]) + A generic version of :class:`collections.abc.Set`. + .. class:: MutableSet(AbstractSet[T]) -.. class:: Mapping(Sized, Iterable[KT_co], Container[KT_co], Generic[KT_co, VT_co]) + A generic version of :class:`collections.abc.MutableSet`. + +.. class:: Mapping(Sized, Iterable[KT], Container[KT], Generic[VT_co]) + + A generic version of :class:`collections.abc.Mapping`. .. class:: MutableMapping(Mapping[KT, VT]) + A generic version of :class:`collections.abc.MutableMapping`. + .. class:: Sequence(Sized, Iterable[T_co], Container[T_co]) + A generic version of :class:`collections.abc.Sequence`. + .. class:: MutableSequence(Sequence[T]) .. class:: ByteString(Sequence[int])