Comment 10 for bug 1178807

Revision history for this message
John Vandenberg (jayvdb) wrote :

A stricter scope for doctest is easy to construct. The problem will be that we may need to create a few pyflakes options in order to accommodate various opinions of 'strict' ;-) pyflakes doesnt seem to like providing end-users with options -- only PYFLAKES_BUILTINS and PYFLAKES_DOCTEST exist. :/

I expect most people would expect that a strict doctest scope would include only what is exported using __all__, if __all__ was defined (and sympy appears to do that systematically). That would typically eliminate imports, requiring re-imports in the doctest to access any other modules.

That would mean a doctest is not quite a completely independent module; instead each doctest essentially has an implicit "from <current module> import *' at the beginning.

@asmeurer, would that be 'close enough' for your needs?

If that still isnt good enough, then I think we need to design user selection of at least three alternatives:
1. cpython doctest mode (default)
2. __all__ exported names only
3. empty scope

The obvious approach is some command line argument.

If the module includes a footer of `if __name__ == "__main__": import doctest; doctest.testmod()` , we could detect the list of desired globals by identifying `doctest.testmod(globs=[(name, globals()[name]) for name in __all__])` and `doctest.testmod(globs=None)` as a means of enabling option 2 or 3 respectively. The detection logic could also support extracting a custom list of valid names using a literal dict, i.e. doctest.testmod(globs={'a': 1, 'b': b}).

A little more difficult approach, which probably needs assistance from pep8/flake8, is to export the "inside doctest" status to flake8 plugins, so that people wanting a stricter rules within doctests only can create flake8 plugins to achieve their own objectives.