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

Fix false-positive of unused-private-member with class name #4782

Merged

Conversation

yushao2
Copy link
Collaborator

@yushao2 yushao2 commented Aug 1, 2021

Type of Changes

Type
βœ“ πŸ› Bug fix

Description

Simple fix to avoid false-positive to check for private attributes and do not emit if it was called with the Class's name.

Done by using a boolean condition and comparing it to the node's (classdef) name

Also, added a pylint ignore as more boolean expressions are needed here

Closes #4681

@coveralls
Copy link

coveralls commented Aug 1, 2021

Coverage Status

Coverage increased (+0.004%) to 92.269% when pulling 0dcfd89 on yushao2:fix-unused-private-member-4681 into 2ad8247 on PyCQA:main.

@yushao2 yushao2 force-pushed the fix-unused-private-member-4681 branch from df706ee to f46af19 Compare August 1, 2021 12:15
Copy link
Member

@cdce8p cdce8p left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments. Additionally I noticed that _check_unused_private_attributes doesn't have typing.cast after nodes_of_class. Maybe you would like to add it with this change (two times).

--
One more thing, just so you know. For PRs it doesn't make much sense to assign yourself if you are already the author. I usually assign myself if I want to show that I'll review a PR from someone else and what to help see it through.

pylint/checkers/classes.py Outdated Show resolved Hide resolved
pylint/checkers/classes.py Outdated Show resolved Hide resolved
@cdce8p cdce8p assigned cdce8p and unassigned yushao2 Aug 1, 2021
@cdce8p cdce8p modified the milestones: 2.10.1, 2.10.0 Aug 1, 2021
Copy link
Member

@cdce8p cdce8p left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I also like the additional test case you added πŸš€

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you !

@Pierre-Sassoulas Pierre-Sassoulas added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Aug 1, 2021
@Pierre-Sassoulas Pierre-Sassoulas merged commit 7c51580 into pylint-dev:main Aug 1, 2021
@yumasheta
Copy link
Contributor

Hello there!

Shouldn't this PR also fix:

class UnusedPrivateMember:
    """unused-private-member (W0238)"""

    @staticmethod
    def __private_method():
        """Is private and does nothing."""

    @classmethod
    def use_private_method(cls):
        """Calls private method."""
        cls.__private_method()

which produces:

W0238: Unused private member `UnusedPrivateMember.__private_method()` (unused-private-member)

I'm running the latest master branch:

pylint 2.10.0-dev0
astroid 2.6.6
Python 3.9.6 (default, Jun 30 2021, 10:22:16) 
[GCC 11.1.0]

Note, when using a class attribute (i.e. change __private_method to an attr) it does work as expected.

@cdce8p
Copy link
Member

cdce8p commented Aug 15, 2021

@yumasheta Can you open a new issue for it? That way we are better able to track it

@yushao2 yushao2 deleted the fix-unused-private-member-4681 branch January 4, 2023 13:56
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
Development

Successfully merging this pull request may close these issues.

False-positive unused-private-member with class name
5 participants