I call doctest.testmod() at the end of some my cell to run tests in my notebook.
But when I rerun this cell doctest raise some exception which forces me to restart all notebook.
Is there some function in doctest to reset its internal cache?
Cell[1]:
import doctest
class AttrDict(dict):
def __getattr__(self, key):
return self[key]
def foo():
'''
>>> foo()
{}
'''
return AttrDict()
doctest.testmod()
Cell[2]:
foo()
Cell[3]:
doctest.testmod()
Cell[3] raises next error:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[3], line 1
----> 1 doctest.testmod()
File ~/mambaforge/envs/nout/lib/python3.12/doctest.py:1995, in testmod(m, name, globs, verbose, report, optionflags, extraglobs, raise_on_error, exclude_empty)
1992 else:
1993 runner = DocTestRunner(verbose=verbose, optionflags=optionflags)
-> 1995 for test in finder.find(m, name, globs=globs, extraglobs=extraglobs):
1996 runner.run(test)
1998 if report:
File ~/mambaforge/envs/nout/lib/python3.12/doctest.py:948, in DocTestFinder.find(self, obj, name, module, globs, extraglobs)
946 # Recursively explore `obj`, extracting DocTests.
947 tests = []
--> 948 self._find(tests, obj, name, module, source_lines, globs, {})
949 # Sort the tests by alpha order of names, for consistency in
950 # verbose-mode output. This was a feature of doctest in Pythons
951 # <= 2.3 that got lost by accident in 2.4. It was repaired in
952 # 2.4.4 and 2.5.
953 tests.sort()
File ~/mambaforge/envs/nout/lib/python3.12/doctest.py:1020, in DocTestFinder._find(self, tests, obj, name, module, source_lines, globs, seen)
1017 valname = '%s.%s' % (name, valname)
1019 # Recurse to functions & classes.
-> 1020 if ((self._is_routine(val) or inspect.isclass(val)) and
1021 self._from_module(module, val)):
1022 self._find(tests, val, valname, module, source_lines,
1023 globs, seen)
1025 # Look for tests in a module's __test__ dictionary.
File ~/mambaforge/envs/nout/lib/python3.12/doctest.py:991, in DocTestFinder._is_routine(self, obj)
989 maybe_routine = obj
990 try:
--> 991 maybe_routine = inspect.unwrap(maybe_routine)
992 except ValueError:
993 pass
File ~/mambaforge/envs/nout/lib/python3.12/inspect.py:774, in unwrap(func, stop)
772 memo = {id(f): f}
773 recursion_limit = sys.getrecursionlimit()
--> 774 while _is_wrapper(func):
775 func = func.__wrapped__
776 id_func = id(func)
File ~/mambaforge/envs/nout/lib/python3.12/inspect.py:765, in unwrap.<locals>._is_wrapper(f)
764 def _is_wrapper(f):
--> 765 return hasattr(f, '__wrapped__')
Cell In[1], line 6, in AttrDict.__getattr__(self, key)
5 def __getattr__(self, key):
----> 6 return self[key]
KeyError: '__wrapped__'