Duck-typing in `inspect.getfile`
Ninguém assumiu esta issue ainda.
- Linguagem predominante
- Python
- Estrelas
- 77.2k
- Forks
- 36k
- Métricas de merge de PRs
- Métricas de PR pendentes
Descrição
Running inspect.getsource(numpy.random.random_sample) yields
File ~\.conda\envs\sage-dev\Lib\inspect.py:943, in getfile(object)
941 if iscode(object):
942 return object.co_filename
--> 943 raise TypeError('module, class, method, function, traceback, frame, or '
944 'code object was expected, got {}'.format(
945 type(object).__name__))
since the cython function is not correct recognized as a "true" function (i.e. it is not derived from FunctionType). But since a Cython function correct provides __code__, all the information is actually there to properly get the source file.
While it is quite possible to emulate the interface of functions in Cython's own function type, and recent Cython releases have seen several improvements here, the "inspect" module does not consider a Cython implemented function a "function", because it tests the object type explicitly instead of comparing an abstract interface or an abstract base class. This has a negative impact on code that uses inspect to inspect function objects, but would require a change to Python itself.
and
At the very least, the inspect module should use more duck-typing internally. For example, consider this code from "getfile":
if ismethod(object): object = object.__func__ if isfunction(object): object = object.__code__ if istraceback(object): object = object.tb_frame if isframe(object): object = object.f_code if iscode(object): return object.co_filename
Originally posted by @jdemeyer in #74257
Linked PRs
- gh-131632
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Comece com inspect.getfile e as verificações isfunction/iscode mostradas na issue. Compare como os objetos que expõem code são tratados e, em seguida, revise o PR vinculado gh-131632 antes de fazer qualquer trabalho. Considera-se concluído quando a função Cython reportada consegue resolver seu arquivo-fonte sem quebrar o comportamento existente de inspect.getfile.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- python
- Domínio
- devtools
- Tipo de issue
- Funcionalidade
- Dificuldade
- 4/5
- Tempo estimado
- 3-5 dias
- Status de atividade
- Estagnada
- Clareza
- Razoavelmente clara
- Facilidade para iniciantes
- 25/100