classification
Title: Easier way to specify reduced globals for doctest
Type: enhancement Stage:
Components: Tests Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: jayvdb, r.david.murray
Priority: normal Keywords:

Created on 2015-11-22 20:46 by jayvdb, last changed 2016-10-03 00:23 by jayvdb.

Messages (3)
msg255111 - (view) Author: John Mark Vandenberg (jayvdb) * Date: 2015-11-22 20:46
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.
msg255192 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-11-23 16:54
I don't understand what the problem is you are trying to solve.  Doctests are designed to run with a copy of the module global scope.  If pyflakes does not understand that, it is a pyflakes bug, but it doesn't sound like that's the issue.  It sounds like you want to use a different global scope, in which case I think the onus is on you to deal with it.

I'm -1 on this request.  I don't think the additional complexity is worthwhile.
msg277918 - (view) Author: John Mark Vandenberg (jayvdb) * Date: 2016-10-03 00:23
pyflakes does assume doctest run with a copy of the module scope.


However when there is an __all__, the module scope as seen by other modules 'should' be only items in __all__.  If a doctest is included in documentation, it 'should' only use names that are in __all__.

In addition, pyflakes has an outstanding feature request (https://bugs.launchpad.net/pyflakes/+bug/1178807/comments/8) that doctest run as if they are an independent module, and need to import everything that they use, so that each doctest is self-contained.

pyflakes can build support for both of those needs within the package.  No worries.  If you cant see a broader need for these, please close.
History
Date User Action Args
2016-10-03 00:23:15jayvdbsetmessages: + msg277918
2015-11-23 16:54:02r.david.murraysetnosy: + r.david.murray
messages: + msg255192
2015-11-22 20:46:20jayvdbcreate