Skip to content

doctest fails to collect tests from a C function that has been wrapped #117692

Closed
@lpsinger

Description

@lpsinger

Bug report

Bug description:

doctest.DocTestFinder is now failing to collect examples from functions that are defined in C and then wrapped. It still works just fine with functions that are defined in C but that are not wrapped.

This bug was introduced by #115440. It breaks doctests for Numpy ufuncs in pytest-doctestplus (see scientific-python/pytest-doctestplus#248).

I have placed reproducer code in this Gist: https://gist.github.com/lpsinger/65e59728555dc2096af88d394e2d4a6b. To reproduce, retrieve the code and run the following commands:

pip install -e .
python test.py

The script test.py fails with this error message:

$ python test.py 
Traceback (most recent call last):
  File "/Users/lpsinger/src/doctest-func-without-code/test.py", line 14, in <module>
    assert len(finder.find(bar.hello)[0].examples) == 1
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/doctest.py", line 942, in find
    self._find(tests, obj, name, module, source_lines, globs, {})
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/doctest.py", line 1004, in _find
    test = self._get_test(obj, name, module, globs, source_lines)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/doctest.py", line 1072, in _get_test
    lineno = self._find_lineno(obj, source_lines)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/doctest.py", line 1121, in _find_lineno
    obj = inspect.unwrap(obj).__code__
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'builtin_function_or_method' object has no attribute '__code__'. Did you mean: '__call__'?

CPython versions tested on:

3.9

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

Labels

3.13bugs and security fixesstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions