From fe93a74c5f143e4aac99e167aed524286bc039b1 Mon Sep 17 00:00:00 2001 From: Evgeni Burovski Date: Sun, 15 Dec 2024 18:31:57 +0200 Subject: [PATCH] BUG: collect methods from private superclasses --- scipy_doctest/frontend.py | 8 ++++++++ scipy_doctest/tests/finder_cases_2.py | 22 ++++++++++++++++++++++ scipy_doctest/tests/test_finder.py | 12 ++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 scipy_doctest/tests/finder_cases_2.py diff --git a/scipy_doctest/frontend.py b/scipy_doctest/frontend.py index 25fcb68..6361602 100644 --- a/scipy_doctest/frontend.py +++ b/scipy_doctest/frontend.py @@ -97,6 +97,14 @@ def find_doctests(module, strategy=None, t = _finder.find(item, name, globs=globs, extraglobs=extraglobs) else: t = finder.find(item, full_name, globs=globs, extraglobs=extraglobs) + + if hasattr(item, '__mro__'): + # is a class, inspect superclasses + # cf https://github.com/scipy/scipy_doctest/issues/177 + # item.__mro__ starts with itself, ends with `object` + for item_ in item.__mro__[1:-1]: + t_ = finder.find(item_, full_name, globs=globs, extraglobs=extraglobs) + t += t_ tests += t # If the skiplist contains methods of objects, their doctests may have been diff --git a/scipy_doctest/tests/finder_cases_2.py b/scipy_doctest/tests/finder_cases_2.py new file mode 100644 index 0000000..e049e3d --- /dev/null +++ b/scipy_doctest/tests/finder_cases_2.py @@ -0,0 +1,22 @@ +""" +Private method in subclasses +""" + +__all__ = ["Klass"] + +class _PrivateKlass: + def private_method(self): + """ + >>> 2 / 3 + 0.667 + """ + pass + + +class Klass(_PrivateKlass): + def public_method(self): + """ + >>> 3 / 4 + 0.74 + """ + pass diff --git a/scipy_doctest/tests/test_finder.py b/scipy_doctest/tests/test_finder.py index 5453e38..b818e93 100644 --- a/scipy_doctest/tests/test_finder.py +++ b/scipy_doctest/tests/test_finder.py @@ -1,6 +1,7 @@ import pytest from . import finder_cases +from . import finder_cases_2 from ..util import get_all_list, get_public_objects from ..impl import DTFinder, DTConfig from ..frontend import find_doctests @@ -168,3 +169,14 @@ def test_dtfinder_config(): config = DTConfig() finder = DTFinder(config=config) assert finder.config is config + + +@pytest.mark.parametrize('strategy', [None, 'api']) +def test_private_superclasses(strategy): + # Test that methods from inherited private superclasses get collected + tests = find_doctests(finder_cases_2, strategy=strategy) + + names = set(test.name.split('.')[-1] for test in tests) + assert len(tests) == 3 + assert names == {'finder_cases_2', 'public_method', 'private_method'} +