[issue26303] Shared execution context between doctests in a module

New submission from kernc: The doctest execution context documentation [0] says the tests get shallow *copies* of module's globals, so one test can't mingle with results of another. This makes it impossible to make literate modules such as: """ This module is about reusable doctests context. Examples -------- Let's prepare something the later examples can work with: >>> import foo >>> result = foo.Something() 2 """ class Bar: """ Class about something. >>> bar = Bar(foo) >>> bar.uses(foo) True """ def baz(self): """ Returns 3. >>> result + bar.baz() 5 """ return 3 I.e. one has to instantiate everything in every single test. The documentation says one can pass their own globals as `glob=your_dict`, but it doesn't mention the dict is *cleared* after the test run. Please acknowledge the use case of doctests in a module sharing their environment and results sometimes legitimately exists, and to make it future-compatible, please amend the final paragraph of the relevant part of documentation [0] like so: You can force use of your own dict as the execution context by passing `globs=your_dict` to `testmod()` or `testfile()` instead, e.g., to have all doctests in a module use the _same_ execution context (sharing variables), define a context like so: class Context(dict): def copy(self): return self def clear(self): pass and use it, optionally prepopulated with `M`'s globals: doctest.testmod(module, glob=Context(module.__dict__.copy())) Thank you! [0]: https://docs.python.org/3/library/doctest.html#what-s-the-execution-context ---------- assignee: docs@python components: Documentation messages: 259731 nosy: docs@python, kernc priority: normal severity: normal status: open title: Shared execution context between doctests in a module type: enhancement versions: Python 3.6 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue26303> _______________________________________

kernc added the comment: The use case is not unpopular [1] and with unambiguous examples like >>> arr = np.arange(5) >>> my_sum(arr) 10 (i.e., without importing numpy as np everywhere), it also makes a lot of sense. [1]: http://stackoverflow.com/questions/13106118/object-reuse-in-python-doctest/3... ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue26303> _______________________________________

anthony shaw <anthonyshaw@apache.org> added the comment: this issue would be good for pycon sprints ---------- nosy: +Mariatta, anthonypjshaw _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue26303> _______________________________________

Change by anthony shaw <anthonyshaw@apache.org>: ---------- assignee: docs@python -> Mariatta _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue26303> _______________________________________
participants (2)
-
anthony shaw
-
kernc