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

[Backport maintenance/3.0.x] Fix undefined-variable etc for Python 3.12 generic type syntax #9199

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/9193.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix false positives for ``undefined-variable`` and ``unused-argument`` for
classes and functions using Python 3.12 generic type syntax.

Closes #9193
12 changes: 9 additions & 3 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1738,6 +1738,9 @@ def _should_node_be_skipped(
elif consumer.scope_type == "function" and self._defined_in_function_definition(
node, consumer.node
):
if any(node.name == param.name.name for param in consumer.node.type_params):
return False

# If the name node is used as a function default argument's value or as
# a decorator, then start from the parent frame of the function instead
# of the function frame - and thus open an inner class scope
Expand Down Expand Up @@ -2262,10 +2265,13 @@ def _is_variable_violation(
isinstance(defframe, nodes.FunctionDef)
and frame is defframe
and defframe.parent_of(node)
and stmt is not defstmt
and (
defnode in defframe.type_params
# Single statement function, with the statement on the
# same line as the function definition
or stmt is not defstmt
)
):
# Single statement function, with the statement on the
# same line as the function definition
maybe_before_assign = False
elif (
isinstance(defstmt, NODES_WITH_VALUE_ATTR)
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/u/undefined/undefined_variable_py312.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# pylint: disable=missing-function-docstring,missing-module-docstring,missing-class-docstring,too-few-public-methods

def f[T](a: T) -> T:
print(a)

class ChildClass[T, *Ts, **P]:
...
2 changes: 2 additions & 0 deletions tests/functional/u/undefined/undefined_variable_py312.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[testoptions]
min_pyver=3.12