-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
decorator as class member raises "self-argument missing" #7778
Comments
It is really interesting that this is closely related to #7191 as well. I think the solution is the same, move consistency check from definition to use site, i.e. to It looks like we have a large bunch of issues that would be fixed my moving the check to use site (plus some changes for Probably we should open another meta-issue like #7724, @JukkaL what do you think? (Btw, the new meta issue would partially depend on the previous one, because we would need more "principled" Liskov checks, like in this case |
Fixes python#3625 Fixes python#5305 Fixes python#5320 Fixes python#5868 Fixes python#7191 Fixes python#7778 Fixes python/typing#680 So, lately I was noticing many issues that would be fixed by (partially) moving the check for self-type from definition site to call site. This morning I found that we actually have such function `check_self_arg()` that is applied at call site, but it is almost not used. After more reading of the code I found that all the patterns for self-types that I wanted to support should either already work, or work with minimal modifications. Finally, I discovered that the root cause of many of the problems is the fact that `bind_self()` uses wrong direction for type inference! All these years it expected actual argument type to be _supertype_ of the formal one. After fixing this bug, it turned out it was easy to support following patterns for explicit self-types: * Structured match on generic self-types * Restricted methods in generic classes (methods that one is allowed to call only for some values or type arguments) * Methods overloaded on self-type * (Important case of the above) overloaded `__init__` for generic classes * Mixin classes (using protocols) * Private class-level decorators (a bit hacky) * Precise types for alternative constructors (mostly already worked) This PR cuts few corners, but it is ready for review (I left some TODOs). Note I also add some docs, I am not sure this is really needed, but probably good to have.
@ilevkivskyi this still seems to occur in MyPy 0.782, Python 3.8.2. from typing import Any, Callable
class Foo:
def decorator(func: Callable[[Any, int, int], int]):
def modified_func(self, a: int, b: int) -> int:
return func(self, a, b) + 1
return modified_func
@decorator
def add(self, a: int, b: int) -> int:
return a + b
if __name__ == '__main__':
foo = Foo()
print(foo.add(1, 2))
|
We have a similar situation in Apache Spark: https://github.com/apache/spark/blob/master/python/pyspark/mllib/linalg/__init__.py#L472-L490
|
There is an open issue: python/mypy#7778
Should this issue be reopened? |
Probably best to open a new issue with a simple, complete repro. |
Just re-encountered this problem in mypy 0.902 with Python 3.8.
Did anybody ever open this issue? |
@kernelmethod have you found out if the issue was opened? |
I'm still getting the same error, is there any update regarding class member decorators without self? |
Still have this error in July, 2022. |
Still have this error in February, 2023. |
Same here: class T_Collator(typing.Protocol):
@classmethod
def createInstance(loc: "T_Locale | None" = None) -> "T_Collator":
pass
|
@ilevkivskyi #7860 added the |
The check is not going anywhere, because it is correct and, judging from the last example, useful. That PR allowed a hacky way to avoid the error while staying type-safe: that PR allows arbitrary annotation method for first argument (even completely unrelated to current class) as soon as it is a protocol. This was initially intended for typing mixins (see docs), but it apparently also helps with class level decorators, one just needs to replace |
For some reason I assumed we moved the check to call-sites. |
Thanks for the summary! I had this typing question during work today and found the solution here. Below is a working example that I find interesting, and would like to post it here in case it's helpful to anyone: from __future__ import annotations
from functools import wraps
from typing import Callable, Protocol
class Greeter(Protocol):
# def __call__(_, self: A, message: str) -> None: # this works
def __call__(self, a: A, msg: str, /) -> None: # also works
...
class A:
def _decorator(f: Greeter) -> Callable[[A, str], None]:
@wraps(f)
def _f_(self: A, msg: str) -> None:
print("-" * 40)
f(self, msg)
print("-" * 40)
return _f_
def __init__(self, name: str) -> None:
self.name = name
@_decorator
def greet(self, message: str) -> None:
print(f"{message} from {self.name}.")
a = A("random guy")
a.greet("Hello World") I find that if |
I have a legitimate case for member functions not requiring a
self
parameter, which fails witherror: Self argument missing for a non-static method (or an invalid type for self)
(mypy version 0.730, python 3.7.4). MWE:It is a member function of a class, to be used as decorator.
The text was updated successfully, but these errors were encountered: