python / python/cpython

`singledispatchmethod` inspected signature incorrectly includes `self`

Abierto Apto para principiantes
#117,735 0 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

type-bug
Lenguaje dominante
Python
Estrellas
77.2k
Forks
36k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Lee Lib/functools.py alrededor de la línea 979, centrándote en cómo singledispatchmethod.get envuelve el método enlazado. Ejecuta la reproducción proporcionada de inspect.signature y sig.bind; se considera terminado cuando la firma enlazada omite self, acepta el argumento real del método y pasan las pruebas relevantes.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
tooling
Tipo de issue
Error
Dificultad
2/5
Tiempo estimado
1-3 horas
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
68/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.