Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't emit incorrect-variance for type parameters (PEP 695) #9638

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions doc/whatsnew/fragments/9638.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Don't emit ``typevar-name-incorrect-variance`` warnings for PEP 695 style TypeVars.
The variance is inferred automatically by the type checker.
Adding ``_co`` or ``_contra`` suffix can help to reason about TypeVar.

Refs #9638
11 changes: 9 additions & 2 deletions pylint/checkers/base/name_checker/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class TypeVarVariance(Enum):
covariant = auto()
contravariant = auto()
double_variant = auto()
inferred = auto()


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

def _check_typevar(self, name: str, node: nodes.AssignName) -> None:
"""Check for TypeVar lint violations."""
variance: TypeVarVariance = TypeVarVariance.invariant
if isinstance(node.parent, nodes.Assign):
keywords = node.assign_type().value.keywords
args = node.assign_type().value.args
Expand All @@ -634,8 +636,8 @@ def _check_typevar(self, name: str, node: nodes.AssignName) -> None:
else: # PEP 695 generic type nodes
keywords = ()
args = ()
variance = TypeVarVariance.inferred

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

if variance == TypeVarVariance.double_variant:
if variance == TypeVarVariance.inferred:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if variance == TypeVarVariance.inferred:
if variance is TypeVarVariance.inferred:

Copy link
Member Author

Choose a reason for hiding this comment

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

All other comparisons with TypeVarVariance also use the equals sign.
Let's change it when we implement #5356, maybe?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Fine!

# Ignore variance check for PEP 695 type parameters.
# The variance is inferred by the type checker.
# Adding _co or _contra suffix can help to reason about TypeVar.
pass
elif variance == TypeVarVariance.double_variant:
cdce8p marked this conversation as resolved.
Show resolved Hide resolved
self.add_message(
"typevar-double-variance",
node=node,
Expand Down
6 changes: 6 additions & 0 deletions tests/functional/t/type/typevar_naming_style_py312.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
"""PEP 695 generic typing nodes"""

from collections.abc import Sequence

type Point[T] = tuple[T, ...]
type Point[t] = tuple[t, ...] # [invalid-name]

# Don't report typevar-name-incorrect-variance for type parameter
# The variance is determined by the type checker
type Array[T_co] = Sequence[T_co]
2 changes: 1 addition & 1 deletion tests/functional/t/type/typevar_naming_style_py312.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
invalid-name:4:11:4:12::"Type variable name ""t"" doesn't conform to predefined naming style":HIGH
invalid-name:6:11:6:12::"Type variable name ""t"" doesn't conform to predefined naming style":HIGH
Loading