python / python/mypy

(🐞) `staticmethod` special-casing causes error to not be reported

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

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

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

説明

If we copy the implementation of staticmethod from typeshed, mypy correctly reports the error here.

So it seems that mypys dedicated special-casing of staticmethod is causing this issue to be incorrectly not reported.

from collections.abc import Callable
from typing import Generic, ParamSpec, TypeVar, overload

P = ParamSpec("P")
T = TypeVar("T")
R_co = TypeVar("R_co", covariant=True)


def contextmanager(fn: Callable[P, R_co]) -> Callable[P, R_co]:
    def func(*args: P.args, **kwargs: P.kwargs) -> R_co:
        return fn(*args, **kwargs)
    return func


class staticmethod_copy(Generic[P, R_co]):
    @property
    def __func__(self) -> Callable[P, R_co]: ...
    @property
    def __isabstractmethod__(self) -> bool: ...
    def __init__(self, __f: Callable[P, R_co]) -> None: ...
    @overload
    def __get__(self, __instance: None, __owner: type) -> Callable[P, R_co]: ...
    @overload
    def __get__(self, __instance: T, __owner: type[T] | None = None) -> Callable[P, R_co]: ...
    __name__: str
    __qualname__: str
    @property
    def __wrapped__(self) -> Callable[P, R_co]: ...
    def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R_co: ...

# Just the relevant part of staticmethod definition that is needed
#class staticmethod(Generic[P, R_co]):
#    def __init__(self, __f: Callable[P, R_co]) -> None: ...
#    def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R_co: ...

class Foo:
    @contextmanager
    @staticmethod_copy
    def copy() -> None: ...

    @contextmanager
    @staticmethod
    def native() -> None: ...


Foo().copy()   # ✅ error: Attribute function "copy" with type "Callable[[], None]" does not accept self argument  [misc]
Foo().native()  # ❌no error

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

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

はじめの一歩

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

調査の方向性

提供された例を再現し、mypy の staticmethod 専用の特別扱いと、コピーした typeshed の実装を比較する。デコレータと descriptor 型がどのように解析されるかを追跡する。完了条件は、Foo().copy() の場合と同様に、mypy が Foo().native() に対して missing-self エラーを報告することである。

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

評価

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

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

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