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 false-positive of unused-private-member with class name #4782

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
cdce8p marked this conversation as resolved.
Show resolved Hide resolved
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)
cdce8p marked this conversation as resolved.
Show resolved Hide resolved
):
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
cdce8p marked this conversation as resolved.
Show resolved Hide resolved
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