-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
doctest doesn't allow duck-typing callables #65939
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
Comments
doctest uses inspect.isfunction() to detect callable objects on which to detect docstrings. Unfortunately, this prevents running doctests on functions which have been decorated to return other types of callables (for example numba's @jit decorator). In the attached example file, the wrapped "bar" function does not have its doctest executed. It would be useful for doctest to be more flexible here, although I'm not sure what the exact criterion should be. |
Would using callable() instead of inspect.isfunction() be ok? |
I'm not sure, because it would also select classes. I guess we need |
How about using this? diff -r 1e74350dd056 Lib/doctest.py
--- a/Lib/doctest.py Tue Jun 17 22:27:46 2014 -0500
+++ b/Lib/doctest.py Fri Jun 20 11:08:00 2014 +0300
@@ -984,7 +984,8 @@
for valname, val in obj.__dict__.items():
valname = '%s.%s' % (name, valname)
# Recurse to functions & classes.
- if ((inspect.isroutine(val) or inspect.isclass(val)) and
+
+ if ((inspect.isroutine(inspect.unwrap(val)) or inspect.isclass(val)) and
self._from_module(module, val)):
self._find(tests, val, valname, module, source_lines,
globs, seen) This seems to work for the given example and if the decorator uses update_wrapper or @wraps. |
Is there any reason to preclude classes? They could reasonably have docstrings that contain doctests. |
I don't know. I just didn't want to change doctest behaviour too much, |
Class doctests are already supported separately, see https://docs.python.org/3/library/doctest.html#which-docstrings-are-examined |
Here's a test patch which uses inspect.unwrap. Unfortunately, I can't test with numba, so I don't know if it works for that, but any decorated function which uses |
New changeset d22ca7496c54 by Yury Selivanov in branch 'default': |
Thank you for the patch, Claudiu! |
New changeset 12ef799a9a51 by Zachary Ware in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: