Improve wording on covariance of Type[] (Ivan L, #107).

This commit is contained in:
Guido van Rossum 2016-05-20 11:33:13 -07:00
parent e70675917d
commit 9fd8df6d0c
1 changed files with 8 additions and 3 deletions

View File

@ -372,7 +372,7 @@ takes a single type parameter ``T``. This also makes ``T`` valid as
a type within the class body. a type within the class body.
The ``Generic`` base class uses a metaclass that defines ``__getitem__`` The ``Generic`` base class uses a metaclass that defines ``__getitem__``
so that e.g. ``LoggedVar[int]`` is valid as a type:: so that ``LoggedVar[t]`` is valid as a type::
from typing import Iterable from typing import Iterable
@ -914,6 +914,7 @@ Note that it is legal to use a union of classes as the parameter for
``Type[]``, as in:: ``Type[]``, as in::
def new_non_team_user(user_class: Type[Union[BasicUser, ProUser]]): def new_non_team_user(user_class: Type[Union[BasicUser, ProUser]]):
user = new_user(user_class)
... ...
However the actual argument passed in at runtime must still be a However the actual argument passed in at runtime must still be a
@ -952,8 +953,12 @@ attributes and methods defined by ``type`` (for example,
``__repr__()`` and ``__mro__``). Such a variable can be called with ``__repr__()`` and ``__mro__``). Such a variable can be called with
arbitrary arguments, and the return type is ``Any``. arbitrary arguments, and the return type is ``Any``.
``Type[T]`` should be considered covariant, since for a concrete class ``Type`` is covariant in its parameter, because ``Type[Derived]`` is a
``C``, ``Type[C]`` matches ``C`` and any of its subclasses. subtype of ``Type[Base]``::
def new_pro_user(pro_user_class: Type[ProUser]):
user = new_user(pro_user_class) # OK
...
Version and platform checking Version and platform checking