Skip to content

Commit

Permalink
BUG: collect doctests from docstrings of data descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
ev-br committed Dec 15, 2024
1 parent 5113870 commit 0b8683d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
14 changes: 14 additions & 0 deletions scipy_doctest/impl.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
import warnings
import inspect
import doctest
from doctest import NORMALIZE_WHITESPACE, ELLIPSIS, IGNORE_EXCEPTION_DETAIL
from itertools import zip_longest
Expand Down Expand Up @@ -538,6 +539,19 @@ def find(self, obj, name=None, module=None, globs=None, extraglobs=None):
# XXX: does this make similar checks in testmod/testfile duplicate?
if module not in self.config.skiplist:
tests = super().find(obj, name, module, globs, extraglobs)

if inspect.isclass(obj):
descriptors = [
(name_, method)
for name_, method in inspect.getmembers(obj)
if inspect.isdatadescriptor(method)
]

for name_, method in descriptors:
tests += super().find(
method, f'{name}.{name_}', module, globs, extraglobs
)

return tests


Expand Down
21 changes: 18 additions & 3 deletions scipy_doctest/tests/test_finder.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import pytest

import numpy as np

from . import finder_cases
from ..util import get_all_list, get_public_objects
from ..impl import DTFinder, DTConfig
from ..frontend import find_doctests


def test_get_all_list():
items, depr, other = get_all_list(finder_cases)
assert sorted(items) == ['Klass', 'func']
Expand Down Expand Up @@ -111,6 +114,7 @@ def test_get_doctests_strategy_api(self):
# - *private* stuff, which is not in `__all__`
wanted_names = ['Klass', 'Klass.meth']
wanted_names = [base] + [base + '.' + n for n in wanted_names]
wanted_names += [f'{base}.Klass.__weakref__']

assert sorted(names) == sorted(wanted_names)

Expand All @@ -129,6 +133,7 @@ def test_get_doctests_strategy_list(self):
# - the 'base' module (via the strategy=<list>)
wanted_names = ['Klass', 'Klass.meth']
wanted_names = [base + '.' + n for n in wanted_names]
wanted_names += [f'{base}.Klass.__weakref__']

assert sorted(names) == sorted(wanted_names)

Expand All @@ -139,7 +144,8 @@ def test_explicit_object_list():

base = 'scipy_doctest.tests.finder_cases'
assert ([test.name for test in tests] ==
[base + '.Klass', base + '.Klass.meth', base + '.Klass.meth_2'])
[f'{base}.Klass', f'{base}.Klass.meth', f'{base}.Klass.meth_2',
f'{base}.Klass.__weakref__'])


def test_explicit_object_list_with_module():
Expand All @@ -151,7 +157,8 @@ def test_explicit_object_list_with_module():

base = 'scipy_doctest.tests.finder_cases'
assert ([test.name for test in tests] ==
[base, base + '.Klass', base + '.Klass.meth', base + '.Klass.meth_2'])
[base, f'{base}.Klass', f'{base}.Klass.meth', f'{base}.Klass.meth_2',
f'{base}.Klass.__weakref__'])


def test_find_doctests_api():
Expand All @@ -161,10 +168,18 @@ def test_find_doctests_api():
base = 'scipy_doctest.tests.finder_cases'
assert ([test.name for test in tests] ==
[base + '.func', base + '.Klass', base + '.Klass.meth',
base + '.Klass.meth_2', base])
base + '.Klass.meth_2', base + '.Klass.__weakref__', base])


def test_dtfinder_config():
config = DTConfig()
finder = DTFinder(config=config)
assert finder.config is config


@pytest.mark.skipif(np.__version__ < '2', reason="XXX check if works on numpy 1.x")
def test_descriptors_get_collected():
tests = find_doctests(np, strategy=[np.dtype])
names = [test.name for test in tests]
assert 'numpy.dtype.kind' in names # was previously missing

0 comments on commit 0b8683d

Please sign in to comment.