python / python/cpython

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

Aberta
#131,983 9 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

stdlib type-feature
Linguagem predominante
Python
Estrelas
77.2k
Forks
35.9k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

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:

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece revisando a issue do Cython vinculada e as três implementações de exemplo que detectam funções Cython. Nenhum arquivo ou teste do CPython é indicado, então o primeiro passo é localizar as áreas relevantes de ABC e introspecção e confirmar a API proposta com os maintainers. O trabalho estará concluído quando houver um design aceito para o novo ABC, seus registros e testes, além da alteração correspondente em backports_abc.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
python
Domínio
developer-experience
Tipo de issue
Funcionalidade
Dificuldade
5/5
Tempo estimado
Mais de uma semana
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
32/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.