@@ -879,14 +879,17 @@ def find(self, obj, name=None, module=None, globs=None, extraglobs=None):
879879
880880 if hasattr (obj , '__doctest_skip__' ) or hasattr (obj , '__doctest_requires__' ):
881881
882- def test_filter (test ):
882+ def conditionally_insert_skip (test ):
883+ """
884+ Insert skip statement if `test` matches `__doctest_(skip|requires)__`.
885+ """
883886 for pat in getattr (obj , '__doctest_skip__' , []):
884887 if pat == '*' :
885- return False
888+ self . _prepend_skip ( test )
886889 elif pat == '.' and test .name == name :
887- return False
890+ self . _prepend_skip ( test )
888891 elif fnmatch .fnmatch (test .name , '.' .join ((name , pat ))):
889- return False
892+ self . _prepend_skip ( test )
890893
891894 reqs = getattr (obj , '__doctest_requires__' , {})
892895 for pats , mods in reqs .items ():
@@ -903,14 +906,38 @@ def test_filter(test):
903906 else :
904907 continue # The pattern does not apply
905908
906- if not self . check_required_modules ( mods ) :
907- return False
909+ for mod in mods :
910+ self . _prepend_importorskip ( test , module = mod )
908911 return True
909912
910- tests = list (filter (test_filter , tests ))
913+ for _test in tests :
914+ conditionally_insert_skip (_test )
911915
912916 return tests
913917
918+ def _prepend_skip (self , test ):
919+ """Prepends `pytest.skip` before the doctest."""
920+ source = (
921+ "import pytest; "
922+ "pytest.skip('listed in `__doctest_skip__`'); "
923+ # Don't impact what's available in the namespace
924+ "del pytest"
925+ )
926+ importorskip = doctest .Example (source = source , want = "" )
927+ test .examples .insert (0 , importorskip )
928+
929+ def _prepend_importorskip (self , test , * , module ):
930+ """Prepends `pytest.importorskip` before the doctest."""
931+ source = (
932+ "import pytest; "
933+ # Hide output of this statement in `_`, otherwise doctests fail
934+ f"_ = pytest.importorskip({ module !r} ); "
935+ # Don't impact what's available in the namespace
936+ "del pytest"
937+ )
938+ importorskip = doctest .Example (source = source , want = "" )
939+ test .examples .insert (0 , importorskip )
940+
914941
915942def write_modified_file (fname , new_fname , changes , encoding = None ):
916943 # Sort in reversed order to edit the lines:
0 commit comments