Skip to content

Commit

Permalink
Fix false positive unused-private-member for accessing attributes…
Browse files Browse the repository at this point in the history
… in a class using ``cls`` (#4965)

* Fix false positive ``unused-private-member`` for accessing attributes in
a class using ``cls``

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
  • Loading branch information
yushao2 and Pierre-Sassoulas committed Sep 14, 2021
1 parent af407c7 commit 272596b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ Release date: TBA

Closes #4907

* Fix false positive ``unused-private-member`` for accessing attributes in a class using ``cls``

Closes #4849

* Fix false positive ``unused-private-member`` for private staticmethods accessed in classmethods.

Closes #4849
Expand Down
4 changes: 4 additions & 0 deletions doc/whatsnew/2.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ Other Changes

Closes #4907

* Fix false positive ``unused-private-member`` for accessing attributes in a class using ``cls``

Closes #4849

* Extended ``consider-using-in`` check to work for attribute access.

* Setting ``min-similarity-lines`` to 0 now makes the similarty checker stop checking for duplicate code
Expand Down
14 changes: 9 additions & 5 deletions pylint/checkers/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,11 +1003,15 @@ def _check_unused_private_attributes(self, node: nodes.ClassDef) -> None:
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
if (
assign_attr.expr.name
in [
"cls",
node.name,
]
and attribute.expr.name in ["cls", "self", node.name]
):
# If assigned to cls or class name, can be accessed by cls/self/class name
break

if (
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 @@ -242,6 +242,25 @@ def __init__(self):
FalsePositive4681.__instance = False # This should be fine
FalsePositive4681.__should_cause_error = False # [unused-private-member]

# Accessing attributes of the class using `cls` should not result in a false positive
# as long as it is used within the class
class FalsePositive4681b:
__instance = None

@classmethod # Use class method here
def instance(cls):
if cls.__instance is None:
cls()
return cls.__instance

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


# https://github.com/PyCQA/pylint/issues/4849
# Accessing private static methods from classmethods via `cls` should not result in a
# false positive
Expand Down
6 changes: 3 additions & 3 deletions tests/functional/u/unused/unused_private_member.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ unused-private-member:212:8:Crash4755Context.__init__:Unused private member `Cra
unused-private-member:229:4:FalsePositive4681:Unused private member `FalsePositive4681.__should_cause_error`:HIGH
unused-private-member:239:12:FalsePositive4681.__init__:Unused private member `FalsePositive4681.__should_cause_error`:HIGH
unused-private-member:243:12:FalsePositive4681.__init__:Unused private member `FalsePositive4681.__should_cause_error`:HIGH
unused-private-member:255:4:FalsePositive4849.__unused_private_method:Unused private member `FalsePositive4849.__unused_private_method()`:HIGH
unused-private-member:272:4:Pony.__init_defaults:Unused private member `Pony.__init_defaults(self)`:HIGH
unused-private-member:277:4:Pony.__get_fur_color:Unused private member `Pony.__get_fur_color(self)`:HIGH
unused-private-member:274:4:FalsePositive4849.__unused_private_method:Unused private member `FalsePositive4849.__unused_private_method()`:HIGH
unused-private-member:291:4:Pony.__init_defaults:Unused private member `Pony.__init_defaults(self)`:HIGH
unused-private-member:296:4:Pony.__get_fur_color:Unused private member `Pony.__get_fur_color(self)`:HIGH

0 comments on commit 272596b

Please sign in to comment.