python / python/cpython

Doctest ignores methods decorated with `functools.singledispatchmethod`

Aperta
#129,578 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib 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:

Doctest does not recognize tests in the docstrings of methods decorated with functools.singledispatchmethod. There's 2 causes:

  1. It checks the dictionary of classes for functions and subclasses, thereby not invoking descriptors. https://github.com/python/cpython/blob/df4a2f5bd74fc582d99e6a82e070058d7765f44d/Lib/doctest.py#L1061-L1073
  2. functools.singledispatchmethod does not call functools.update_wrapper on its instance, only on the wrapper function returned from its __get__ method.

A similar issue for cached_property was fixed in #107996.

Example

# mwe.py
import functools

class Class:
    @functools.singledispatchmethod
    def singledispatchmethod(self, arg):
        """
        >>> print(Class().singledispatchmethod(5))
        foo
        """
        return "foo"

    def regularmethod(self, arg):
        """
        >>> print(Class().regularmethod(5))
        foo
        """
        return "foo"
$ python3 -m doctest -v mwe.py
Trying:
    print(Class().regularmethod(5))
Expecting:
    foo
ok
2 items had no tests:
    mwe
    mwe.Class
1 item passed all tests:
   1 test in mwe.Class.regularmethod
1 test in 3 items.
1 passed.
Test passed.

doctest only finds one doctest, where there should be two.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-129579

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

Inizia in Lib/doctest.py, nella gestione del dizionario della classe intorno alle righe 1061-1073, quindi esamina functools.singledispatchmethod e la correzione di cached_property citata in issue #107996. Aggiungi una copertura di regressione per la docstring dell'esempio di singledispatchmethod e verifica che doctest rilevi entrambi i metodi.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.