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 for no-self-argument #7301

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/7300.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix false positive for `no-self-argument` when a staticmethod is applied to a function but uses a different name.

Closes #7300
3 changes: 3 additions & 0 deletions pylint/checkers/classes/class_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1926,6 +1926,9 @@ def _check_first_arg_for_type(
self.add_message("bad-staticmethod-argument", args=first, node=node)
return
self._first_attrs[-1] = None
elif any(name == "builtins.staticmethod" for name in node.decoratornames()):
# Check if there is a decorator which is not named `staticmethod` but is assigned to one.
return
# class / regular method with no args
elif not node.args.args and not node.args.posonlyargs:
self.add_message("no-method-argument", node=node)
Expand Down
28 changes: 25 additions & 3 deletions tests/functional/n/no/no_self_argument.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
"""Check for method without self as first argument"""
# pylint: disable=useless-object-inheritance
from __future__ import print_function
Copy link
Member

Choose a reason for hiding this comment

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

Nice catch ! If there are a lot of those we could create another merge request to fix all of those python2 remnants at once


class NoSelfArgument(object):

MYSTATICMETHOD = staticmethod


def returns_staticmethod(my_function):
"""Create a staticmethod from function `my_function`"""
return staticmethod(my_function)


class NoSelfArgument:
"""dummy class"""

def __init__(truc): # [no-self-argument]
Expand All @@ -16,3 +23,18 @@ def abdc(yoo): # [no-self-argument]
def edf(self):
"""just another method"""
print('yapudju in', self)

@staticmethod
def say_hello():
"""A standard staticmethod"""
print("hello!")

@MYSTATICMETHOD
def say_goodbye():
"""A staticmethod but using a different name"""
print("goodbye!")

@returns_staticmethod
def sum_strings(string1, string2):
"""A staticmethod created by `returns_staticmethod` function"""
return string1 + string2
4 changes: 2 additions & 2 deletions tests/functional/n/no/no_self_argument.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
no-self-argument:8:4:8:16:NoSelfArgument.__init__:"Method should have ""self"" as first argument":UNDEFINED
no-self-argument:12:4:12:12:NoSelfArgument.abdc:"Method should have ""self"" as first argument":UNDEFINED
no-self-argument:15:4:15:16:NoSelfArgument.__init__:"Method should have ""self"" as first argument":UNDEFINED
no-self-argument:19:4:19:12:NoSelfArgument.abdc:"Method should have ""self"" as first argument":UNDEFINED