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

🐛 Methods converted to staticmethods using a decorator not treated as staticmethods #13434

Open
tombulled opened this issue Aug 16, 2022 · 1 comment
Labels
feature topic-descriptors Properties, class vs. instance attributes

Comments

@tombulled
Copy link

Bug Report
The @staticmethod decorator only appears to work with MyPy when used as a decorator.

For example, the following code works:

class MyClass:
    @staticmethod
    def my_method():
        ...

However, the following code does not work:

from typing import Callable


def make_staticmethod(func: Callable, /) -> staticmethod:
    return staticmethod(func)

class MyClass:
    @make_staticmethod
    def my_method():
        ...

The above code produces the following error:

error: Method must have at least one argument

To Reproduce

  1. Run MyPy against the non-working example shown above

Expected Behavior
MyPy sees that the decorator returns a staticmethod and proceeds to treat MyClass.my_method as a staticmethod

Actual Behavior
MyPy produces the following error:

error: Method must have at least one argument

The above error implies that MyPy is not treating the method as a staticmethod.

Your Environment

  • Mypy version used: 0.971
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.8.10
  • Operating system and version: Linux Mint 20.3 x86_64
@tombulled tombulled added the bug mypy got something wrong label Aug 16, 2022
@AlexWaygood AlexWaygood added feature topic-descriptors Properties, class vs. instance attributes and removed bug mypy got something wrong labels Aug 20, 2022
@RivenSkaye
Copy link

I've noticed this too, for both staticmethod and classmethod. Which is a shame, because application order of decorators can cause some funky stuff to happen, including but not limited to straight up errors at runtime, or typehints breaking down to *args: Any, **kwargs: Any.
If you do add arguments to the functions, the error changes to a warning that there is no self or cls argument.
Tangentially related, both of those decorators return object instances of their specific classes that are seen as e.g. staticmethod[int], but trying to hint a function or method as returning such a function causes a runtime error due to the classes not being subscriptable.
I think a possible solution would be typing entries for StaticMethod[] (and perhaps for typing_extensions too?) and ClassMethod[].

As a positive side effect, this might also stop IDE autocomplete from adding cls as a positional argument in its autocomplete, since the handling of these annotations would be different

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-descriptors Properties, class vs. instance attributes
Projects
None yet
Development

No branches or pull requests

3 participants