python / python/mypy

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

Open
#12,673 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-descriptors
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided example with mypy and inspect how the checker types stacked @decorate and @classmethod decorators. The fix is complete when the example accepts the classmethod passed to decorate and the later x.f() call without either reported error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.