functools.wraps does not work on class methods
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 5.1k
- 派生
- 2.1k
- 平均合并
- 1 天 19 小时
- 30 天内合并 PR
- 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 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 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