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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
32 changes: 21 additions & 11 deletions pylint/checkers/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,7 @@ 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):
assign_attr = cast(astroid.AssignAttr, assign_attr)
if not is_attr_private(assign_attr.attrname):
continue

Expand All @@ -992,20 +993,29 @@ def _check_unused_private_attributes(self, node: astroid.ClassDef) -> None:
)

for attribute in node.nodes_of_class(astroid.Attribute):
if attribute.attrname == assign_attr.attrname and (
(
# If assigned to cls.attrib, can be accessed by cls/self
assign_attr.expr.name == "cls"
and attribute.expr.name in ("cls", "self")
)
attribute = cast(astroid.Attribute, attribute)
if attribute.attrname != assign_attr.attrname:
continue

if assign_attr.expr.name == "cls" and attribute.expr.name in (
"cls",
"self",
):
# If assigned to cls.attrib, can be accessed by cls/self
break

if (
assign_attr.expr.name in acceptable_obj_names
and attribute.expr.name == "self"
):
# If assigned to self.attrib, can only be accessed by self
# Or if __new__ was used, the returned object names are acceptable
or (
assign_attr.expr.name in acceptable_obj_names
and attribute.expr.name == "self"
)
):
break

if assign_attr.expr.name == attribute.expr.name == node.name:
# Recognise attributes which are accessed via the class name
break

else:
args = (node.name, assign_attr.attrname)
self.add_message("unused-private-member", node=assign_attr, args=args)
Expand Down
22 changes: 22 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,25 @@ def __inner_4(): # [unused-private-member]

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


# https://github.com/PyCQA/pylint/issues/4681
# Accessing attributes of the class using the class name should not result in a false positive
# as long as it is used within the class
class FalsePositive4681:
__instance = None
__should_cause_error = None # [unused-private-member]
@staticmethod
def instance():
if FalsePositive4681.__instance is None:
FalsePositive4681()
return FalsePositive4681.__instance

def __init__(self):
try:
FalsePositive4681.__instance = 42 # This should be fine
FalsePositive4681.__should_cause_error = True # [unused-private-member]
except Exception: # pylint: disable=broad-except
print("Error")
FalsePositive4681.__instance = False # This should be fine
FalsePositive4681.__should_cause_error = False # [unused-private-member]
3 changes: 3 additions & 0 deletions tests/functional/u/unused/unused_private_member.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ 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:211:4:FalsePositive4681:Unused private member `FalsePositive4681.__should_cause_error`:HIGH
unused-private-member:221:12:FalsePositive4681.__init__:Unused private member `FalsePositive4681.__should_cause_error`:HIGH
unused-private-member:225:12:FalsePositive4681.__init__:Unused private member `FalsePositive4681.__should_cause_error`:HIGH