Skip to content

Commit

Permalink
Fix crash for unused-private-member when there are nested attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
yushao2 committed Aug 1, 2021
1 parent 2ad8247 commit 49c4bba
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Expand Up @@ -60,6 +60,10 @@ Release date: TBA

Closes #4673

* Fix crash for ``unused-private-member`` that occurs when there is a presence of nested attributes

Closes #4755


What's New in Pylint 2.9.6?
===========================
Expand Down
5 changes: 3 additions & 2 deletions pylint/checkers/classes.py
Expand Up @@ -974,9 +974,10 @@ def _check_unused_private_variables(self, node: astroid.ClassDef) -> None:

def _check_unused_private_attributes(self, node: astroid.ClassDef) -> None:
for assign_attr in node.nodes_of_class(astroid.AssignAttr):
if not is_attr_private(assign_attr.attrname):
if not is_attr_private(assign_attr.attrname) or not isinstance(
assign_attr.expr, astroid.Name
):
continue

# Logic for checking false positive when using __new__,
# Get the returned object names of the __new__ magic function
# Then check if the attribute was consumed in other instance methods
Expand Down
16 changes: 16 additions & 0 deletions tests/functional/u/unused/unused_private_member.py
Expand Up @@ -201,3 +201,19 @@ def __inner_4(): # [unused-private-member]

fn_to_return = __inner_1 if flag else __inner_3(__inner_2)
return fn_to_return


# Test cases for crash reported in #4755
# https://github.com/PyCQA/pylint/issues/4755
class Crash4755Context:
def __init__(self):
self.__messages = None # [unused-private-member]

class Crash4755Command:
def __init__(self):
self.context = Crash4755Context()

def method(self):
self.context.__messages = []
for message in self.context.__messages:
print(message)
1 change: 1 addition & 0 deletions tests/functional/u/unused/unused_private_member.txt
Expand Up @@ -9,3 +9,4 @@ unused-private-member:132:8:FalsePositive4657.__init__:Unused private member `Fa
undefined-variable:137:15:FalsePositive4657.attr_c:Undefined variable 'cls':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
unused-private-member:210:8:Crash4755Context.__init__:Unused private member `Crash4755Context.__messages`:HIGH

0 comments on commit 49c4bba

Please sign in to comment.