functools.wraps does not work on class methods
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 5.1k
- フォーク
- 2.1k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 82
説明
We recently hit an error using @wraps on a class method in Trio: https://github.com/python-trio/trio/issues/2775#issuecomment-1702892474 when checked with pyright. It works fine on mypy, but I'm guessing they might be special-casing wraps usage while pyright doesn't.
Minimal repro (python 3.11):
from functools import wraps
from typing import assert_type
def foo(param: int) -> str:
"""docstring"""
return ""
@wraps(foo)
def my_function(blah: int) -> str:
return 'foo'
# works fine
assert_type(my_function(5), str)
class MyOtherClass:
def bar(self, param: int) -> str:
"""docstring"""
return ""
class MyClass:
@wraps(MyOtherClass.bar)
def my_method(self, blah: int) -> str:
return 'foo'
instance = MyClass()
# works in mypy, but not pyright
assert_type(instance.my_method(5), str)
mypy 1.5.1 works without any issue, pyright 1.1.325 gives
./test.py
./test.py:26:13 - error: Argument missing for parameter "blah" (reportGeneralTypeIssues)
./test.py:26:13 - error: "assert_type" mismatch: expected "str" but received "Unknown" (reportGeneralTypeIssues)
2 errors, 0 warnings, 0 informations
This smells a lot like the problem with getting functools.cache to work both with methods and functions, see e.g. https://github.com/python/typeshed/issues/6347
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、pyright を使って最小限の Python 3.11 再現コードを実行し、mypy と比較します。次に、functools.wraps の typeshed スタブを調べ、デコレートされた関数とメソッドをどのように表現しているかに注目します。クラスメソッドの例が型チェックを通り、assert_type(instance.my_method(5), str) がエラーを報告しなければ完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100