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 with class name #4681

Closed
cdce8p opened this issue Jul 6, 2021 · 9 comments · Fixed by #4782 or #4965
Closed

False-positive unused-private-member with class name #4681

cdce8p opened this issue Jul 6, 2021 · 9 comments · Fixed by #4782 or #4965
Assignees
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code
Milestone

Comments

@cdce8p
Copy link
Member

cdce8p commented Jul 6, 2021

Steps to reproduce

class Singleton:
    __instance = None

    @staticmethod
    def instance():
        if Singleton.__instance is None:
            Singleton()
        return Singleton.__instance

    def __init__(self):
        try:
            Singleton.__instance = 42  # Error here
        except Exception:  # pylint: disable=broad-except
            print("Error")
            Singleton.__instance = False  # Error here

or

class Singleton:
    __instance = None

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

    def __init__(self):
        try:
            Singleton.__instance = 42  # Error here
        except Exception:  # pylint: disable=broad-except
            print("Error")
            Singleton.__instance = False  # Error here

Current behavior

test.py:12:12: W0238: Unused private member `Singleton.__instance` (unused-private-member)
test.py:15:12: W0238: Unused private member `Singleton.__instance` (unused-private-member)

Expected behavior

No errors

pylint --version output

pylint 2.9.4-dev0
astroid 2.6.3-dev0
Python 3.10.0b3

Additional information

Probably needs to be fixed similar to #4663

@cdce8p cdce8p added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Jul 6, 2021
@cdce8p
Copy link
Member Author

cdce8p commented Jul 6, 2021

@yushao2 Would you like to take a look at that?

@yushao2
Copy link
Collaborator

yushao2 commented Jul 7, 2021

@yushao2 Would you like to take a look at that?

Sure thing.

I don't really use classes that much, but just wanted to ask if there might be cases when a superclass's class name is used too

e.g.

class Singleton:
    __instance = None

    @staticmethod
    def instance():
        if Singleton.__instance is None:
            Singleton()
        return Singleton.__instance

    def __init__(self):
        try:
            Singleton.__instance = 42  # Error here
        except Exception:  # pylint: disable=broad-except
            print("Error")
            Singleton.__instance = False  # Error here

class InheritingClass(Singleton):
    def test(self):
        Singleton.__instance

Not sure if this is even valid syntax, but just wanted some clarifications at this stage before I start working on it

@Pierre-Sassoulas
Copy link
Member

As __instance is private in Singleton you should not be able to access it from InheritingClass

@yushao2
Copy link
Collaborator

yushao2 commented Jul 7, 2021

As __instance is private in Singleton you should not be able to access it from InheritingClass

right... not sure why my brain wasn't working. thank you 🙏

@yushao2
Copy link
Collaborator

yushao2 commented Aug 1, 2021

@cdce8p

picking this up after a hiatus, see #4782

@cdce8p
Copy link
Member Author

cdce8p commented Aug 1, 2021

@cdce8p

picking this up after a hiatus, see #4782

I'll be sure to take a look!

@cdce8p cdce8p added this to the 2.10.0 milestone Aug 2, 2021
@stanislavlevin
Copy link
Contributor

The second example in this issue hasn't been fixed with the mentioned PR (today's main).

[builder@localhost BUILD]$ python3 -m pylint --version
pylint 2.10.0-dev0
astroid 2.6.5
Python 3.9.6 (default, Jun 29 2021, 10:42:27) 
[GCC 10.2.1 20210313 (ALT Sisyphus 10.2.1-alt3)]

@stanislavlevin
Copy link
Contributor

[builder@localhost BUILD]$ python3 -m pylint a.py
************* Module a
a.py:12:12: W0238: Unused private member `Singleton.__instance` (unused-private-member)
a.py:15:12: W0238: Unused private member `Singleton.__instance` (unused-private-member)

@yushao2
Copy link
Collaborator

yushao2 commented Aug 2, 2021

@stanislavlevin hey thanks, I'll try it out and investigate

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