Skip to content

Commit 5863242

Browse files
author
Leo Ji
committed
docs: clarify that invariant-by-default applies to legacy TypeVar syntax
Under PEP 695 class syntax variance is inferred, not invariant by default. Closes #20366. Made-with: Cursor
1 parent 9790459 commit 5863242

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

docs/source/common_issues.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,12 @@ 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
309-
unexpected errors when combined with type inference. For example:
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. This could lead to some unexpected
311+
errors when combined with type inference. For example:
310312

311313
.. code-block:: python
312314

0 commit comments

Comments
 (0)