`singledispatchmethod` inspected signature incorrectly includes `self`

Aberta Para iniciantes
#117,735 0 comentários 1 reação 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
68/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Estagnada
Stack de tecnologia
python
Domínio
tooling

Direção de pesquisa

Leia Lib/functools.py por volta da linha 979, concentrando-se em como singledispatchmethod.get envolve o método vinculado. Execute a reprodução fornecida de inspect.signature e sig.bind; está concluído quando a assinatura vinculada omitir self, aceitar o argumento real do método e os testes relevantes passarem.

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

Descrição

type-bug

Bug report

Bug description:

The result of singledispatchmethod.__get__ is wrapped to imitate the underlying function, instead of the result of func.__get__. This leads to an incorrect signature being reported by inspect.signature, which includes the leading self argument, thus misleading introspection tools as to the real arguments the method accepts.

import inspect
from functools import singledispatchmethod

class A:
    @singledispatchmethod
    def a(self, arg):
        pass

sig = inspect.signature(A().a)
# <Signature (self, arg)> but we expect <Signature (arg)>

sig.bind(None)  # should be OK because `A().a(None)` is OK. Instead:
# TypeError: missing a required argument: 'arg'
# because the first argument is erroneously bound to `self`.

One way to fix this might be to edit https://github.com/python/cpython/blob/6258844c27e3b5a43816e7c559089a5fe0a47123/Lib/functools.py#L979 to read instead

update_wrapper(_method, self.func.__get__(obj, cls))
CPython versions tested on:

3.10, 3.12

Operating systems tested on:

Linux, Windows

Linguagem predominante
Python
Estrelas
77.2k
Forks
36k
Merge médio
1d 10h
PRs com merge (30d)
560

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.

Mais de python/cpython

Todas as issues de python/cpython

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

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