Skip to content
Merged
Changes from 1 commit
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: 6 additions & 4 deletions docs/source/common_issues.rst
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,12 @@ 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
unexpected errors when combined with type inference. For example:
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. This could lead to some unexpected
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last sentence reads oddly now, I think "this" refers to the fact that mutable sequences are invariant, which we mentioned several sentences ago. I would start a new paragraph here with something like "The fact that mutable sequences are usually invariant can lead to unexpected errors..."

errors when combined with type inference. For example:

.. code-block:: python

Expand Down
Loading