Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix undefined-variable for __class__ in inner methods #6957

Merged
merged 1 commit into from Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/2/2.14/full.rst
Expand Up @@ -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?
----------------------------
Expand Down
7 changes: 5 additions & 2 deletions pylint/checkers/variables.py
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions tests/functional/u/undefined/undefined_variable.py
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
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