Skip to content

Commit 5310f7e

Browse files
committed
Fix invariance statement and add missing imports in variance examples
1 parent 5f15d2e commit 5310f7e

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

‎docs/reference/generics.rst‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,12 @@ Let us illustrate this by few simple examples:
335335

336336
.. code-block:: python
337337
338-
# We'll use these classes in the examples below
339-
class Shape: ...
340-
class Triangle(Shape): ...
341-
class Square(Shape): ...
338+
from typing import Sequence, Union, Callable
339+
340+
# We'll use these classes in the examples below
341+
class Shape: ...
342+
class Triangle(Shape): ...
343+
class Square(Shape): ...
342344
343345
* Most immutable containers, such as :py:class:`~typing.Sequence` and
344346
:py:class:`~typing.FrozenSet` are covariant. :py:data:`~typing.Union` is
@@ -405,7 +407,9 @@ Let us illustrate this by few simple examples:
405407
Another example of an invariant type is :py:class:`~typing.Dict`. Most mutable containers
406408
are invariant.
407409

408-
By default, all user-defined generics are invariant.
410+
The variance of user-defined generics depends on how type parameters
411+
are used. Explicit variance annotations are only required when using
412+
TypeVar.
409413
To declare a given generic class as covariant or contravariant use
410414
type variables defined with special keyword arguments ``covariant`` or
411415
``contravariant``. For example:

0 commit comments

Comments
 (0)