8000 Doctest ignores methods decorated with `functools.singledispatchmethod` · Issue #129578 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Doctest ignores methods decorated with functools.singledispatchmethod #129578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
viccie30 opened this issue Feb 2, 2025 · 0 comments
Open

Doctest ignores methods decorated with functools.singledispatchmethod #129578

viccie30 opened this issue Feb 2, 2025 · 0 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@viccie30
Copy link
Contributor
viccie30 commented Feb 2, 2025

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.

    cpython/Lib/doctest.py

    Lines 1061 to 1073 in df4a2f5

    if inspect.isclass(obj) and self._recurse:
    for valname, val in obj.__dict__.items():
    # Special handling for staticmethod/classmethod.
    if isinstance(val, (staticmethod, classmethod)):
    val = val.__func__
    # Recurse to methods, properties, and nested classes.
    if ((inspect.isroutine(val) or inspect.isclass(val) or
    isinstance(val, property)) and
    self._from_module(module, val)):
    valname = '%s.%s' % (name, valname)
    self._find(tests, val, valname, module, source_lines,
    globs, seen)
  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

@viccie30 viccie30 added the type-bug An unexpected behavior, bug, or error label Feb 2, 2025
@picnixz picnixz added the stdlib Python modules in the Lib dir label Feb 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants
0