Skip to content

Commit

Permalink
Fix undefined-variable etc for Python 3.12 generic type syntax (#9195
Browse files Browse the repository at this point in the history
…) (#9199)

(cherry picked from commit c648f16)

Co-authored-by: Jacob Walls <[email protected]>
  • Loading branch information
github-actions[bot] and jacobtylerwalls authored Oct 29, 2023
1 parent efee961 commit 53d4541
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
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

0 comments on commit 53d4541

Please sign in to comment.