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

decorator as class member raises "self-argument missing" #7778

Closed
azrdev opened this issue Oct 22, 2019 · 16 comments · Fixed by #7860
Closed

decorator as class member raises "self-argument missing" #7778

azrdev opened this issue Oct 22, 2019 · 16 comments · Fixed by #7860

Comments

@azrdev
Copy link

azrdev commented Oct 22, 2019

I have a legitimate case for member functions not requiring a self parameter, which fails with error: Self argument missing for a non-static method (or an invalid type for self) (mypy version 0.730, python 3.7.4). MWE:

import functools
from typing import Callable

class API:
    def _login_required(meth: Callable) -> Callable:
        @functools.wraps(meth)
        def check_login(self, *args, **kwargs):
            if not self.check_login():
                self.login()
            return meth(*args, **kwargs)
        return check_login

    @_login_required
    def version(self):
        pass

It is a member function of a class, to be used as decorator.

@ilevkivskyi
Copy link
Member

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 bind_self(). In this case we would give an error when one tries to write API()._login_required.

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 __init__ and overloaded methods, because those a special w.r.t. self-types).

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 _login_required would be completely excluded from Liskov check.)

@ilevkivskyi
Copy link
Member

Probably we should open another meta-issue like #7724

Well, no we have a "meta-PR" #7860 (turned out to be smaller than expected).

ilevkivskyi added a commit to ilevkivskyi/mypy that referenced this issue Nov 5, 2019
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.
@bardiharborow
Copy link

@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))
$ python3 foo.py
4
$ mypy .
foo.py:4: error: Self argument missing for a non-static method (or an invalid type for self)
Found 1 error in 1 file (checked 1 source file)

@Fokko
Copy link

Fokko commented Jul 24, 2020

We have a similar situation in Apache Spark: https://github.com/apache/spark/blob/master/python/pyspark/mllib/linalg/__init__.py#L472-L490

python/pyspark/mllib/linalg/__init__.py:472: error: Self argument missing for a non-static method (or an invalid type for self)
Found 1 error in 1 file (checked 186 source files)

mypy --version
mypy 0.782

Fokko added a commit to Fokko/spark that referenced this issue Jul 24, 2020
There is an open issue:
python/mypy#7778
@azrdev
Copy link
Author

azrdev commented Aug 11, 2020

Should this issue be reopened?

@gvanrossum
Copy link
Member

Probably best to open a new issue with a simple, complete repro.

@kernelmethod
Copy link

Just re-encountered this problem in mypy 0.902 with Python 3.8.

Probably best to open a new issue with a simple, complete repro.

Did anybody ever open this issue?

@mr-ubik
Copy link

mr-ubik commented Oct 4, 2021

@kernelmethod have you found out if the issue was opened?

@datadominik
Copy link

I'm still getting the same error, is there any update regarding class member decorators without self?

@tanlin2013
Copy link

Still have this error in July, 2022.

@nilreml
Copy link

nilreml commented Feb 15, 2023

Still have this error in February, 2023.

@ilius
Copy link

ilius commented Jul 26, 2023

Same here:

class T_Collator(typing.Protocol):
	@classmethod
	def createInstance(loc: "T_Locale | None" = None) -> "T_Collator":
		pass

error: Self argument missing for a non-static method (or an invalid type for self) [misc]

$ mypy --version
mypy 1.4.1 (compiled: yes)

@ikonst
Copy link
Contributor

ikonst commented Jul 26, 2023

@ilevkivskyi #7860 added the testBadClassLevelDecoratorHack test but presumably it works due to the type-var, not due to the check going away? (this seems to be why this issue is still a problem for many folks)

@ilevkivskyi
Copy link
Member

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 Callable annotation for first argument with an equivalent callback protocol.

@ikonst
Copy link
Contributor

ikonst commented Jul 27, 2023

For some reason I assumed we moved the check to call-sites.

@kxue-godaddy
Copy link

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 Callable annotation for first argument with an equivalent callback protocol.

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 Greeter.__call__ is not marked to have positional only parameters, its 2nd and 3rd arguments must be of the same names as the 1st and 2nd of A.greet (i.e. self and messge). Otherwise mypy reports an arg-type error on the line of @_decorator. I don't have a CS degree, but from what I learned by reading PEP 570, I guess this is due to the Liskov Substitution Principle. Kudos to the mypy developers for such details!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.