Skip to content

Commit

Permalink
Fix false positive unused-private-member for accessing attributes in
Browse files Browse the repository at this point in the history
a class using ``cls``
  • Loading branch information
yushao2 committed Sep 4, 2021
1 parent 6c81831 commit 76ac95e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 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


What's New in Pylint 2.10.3?
============================
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 @@ -69,3 +69,7 @@ Other Changes
* Fix false positive ``superfluous-parens`` for tuples created with inner tuples

Closes #4907

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

Closes #4849
8 changes: 4 additions & 4 deletions pylint/checkers/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,11 +1000,11 @@ 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 [
if assign_attr.expr.name in [
"cls",
"self",
]:
# If assigned to cls.attrib, can be accessed by cls/self
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
17 changes: 17 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,23 @@ 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

class Pony:
"""https://github.com/PyCQA/pylint/issues/4837"""
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/u/unused/unused_private_member.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ 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:254:4:Pony.__init_defaults:Unused private member `Pony.__init_defaults(self)`:HIGH
unused-private-member:259:4:Pony.__get_fur_color:Unused private member `Pony.__get_fur_color(self)`:HIGH
unused-private-member:271:4:Pony.__init_defaults:Unused private member `Pony.__init_defaults(self)`:HIGH
unused-private-member:276:4:Pony.__get_fur_color:Unused private member `Pony.__get_fur_color(self)`:HIGH

0 comments on commit 76ac95e

Please sign in to comment.