Skip to content

Commit

Permalink
Fix undefined-variable for __class__ in inner methods (#6957)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord authored and Pierre-Sassoulas committed Jun 18, 2022
1 parent b9ecb4d commit fb6be59
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
5 changes: 5 additions & 0 deletions doc/whatsnew/2/2.14/full.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1419,8 +1419,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)
Expand Down
6 changes: 6 additions & 0 deletions tests/functional/u/undefined/undefined_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
16 changes: 8 additions & 8 deletions tests/functional/u/undefined/undefined_variable.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fb6be59

Please sign in to comment.