`functools.partialmethod` loses return type and callable signature information

未关闭
#16,499 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
python

调研方向

Start by reading the existing functools.partialmethod stub in typeshed and compare its descriptor behavior with the examples in the issue. Check the relevant stub tests for callable and descriptor typing; the issue does not name a test file. Done means preserving return-type and descriptor-binding information as far as current typing support allows, while acknowledging that a fully precise residual signature needs typing-spec support.

由索引模型根据 Issue 内容生成。

描述

For code of this shape:

from functools import partialmethod

def pprint(arg: object, /, *, options: dict) -> str:
    return "dummy"

class Foo: ...
class Bar: ...

Bar.__repr__ = partialmethod(pprint, options={})

it is difficult for type checkers to verify that partialmethod is compatible with the target method since it is currently typed with only a return-type parameter:

class partialmethod(Generic[_T]):
      ...
      def __get__(self, obj: Any, cls: type[Any] | None = None) -> Callable[..., _T]: ...

The current stub does not preserve the wrapped callable’s parameters or receiver type, so type checkers cannot model these class and instance access forms even though this ebhaviour is valid at runtime, and partialmethod is a descriptor:

Bar.__repr__      # T: (self: Bar) -> str
Bar().__repr__    # T: () -> str

This forces Pyrefly and Pyright to reject this valid code. Mypy on the other hand, looks at stub’s descriptor fallback (Any) and infers partialmethod[Any] even when the wrapped callable returns str.

We should preserve descriptor binding and as much callable information as possible, while accepting that a fully precise residual signature needs typing-spec support.

Born from Pyrefly issue, https://github.com/facebook/pyrefly/issues/5138

主要语言
Python
星标
5.1k
派生
2.1k
平均合并
2 天 1 小时
30 天内合并 PR
115

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python/typeshed 的其他 Issue

查看 python/typeshed 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。