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 on decorators that return a staticmethod #7300

Closed
junrushao opened this issue Aug 12, 2022 · 2 comments · Fixed by #7301
Closed

False positive on decorators that return a staticmethod #7300

junrushao opened this issue Aug 12, 2022 · 2 comments · Fixed by #7301
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@junrushao
Copy link

junrushao commented Aug 12, 2022

Bug description

In some usecases, a decorator is applied to a method of a python class, and it returns a staticmethod instead. Those usecases are supported by mypy with a TYPE_CHECKING guard: python/mypy#6726 (comment).

# pylint: disable=missing-docstring,invalid-name
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    returns_staticmethod = staticmethod
else:
    def returns_staticmethod(f): ...


class MyClass:
    @returns_staticmethod
    def my_staticmethod(a, b): # <======== pylint (E0213): Method should have "self" as first argument
        return a + b

However, testing with pylint, an error pops up indicating self is not the first argument of the method above. Additionally, even if we are not using the TYPE_CHECKING guard, pylint does not seem to propagate staticmethod decorator:

# pylint: disable=missing-docstring,invalid-name,too-few-public-methods
my_decorator = staticmethod

class MyClass:
    @my_decorator
    def my_decorated_func(a, b): # <======== pylint (E0213): Method should have "self" as first argument
        return a + b

Ideally, if pylint propagates staticmethod through my_decorator, the false positive would go away.

Thanks in advance!

Configuration

Configuration is not relevant

Command used

pylint main.py

Pylint output

************* Module main
main.py:14:4: E0213: Method should have "self" as first argument (no-self-argument)

Expected behavior

Expected that pylint propagates staticmethod via my_decorator so that the snippets below will work without warning:

# pylint: disable=missing-docstring,invalid-name,too-few-public-methods
my_decorator = staticmethod

class MyClass:
    @my_decorator
    def my_decorated_func(a, b): # <======== pylint (E0213): Method should have "self" as first argument
        return a + b

Pylint version

>>> pylint --version
pylint 2.12.2
astroid 2.9.0
Python 3.8.13 (default, Mar 28 2022, 11:38:47)
[GCC 7.5.0]

OS / Environment

Ubuntu 20.04

Additional dependencies

N/A

@junrushao junrushao added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Aug 12, 2022
@mbyrnepr2
Copy link
Member

Looks like it can be fixed by checking node.decoratornames:

(venv310) Marks-MacBook-Air-2:programming markbyrne$ pylint example.py 
> /Users/markbyrne/programming/pylint/pylint/checkers/classes/class_checker.py(1920)_check_first_arg_for_type()
-> if node.type == "staticmethod":
(Pdb) node.decoratornames
<BoundFunctionWrapper at 0x102d79c60 for method at 0x102d5f0c0>
(Pdb) node.decoratornames()
{'builtins.staticmethod'}
(Pdb) 

@junrushao
Copy link
Author

@mbyrnepr2 Thank you for your swift response, and that makes a lot of sense! Do you plan to send a quick patch for the fix?

@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Aug 13, 2022
mbyrnepr2 added a commit to mbyrnepr2/pylint that referenced this issue Aug 13, 2022
…ied to a function but uses a different name.

Closes pylint-dev#7300
@jacobtylerwalls jacobtylerwalls added this to the 2.15.0 milestone Aug 13, 2022
jacobtylerwalls pushed a commit that referenced this issue Aug 13, 2022
* Fix false positive for `no-self-argument` when a staticmethod is applied to a function but uses a different name.

Closes #7300
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 Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants