Skip to content

Commit 25b210d

Browse files
nuglifeleojiLeo Ji
andauthored
docs: clarify that invariant-by-default applies to legacy TypeVar syntax (#21108)
Closes #20366. The "invariant by default" rule applies to the legacy `TypeVar` syntax. Under PEP 695 (`class MyClass[T]: ...`), mypy infers variance from usage instead. Updated the wording in `common_issues.rst` to make this distinction clear. --------- Co-authored-by: Leo Ji <nuglifeleoji@gmail.com>
1 parent ef7e8a6 commit 25b210d

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

docs/source/common_issues.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,13 @@ See :ref:`type-narrowing` for more information.
303303
Invariance vs covariance
304304
------------------------
305305

306-
Most mutable generic collections are invariant, and mypy considers all
307-
user-defined generic classes invariant by default
308-
(see :ref:`variance-of-generics` for motivation). This could lead to some
306+
Most mutable generic collections are invariant. When using the legacy
307+
``TypeVar`` syntax, mypy considers all user-defined generic classes invariant
308+
by default (see :ref:`variance-of-generics` for motivation). When using the
309+
:pep:`695` syntax (``class MyClass[T]: ...``), variance is inferred from
310+
usage rather than defaulting to invariant.
311+
312+
The fact that mutable sequences are usually invariant can lead to some
309313
unexpected errors when combined with type inference. For example:
310314

311315
.. code-block:: python

0 commit comments

Comments
 (0)