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