summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre Sassoulas <pierre.sassoulas@gmail.com>2021-07-01 09:19:36 +0200
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2021-07-01 09:31:13 +0200
commit472f25ee0c727c955061c3c7605bcbe6486b2707 (patch)
tree9589f7767b8df2cf4c20acc17f7bc7af088a7bc5
parent74dce8afcbccbda7cab9bebb6766f6b0aec43410 (diff)
downloadpylint-git-472f25ee0c727c955061c3c7605bcbe6486b2707.tar.gz
[unused-private-member] Handle the case using self to access members
As seen in issue #4644
-rw-r--r--ChangeLog5
-rw-r--r--pylint/checkers/classes.py2
-rw-r--r--tests/functional/u/unused/unused_private_member.py16
3 files changed, 22 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index c84b2620b..ef043efa1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -22,6 +22,11 @@ Release date: TBA
Closes #4638
+* Fix a false positive for ``unused-private-member`` when accessing a private variable
+ with ``self``
+
+ Closes #4644
+
What's New in Pylint 2.9.1?
===========================
diff --git a/pylint/checkers/classes.py b/pylint/checkers/classes.py
index 8f1eeec22..abf77a416 100644
--- a/pylint/checkers/classes.py
+++ b/pylint/checkers/classes.py
@@ -949,7 +949,7 @@ a metaclass class method.",
if (
isinstance(child, astroid.Attribute)
and child.attrname == assign_name.name
- and child.expr.name in ("cls", node.name)
+ and child.expr.name in ("self", "cls", node.name)
):
break
else:
diff --git a/tests/functional/u/unused/unused_private_member.py b/tests/functional/u/unused/unused_private_member.py
index 9fb02cf92..a8312e107 100644
--- a/tests/functional/u/unused/unused_private_member.py
+++ b/tests/functional/u/unused/unused_private_member.py
@@ -87,3 +87,19 @@ class Bla:
@classmethod
def __c(cls):
pass
+
+
+class Klass:
+ """Regression test for 4644"""
+
+ __seventyseven = 77
+ __ninetyone = 91
+
+ def __init__(self):
+ self.twentyone = 21 * (1 / (self.__seventyseven + 33)) % 100
+ self.ninetyfive = Klass.__ninetyone + 4
+
+
+k = Klass()
+print(k.twentyone)
+print(k.ninetyfive)