Skip to content

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

Open

Description

@NeilGirdhar

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]"

Activity

  1. NeilGirdhar commented on Apr 26, 2022

    @NeilGirdhar
    ContributorAuthor

    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?

  2. NeilGirdhar commented on Aug 16, 2023

    @NeilGirdhar
    ContributorAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-descriptorsProperties, class vs. instance attributes

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions