Skip to content

Commit 53d4541

Browse files
Fix undefined-variable etc for Python 3.12 generic type syntax (#9195) (#9199)
(cherry picked from commit c648f16) Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
1 parent efee961 commit 53d4541

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix false positives for ``undefined-variable`` and ``unused-argument`` for
2+
classes and functions using Python 3.12 generic type syntax.
3+
4+
Closes #9193

pylint/checkers/variables.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,9 @@ def _should_node_be_skipped(
17381738
elif consumer.scope_type == "function" and self._defined_in_function_definition(
17391739
node, consumer.node
17401740
):
1741+
if any(node.name == param.name.name for param in consumer.node.type_params):
1742+
return False
1743+
17411744
# If the name node is used as a function default argument's value or as
17421745
# a decorator, then start from the parent frame of the function instead
17431746
# of the function frame - and thus open an inner class scope
@@ -2262,10 +2265,13 @@ def _is_variable_violation(
22622265
isinstance(defframe, nodes.FunctionDef)
22632266
and frame is defframe
22642267
and defframe.parent_of(node)
2265-
and stmt is not defstmt
2268+
and (
2269+
defnode in defframe.type_params
2270+
# Single statement function, with the statement on the
2271+
# same line as the function definition
2272+
or stmt is not defstmt
2273+
)
22662274
):
2267-
# Single statement function, with the statement on the
2268-
# same line as the function definition
22692275
maybe_before_assign = False
22702276
elif (
22712277
isinstance(defstmt, NODES_WITH_VALUE_ATTR)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# pylint: disable=missing-function-docstring,missing-module-docstring,missing-class-docstring,too-few-public-methods
2+
3+
def f[T](a: T) -> T:
4+
print(a)
5+
6+
class ChildClass[T, *Ts, **P]:
7+
...
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[testoptions]
2+
min_pyver=3.12

0 commit comments

Comments
 (0)