From 461cdc077df3ab661fcf043023838654cc96afa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Wed, 15 Jun 2022 15:22:50 +0200 Subject: [PATCH] Fix ``undefined-variable`` for ``__class__`` in inner methods --- doc/whatsnew/2/2.14/full.rst | 3 +++ pylint/checkers/variables.py | 7 +++++-- .../functional/u/undefined/undefined_variable.py | 6 ++++++ .../u/undefined/undefined_variable.txt | 16 ++++++++-------- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/doc/whatsnew/2/2.14/full.rst b/doc/whatsnew/2/2.14/full.rst index 885be244e4..3bc3187930 100644 --- a/doc/whatsnew/2/2.14/full.rst +++ b/doc/whatsnew/2/2.14/full.rst @@ -10,6 +10,9 @@ Release date: TBA Closes #2270 +* Fixed false positive for ``undefined-variable`` for ``__class__`` in inner methods. + + Closes #4032 What's New in Pylint 2.14.2? ---------------------------- diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 1ab93ca91b..6fcb576c52 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -1418,8 +1418,11 @@ def _undefined_and_used_before_checker( or node.name in self.linter.config.additional_builtins or ( node.name == "__class__" - and isinstance(frame, nodes.FunctionDef) - and frame.is_method() + and any( + i.is_method() + for i in node.node_ancestors() + if isinstance(i, nodes.FunctionDef) + ) ) ) and not utils.node_ignores_exception(node, NameError) diff --git a/tests/functional/u/undefined/undefined_variable.py b/tests/functional/u/undefined/undefined_variable.py index 1336aa3577..c8fb541100 100644 --- a/tests/functional/u/undefined/undefined_variable.py +++ b/tests/functional/u/undefined/undefined_variable.py @@ -291,6 +291,12 @@ def method(self): # This name is not defined in the AST but it's present at runtime return __class__ + # It is also present in inner methods + def method_two(self): + def inner_method(): + return __class__ + + inner_method() def undefined_annotation(a:x): # [undefined-variable] if x == 2: # [used-before-assignment] diff --git a/tests/functional/u/undefined/undefined_variable.txt b/tests/functional/u/undefined/undefined_variable.txt index 0a73df8b20..1066dbda2c 100644 --- a/tests/functional/u/undefined/undefined_variable.txt +++ b/tests/functional/u/undefined/undefined_variable.txt @@ -29,11 +29,11 @@ undefined-variable:228:25:228:37:LambdaClass4.:Undefined variable 'Lambd undefined-variable:236:25:236:37:LambdaClass5.:Undefined variable 'LambdaClass5':UNDEFINED used-before-assignment:257:26:257:34:func_should_fail:Using variable 'datetime' before assignment:HIGH undefined-variable:284:18:284:24:not_using_loop_variable_accordingly:Undefined variable 'iteree':UNDEFINED -undefined-variable:295:27:295:28:undefined_annotation:Undefined variable 'x':UNDEFINED -used-before-assignment:296:7:296:8:undefined_annotation:Using variable 'x' before assignment:HIGH -undefined-variable:326:11:326:12:decorated3:Undefined variable 'x':UNDEFINED -undefined-variable:331:19:331:20:decorated4:Undefined variable 'y':UNDEFINED -undefined-variable:352:10:352:20:global_var_mixed_assignment:Undefined variable 'GLOBAL_VAR':HIGH -undefined-variable:364:19:364:44:RepeatedReturnAnnotations.x:Undefined variable 'RepeatedReturnAnnotations':UNDEFINED -undefined-variable:366:19:366:44:RepeatedReturnAnnotations.y:Undefined variable 'RepeatedReturnAnnotations':UNDEFINED -undefined-variable:368:19:368:44:RepeatedReturnAnnotations.z:Undefined variable 'RepeatedReturnAnnotations':UNDEFINED +undefined-variable:301:27:301:28:undefined_annotation:Undefined variable 'x':UNDEFINED +used-before-assignment:302:7:302:8:undefined_annotation:Using variable 'x' before assignment:HIGH +undefined-variable:332:11:332:12:decorated3:Undefined variable 'x':UNDEFINED +undefined-variable:337:19:337:20:decorated4:Undefined variable 'y':UNDEFINED +undefined-variable:358:10:358:20:global_var_mixed_assignment:Undefined variable 'GLOBAL_VAR':HIGH +undefined-variable:370:19:370:44:RepeatedReturnAnnotations.x:Undefined variable 'RepeatedReturnAnnotations':UNDEFINED +undefined-variable:372:19:372:44:RepeatedReturnAnnotations.y:Undefined variable 'RepeatedReturnAnnotations':UNDEFINED +undefined-variable:374:19:374:44:RepeatedReturnAnnotations.z:Undefined variable 'RepeatedReturnAnnotations':UNDEFINED