Typos, rst formatting and a minor addition to PEP 483 (Ivan L, upstream #227)

This commit is contained in:
Guido van Rossum 2016-06-16 17:08:10 -07:00
parent ac8459d47b
commit 92b5b19f66
1 changed files with 9 additions and 4 deletions

View File

@ -261,7 +261,7 @@ the runtime classes.* Examples:
Typing interface is implemented with classes, i.e., at runtime it is possible
to evaluate, e.g., ``Generic[T].__bases__``. But to emphasize the distinction
between classes and types the following general rules:
between classes and types the following general rules apply:
- No types defined below (i.e. ``Any``, ``Union``, etc.) can be instantiated,
an attempt to do so will raise ``TypeError``.
@ -496,7 +496,7 @@ Defining and using generic types
--------------------------------
Users can declare their classes as generic types using
the special building block ``Generic``.
the special building block ``Generic``. The definition
``class MyGeneric(Generic[X, Y, ...]): ...`` defines a generic type
``MyGeneric`` over type variables ``X``, etc. ``MyGeneric`` itself becomes
parameterizable, e.g. ``MyGeneric[int, str, ...]`` is a specific type with
@ -531,6 +531,10 @@ not generic. Examples::
def search(urls: URLList) -> Optional[bytes] # URLList is not generic
...
Subclassing a generic type imposes the subtype relation on the corresponding
specific types, so that ``TodoList[t1]`` is a subtype of ``Iterable[t1]``
in the above example.
Generic types can be specialized (indexed) in several steps.
Every type variable could be substituted by a specific type
or by another generic type. If ``Generic`` appears in the base class list,
@ -677,7 +681,8 @@ Types are invariant by default. Examples::
class Sink(Generic[T_contra]): # this type is declared contravariant
def send_to_nowhere(self, data: T_contra) -> None:
print(data, file=os.devnull)
with open(os.devnull, 'w') as devnull:
print(data, file=devnull)
Note, that although the variance is defined via type variables, it is not
a property of type variables, but a property of generic types.