python / python/cpython

`singledispatchmethod` inspected signature incorrectly includes `self`

Aperta Adatta ai principianti
#117,735 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Leggi Lib/functools.py intorno alla riga 979, concentrandoti su come singledispatchmethod.get avvolge il metodo associato. Esegui la riproduzione fornita di inspect.signature e sig.bind; il lavoro è completato quando la firma associata omette self, accetta l'argomento effettivo del metodo e i test pertinenti hanno esito positivo.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
68/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.