python / python/cpython

Add a `collection.abc.Function` ABC for function-like objects

未关闭
#131,983 9 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Feature or enhancement

Proposal:

In the Cython project, we generate a function type that is mostly compatible with Python's function type. There is currently no (simple/reasonably fast) way to detect this kind of object, but there are use cases and Cython aware projects have found their workarounds in one way or another. But it still means that all code that wants to specially support both Python and Cython functions in the same (or similar) way needs to do something special to detect both, rather than just saying "is this a function?".

There should be an abc.Function ABC that other function implementations can register with.
That would make the detection a simple and straight forward isinstance(obj, abc.Function).

inspect.isfunction() does not help since it specifically tests that the object is a Python function object, which is good to have, simple and fills a need. The ABC is for use cases where code needs to test whether an object is a callable with a function-like interface, e.g. for introspection, signature, code-like object, etc., however complete that implementation then is.

This is probably also relevant for other tools like mypyc or numba that provide binary functions to Python in one way or another.

I'd also add this ABC to the PyPI package backports_abc which provides missing ABCs for older Pythons.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

Make inspect.isfunction() support Cython functions:
https://github.com/cython/cython/issues/6379

Some code trying to detect Cython functions:

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先查看链接的 Cython issue 以及用于检测 Cython 函数的三个示例实现。没有指定任何 CPython 文件或测试,因此第一步是定位相关的 ABC 和自省部分,并与维护者确认所提议的 API。完成条件是:新 ABC、其注册和测试都有已接受的设计,并且 backports_abc 中也有相应的更改。

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

评估

技术栈
python
领域
developer-experience
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
32/100

把新 issue 发到你的邮箱

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