From 12caa36456fb1904b4096a1fe62293b83335b118 Mon Sep 17 00:00:00 2001 From: dhanusharer Date: Wed, 4 Mar 2026 17:16:02 +0530 Subject: [PATCH] Fix docs: clarify variance behavior for PEP 695 generics --- docs/source/common_issues.rst | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/docs/source/common_issues.rst b/docs/source/common_issues.rst index 31141ad383e1..7dd674bfd154 100644 --- a/docs/source/common_issues.rst +++ b/docs/source/common_issues.rst @@ -278,9 +278,7 @@ multiple variables (or maybe declare the variable with an ``Any`` type). ... n = 'x' # error: Incompatible types in assignment (expression has type "str", variable has type "int") -.. note:: - - Using the :option:`--allow-redefinition ` +.. note:: Using the :option:`--allow-redefinition ` flag can suppress this error in several cases. Note that you can redefine a variable with a more *precise* or a more @@ -301,9 +299,15 @@ See :ref:`type-narrowing` for more information. Invariance vs covariance ------------------------ +Most mutable generic collections are invariant. With the older +``TypeVar`` syntax (for example ``T = TypeVar("T")``), user-defined +generic classes are invariant by default. + +With the newer PEP 695 syntax (for example ``class MyCls[T]:``), +type parameters may be inferred as covariant if they are not used +in positions that require invariance. + -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: