Currently doctest.testmod `globals` defaults to including everything in the module scope, and tools like pyflakes assume that doctests run with globals as a copy of __dict__ .
It is relatively simple to exclude everything using doctest.testmod(globs={}).
However to use a limited scope, such as only including attributes in __all__, we need to use `doctest.testmod(globs=[(name, globals()[name]) for name in __all__])`
However those solutions require that each module includes a `if __name__ == "__main__":` block that invokes doctest.testmod, and then other doctest related tools (pyflakes, nose, etc) need to *parse* the invocation to determine the desired globals contents.
It would be easier to control the globals using a module attribute that works like __all__, so that all doctest tools could easily determine the desired contents of globals.
e.g. the following could provide a sane reduced globals for doctests
__test_all__ = __all__ = ['foo']
As people sometimes add docstrings/doctest for functions not exported in __all__ for external use, in addition to the list of symbols in __test_all__, the doctest globals should include the function/class which the docstring/doctest is attached to.
See https://bugs.launchpad.net/pyflakes/+bug/1178807 for background to this enhancement request.
|