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 W0238 with private staticmethod used from classmethod #4949

Merged

Conversation

yumasheta
Copy link
Contributor

Type of Changes

Type
βœ“ πŸ› Bug fix

Description

This PR fixes false positive unusede-private-member W0238 when using a private staticmethod from a classmethod, ie. cls.__private_static_method().

Also a regression test is added to the functional tests.

Closes #4849

@@ -926,9 +926,12 @@ def _check_unused_private_functions(self, node: nodes.ClassDef) -> None:
continue
if isinstance(attribute.expr, nodes.Name) and attribute.expr.name in (
"self",
"cls",
Copy link
Member

Choose a reason for hiding this comment

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

I think we already fixed something really similar in another MR, we have a duplication between this and child.expr.name in ("self", "cls", node.name) line 969/972.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can still reproduce #4849 against master. I don't quite understand what the lines you reference fixed.

Copy link
Member

Choose a reason for hiding this comment

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

A previous false positive for unused-private-member. I was wondering if we need to refactor that check to have a single piece of code to check that a name represent the current class. It's probably a little late now πŸ˜„

Copy link
Contributor Author

@yumasheta yumasheta Sep 1, 2021

Choose a reason for hiding this comment

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

Ok. Yes, it seems like the possibility to access attributes/methods on cls representing the class is beeing added by parts now.
I'm still unsure of hardcoding "cls" into the checks in general without further specification. While pylint checks, that the convention with "cls" as the first parameter of a classmethod is followed, it's still no guarantee. And if this rule is disabled on a case by case basis then all these "fixes" will fail again.
But I guess that's the same for the "self" parameter/word.

Copy link
Member

Choose a reason for hiding this comment

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

#4645 found the MR I was talking about.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@Pierre-Sassoulas kinda rusty, let me refresh my brain for a bit

Copy link
Collaborator

Choose a reason for hiding this comment

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

@Pierre-Sassoulas if it can be refactored now, it would be great (since i foresee that it might be refactored again in the future)

Just my two-cents to try to make code more concise and DRY early on rather than doing it later. Could you give it a shot @yumasheta ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm humbled, but as this is my first contribution to pylint source code (and first ever look at it), I reckon there are better qualified people out there.
Also the earliest I could do would be Okt/Nov.

Copy link
Member

Choose a reason for hiding this comment

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

@yushao2 what do you think about merging this then refactoring in #4965 ? Ideally I'd like to include this in 2.11 that should be released soon.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@Pierre-Sassoulas lets do that, i will check on the compatibility once this is merged with my branch

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.11.0 milestone Sep 1, 2021
@Pierre-Sassoulas Pierre-Sassoulas added Bug πŸͺ² False Positive 🦟 A message is emitted but nothing is wrong with the code labels Sep 1, 2021
@cdce8p cdce8p requested a review from yushao2 September 1, 2021 13:13
@coveralls
Copy link

coveralls commented Sep 1, 2021

Pull Request Test Coverage Report for Build 1209376769

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 93.107%

Totals Coverage Status
Change from base Build 1209374325: 0.0%
Covered Lines: 13237
Relevant Lines: 14217

πŸ’› - Coveralls

Copy link
Collaborator

@yushao2 yushao2 left a comment

Choose a reason for hiding this comment

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

LGTM, will take a shot at refactoring / open for others to take a crack at it too :)

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 for your contribution :) !

@Pierre-Sassoulas Pierre-Sassoulas merged commit 9683362 into pylint-dev:main Sep 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug πŸͺ² 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 of unused-private-member with class reference
4 participants