Skip to content
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

BUG: collect doctests from docstrings of data descriptors #180

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -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
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason to collect the descriptors, or could this be rewritten as

for name_, method in inspect.getmembers(obj):
    if inspect.isdatadescriptor(method):
        tests += super().find(
            method, f'{name}.{name_}', module, globs, extraglobs
        )

Not critical, whatever you think

return tests


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']
@@ -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)

@@ -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)

@@ -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():
@@ -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():
@@ -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