diff options
-rw-r--r-- | doc/whatsnew/2/2.14/full.rst | 5 | ||||
-rw-r--r-- | pylint/checkers/variables.py | 7 | ||||
-rw-r--r-- | tests/functional/u/undefined/undefined_variable.py | 6 | ||||
-rw-r--r-- | tests/functional/u/undefined/undefined_variable.txt | 16 |
4 files changed, 24 insertions, 10 deletions
diff --git a/doc/whatsnew/2/2.14/full.rst b/doc/whatsnew/2/2.14/full.rst index 608664cbb..6475ec1f7 100644 --- a/doc/whatsnew/2/2.14/full.rst +++ b/doc/whatsnew/2/2.14/full.rst @@ -10,6 +10,11 @@ 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? ---------------------------- Release date: 2022-06-15 diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 7eeb1e854..9027b6588 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -1419,8 +1419,11 @@ class VariablesChecker(BaseChecker): 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 1336aa357..c8fb54110 100644 --- a/tests/functional/u/undefined/undefined_variable.py +++ b/tests/functional/u/undefined/undefined_variable.py @@ -291,6 +291,12 @@ class DunderClass: # 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 0a73df8b2..1066dbda2 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.<lambda>:Undefined variable 'Lambd undefined-variable:236:25:236:37:LambdaClass5.<lambda>: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 |