python / python/mypy

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

オープン
#12,673 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug topic-descriptors
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、提供された例を mypy で再現し、checker が積み重なった @decorate と @classmethod デコレーターをどのように型付けするかを調べます。例が decorate に渡された classmethod と、後続の x.f() 呼び出しの両方を、どちらのエラーも報告せずに受け入れれば、修正は完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。