Skip to content

Commit

Permalink
Fix false-positive of unused-private-member with class name
Browse files Browse the repository at this point in the history
  • Loading branch information
yushao2 committed Aug 1, 2021
1 parent 2ad8247 commit f46af19
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ Release date: TBA

Closes #4673

* Fix a false positive for ``unused-private-member`` with class names

Closes #4681


What's New in Pylint 2.9.6?
===========================
Expand Down
2 changes: 2 additions & 0 deletions pylint/checkers/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,7 @@ def _check_unused_private_attributes(self, node: astroid.ClassDef) -> None:
)

for attribute in node.nodes_of_class(astroid.Attribute):
# pylint: disable=too-many-boolean-expressions
if attribute.attrname == assign_attr.attrname and (
(
# If assigned to cls.attrib, can be accessed by cls/self
Expand All @@ -1004,6 +1005,7 @@ def _check_unused_private_attributes(self, node: astroid.ClassDef) -> None:
assign_attr.expr.name in acceptable_obj_names
and attribute.expr.name == "self"
)
or (assign_attr.expr.name == attribute.expr.name == node.name)
):
break
else:
Expand Down
19 changes: 19 additions & 0 deletions tests/functional/u/unused/unused_private_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,22 @@ 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 false-positive reported in #4681
# https://github.com/PyCQA/pylint/issues/4681
class FalsePositive4681:
__instance = None

@staticmethod
def instance():
if FalsePositive4681.__instance is None:
FalsePositive4681()
return FalsePositive4681.__instance

def __init__(self):
try:
FalsePositive4681.__instance = 42 # This should be fine
except Exception: # pylint: disable=broad-except
print("Error")
FalsePositive4681.__instance = False # This should be fine

0 comments on commit f46af19

Please sign in to comment.