x = f()
TypeError: 'staticmethod' object is not callable
(However mypy does not complain.)
Some suggest the solution is:
def f() -> int:
x = f.__func__()
But now mypy complains:
"Callable[, int]" has no attribute "__func__".
I also tried other variations with
self.f but none of them seemed to work either.
Is there a recommended / better approach to doing something like this?
See the docs for staticmethod:
Prior to Python 3.10, staticmethods are not directly callable from inside the class. So you can try upgrading to 3.10.
If you cannot upgrade to 3.10, you can try to use this instead of staticmethod:
"""Callable version of staticmethod."""
def __call__(self, *args, **kwargs):
return self.__func__(*args, **kwargs)
The wrong results with mypy should be reported to the mypy project as bugs, or at least queried there in case they have some good reason why they aren’t bugs.
Thanks! Great this is fixed in Python 3.10.
I had briefly looked at those docs, but didn’t (and still don’t) really see anything that explains this specific aspect.