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

classmethod decorator transforms callable into callable rather than producing a classmethod? #12673

Open
NeilGirdhar opened this issue Apr 26, 2022 · 2 comments
Labels
bug mypy got something wrong topic-descriptors Properties, class vs. instance attributes

Comments

@NeilGirdhar
Copy link
Contributor

NeilGirdhar commented Apr 26, 2022

At the point at which the decorator is applied, the class method isn't a callable yet. It's a classmethod object that replies to __get__ to produce the bound method. Yet, MyPy already thinks it's a callable:

from __future__ import annotations
from typing import Callable

def decorate(c: classmethod[None]) -> Callable[[X], None]:
    def g(x: X) -> None:
        return c.__func__(type(x))
    return g

class X:
    @decorate  # error: Argument 1 to "decorate" has incompatible type "Callable[[Type[X]], None]"; expected "classmethod[None]"
    @classmethod
    def f(cls) -> None:
        print("okay")

x = X()
x.f()  # error: Invalid self argument "Type[X]" to class attribute function "f" with type "Callable[[X], None]"
@NeilGirdhar NeilGirdhar added the bug mypy got something wrong label Apr 26, 2022
@AlexWaygood AlexWaygood added the topic-descriptors Properties, class vs. instance attributes label Apr 26, 2022
@NeilGirdhar
Copy link
Contributor Author

Pyright won't fix the first problem, so I can just ignore it. But is the second problem fixable? The decorator changes the class method into an ordinary method, so why is x.f() passing the type?

@NeilGirdhar
Copy link
Contributor Author

This would be solved by python/typing#1372

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

No branches or pull requests

2 participants