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

False positive unused-private-member if class member is accessed via self #4644

Closed
akosthekiss opened this issue Jun 30, 2021 · 5 comments · Fixed by #4645
Closed

False positive unused-private-member if class member is accessed via self #4644

akosthekiss opened this issue Jun 30, 2021 · 5 comments · Fixed by #4645
Assignees
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code
Milestone

Comments

@akosthekiss
Copy link

Steps to reproduce

Given a file p.py:

"""p.py"""

class Klass:  # pylint: disable=too-few-public-methods
    """Klass"""

    __seventyseven = 77
    __ninetyone = 91

    def __init__(self):
        self.twentyone = (self.__seventyseven + 44) % 100
        self.ninetyfive = Klass.__ninetyone + 4

k = Klass()
print(k.twentyone)
print(k.ninetyfive)

Current behavior

Result of pylint p.py:

************* Module p
p.py:6:4: W0238: Unused private member `Klass.__seventyseven` (unused-private-member)

Expected behavior

No diagnostics.

pylint --version output

Result of pylint --version output:

pylint 2.9.1
astroid 2.6.2
Python 3.6.9 (default, Jan 26 2021, 15:33:00) 
[GCC 8.4.0]
pylint 3.0.0-a4
astroid 2.6.2
Python 3.6.9 (default, Jan 26 2021, 15:33:00) 
@Pierre-Sassoulas Pierre-Sassoulas added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Jun 30, 2021
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.9.2 milestone Jun 30, 2021
@Pierre-Sassoulas
Copy link
Member

Thank you, I can reproduce this.

@Pierre-Sassoulas
Copy link
Member

This will be fixed in 2.9.2, we'll release it when #4630 is merged :)

@earonesty
Copy link

I get this error on classmethods no matter what, regardless of self-use and no mutation needed:

Snippet:

    @classmethod
    def __get_type(cls, k) -> Type:						# pylint: disable=unused-private-member
        return cls.__annotations__.get(k, None)

    @staticmethod
    def __accept_instance(v, typ):                      # pylint: disable=unused-private-member
        if typ is Any:
            return True
        if type(v) is int and issubclass(typ, float):
            return True
        try:
            return isinstance(v, typ)
        except TypeError:
            # not checking types i don't know how to handle
            return True

    @classmethod
    def __assert_instance(cls, k, v):
        typ = cls.__get_type(k)
        if getattr(typ, "__origin__", None) is Union:
            for sub in typ.__args__:
                if cls.__accept_instance(v, sub):
                    return

@vepain
Copy link

vepain commented Sep 13, 2021

Dear @Pierre-Sassoulas,

this issue is still here in Pylint 3.0.0

Versions:

$ pylint --version
pylint 3.0.0-a4
astroid 2.6.6
Python 3.9.5 (default, May 19 2021, 11:32:47) 
[GCC 9.3.0]

MWE:

class A():

    __CLS_ATTR = 0  # pylint: unused-private-member

    def __init__(self, t_int_int: tuple[int, int]):
        self._object_attr: tuple[int, int] = t_int_int
     
    def get_first_int(self) -> int:
        return self._object_attr[self.__CLS_ATTR]

Thank your for your help !

@Pierre-Sassoulas
Copy link
Member

Hey, it's because 3.0 alpha is pylint 2.8 with some breaking change rebased on it. I might release a new 3.0 alpha rebased on 2.11 when we fix the problem with the current alpha version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
4 participants