Skip to content

Commit

Permalink
Update pr based on review
Browse files Browse the repository at this point in the history
  • Loading branch information
yushao2 committed Aug 1, 2021
1 parent e4d6243 commit 4ffea0b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
11 changes: 7 additions & 4 deletions pylint/checkers/classes.py
Expand Up @@ -912,9 +912,8 @@ def _check_unused_private_functions(self, node: astroid.ClassDef) -> None:
parent_scope = function_def.parent.scope()
if isinstance(parent_scope, astroid.FunctionDef):
# Handle nested functions
outer_fn = parent_scope
if function_def.name in (
n.name for n in outer_fn.nodes_of_class(astroid.Name)
n.name for n in parent_scope.nodes_of_class(astroid.Name)
):
continue
for attribute in node.nodes_of_class(astroid.Attribute):
Expand All @@ -940,19 +939,23 @@ def _check_unused_private_functions(self, node: astroid.ClassDef) -> None:
break
else:
name_stack = []
outer_level_names = ""
curr = parent_scope
# Generate proper names for nested functions
while curr != node:
name_stack.append(curr.name)
curr = curr.parent.scope()

function_repr = f"{function_def.name}({function_def.args.as_string()})"
if name_stack:
outer_level_names = f"{'.'.join(reversed(name_stack))}."

function_repr = f"{outer_level_names}{function_def.name}({function_def.args.as_string()})"
self.add_message(
"unused-private-member",
node=function_def,
args=(
node.name,
f"{'.'.join(reversed(name_stack))}{'.' if name_stack else ''}{function_repr}",
function_repr,
),
)

Expand Down
6 changes: 2 additions & 4 deletions tests/functional/u/unused/unused_private_member.py
Expand Up @@ -136,7 +136,6 @@ def attr_c(self):
"""Get c."""
return cls.__attr_c # [undefined-variable]

<<<<<<< HEAD

# Test cases for false-positive reported in #4668
# https://github.com/PyCQA/pylint/issues/4668
Expand All @@ -161,10 +160,10 @@ def __new__(cls, func, *args):
def exec(self):
print(self.__secret_bool)
return self.func(*self.__args)
=======


# Test cases for false-positive reported in #4673
# https://github.com/PyCQA/pylint/issues/4673

class FalsePositive4673:
""" The testing class """

Expand Down Expand Up @@ -202,4 +201,3 @@ def __inner_4(): # [unused-private-member]

fn_to_return = __inner_1 if flag else __inner_3(__inner_2)
return fn_to_return
>>>>>>> 82fd4e90... [unused-private-member] add logic for checking nested functions
4 changes: 2 additions & 2 deletions tests/functional/u/unused/unused_private_member.txt
Expand Up @@ -7,5 +7,5 @@ unused-private-member:34:4:HasUnusedInClass.__test:Unused private member `HasUnu
unused-private-member:55:4:HasUnusedInClass.__test_recursive:Unused private member `HasUnusedInClass.__test_recursive(self)`:HIGH
unused-private-member:132:8:FalsePositive4657.__init__:Unused private member `FalsePositive4657.__attr_c`:HIGH
undefined-variable:137:15:FalsePositive4657.attr_c:Undefined variable 'cls':HIGH
unused-private-member:154:8:FalsePositive4673.do_thing.__true_positive:Unused private member `FalsePositive4673.do_thing.__true_positive(in_thing)`:HIGH
unused-private-member:174:8:FalsePositive4673.complicated_example.__inner_4:Unused private member `FalsePositive4673.complicated_example.__inner_4()`:HIGH
unused-private-member:179:8:FalsePositive4673.do_thing.__true_positive:Unused private member `FalsePositive4673.do_thing.__true_positive(in_thing)`:HIGH
unused-private-member:199:8:FalsePositive4673.complicated_example.__inner_4:Unused private member `FalsePositive4673.complicated_example.__inner_4()`:HIGH

0 comments on commit 4ffea0b

Please sign in to comment.