Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions docs/source/common_issues.rst
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,13 @@ See :ref:`type-narrowing` for more information.
Invariance vs covariance
------------------------

Most mutable generic collections are invariant, and mypy considers all
user-defined generic classes invariant by default
(see :ref:`variance-of-generics` for motivation). This could lead to some
Most mutable generic collections are invariant. When using the legacy
``TypeVar`` syntax, mypy considers all user-defined generic classes invariant
by default (see :ref:`variance-of-generics` for motivation). When using the
:pep:`695` syntax (``class MyClass[T]: ...``), variance is inferred from
usage rather than defaulting to invariant.

The fact that mutable sequences are usually invariant can lead to some
unexpected errors when combined with type inference. For example:

.. code-block:: python
Expand Down
Loading