python / python/cpython

Doctest ignores methods decorated with `functools.singledispatchmethod`

Open
#129,578 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Lib/doctest.py at the class-dictionary handling around lines 1061-1073, then inspect functools.singledispatchmethod and the cached_property fix referenced in issue #107996. Add regression coverage for the example's singledispatchmethod docstring and verify doctest discovers both methods.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.