Skip to content

Commit b2ea316

Browse files
[Backport maintenance/3.2.x] Don't emit incorrect-variance for type parameters (PEP 695) (#9640)
Backport fbc1ed3 from #9638.
2 parents ee6b62f + cdcc509 commit b2ea316

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Don't emit ``typevar-name-incorrect-variance`` warnings for PEP 695 style TypeVars.
2+
The variance is inferred automatically by the type checker.
3+
Adding ``_co`` or ``_contra`` suffix can help to reason about TypeVar.
4+
5+
Refs #9638

pylint/checkers/base/name_checker/checker.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class TypeVarVariance(Enum):
6060
covariant = auto()
6161
contravariant = auto()
6262
double_variant = auto()
63+
inferred = auto()
6364

6465

6566
def _get_properties(config: argparse.Namespace) -> tuple[set[str], set[str]]:
@@ -623,6 +624,7 @@ def _assigns_typealias(node: nodes.NodeNG | None) -> bool:
623624

624625
def _check_typevar(self, name: str, node: nodes.AssignName) -> None:
625626
"""Check for TypeVar lint violations."""
627+
variance: TypeVarVariance = TypeVarVariance.invariant
626628
if isinstance(node.parent, nodes.Assign):
627629
keywords = node.assign_type().value.keywords
628630
args = node.assign_type().value.args
@@ -634,8 +636,8 @@ def _check_typevar(self, name: str, node: nodes.AssignName) -> None:
634636
else: # PEP 695 generic type nodes
635637
keywords = ()
636638
args = ()
639+
variance = TypeVarVariance.inferred
637640

638-
variance = TypeVarVariance.invariant
639641
name_arg = None
640642
for kw in keywords:
641643
if variance == TypeVarVariance.double_variant:
@@ -659,7 +661,12 @@ def _check_typevar(self, name: str, node: nodes.AssignName) -> None:
659661
if name_arg is None and args and isinstance(args[0], nodes.Const):
660662
name_arg = args[0].value
661663

662-
if variance == TypeVarVariance.double_variant:
664+
if variance == TypeVarVariance.inferred:
665+
# Ignore variance check for PEP 695 type parameters.
666+
# The variance is inferred by the type checker.
667+
# Adding _co or _contra suffix can help to reason about TypeVar.
668+
pass
669+
elif variance == TypeVarVariance.double_variant:
663670
self.add_message(
664671
"typevar-double-variance",
665672
node=node,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
"""PEP 695 generic typing nodes"""
22

3+
from collections.abc import Sequence
4+
35
type Point[T] = tuple[T, ...]
46
type Point[t] = tuple[t, ...] # [invalid-name]
7+
8+
# Don't report typevar-name-incorrect-variance for type parameter
9+
# The variance is determined by the type checker
10+
type Array[T_co] = Sequence[T_co]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
invalid-name:4:11:4:12::"Type variable name ""t"" doesn't conform to predefined naming style":HIGH
1+
invalid-name:6:11:6:12::"Type variable name ""t"" doesn't conform to predefined naming style":HIGH

0 commit comments

Comments
 (0)