classification
Title: Remove doctest import from heapq
Type: resource usage Stage: resolved
Components: Tests Versions: Python 3.10, Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: alexchandel, rhettinger, ronaldoussoren, steven.daprano
Priority: normal Keywords:

Created on 2020-07-14 21:49 by alexchandel, last changed 2020-07-19 07:37 by ronaldoussoren. This issue is now closed.

Messages (8)
msg373658 - (view) Author: alex c (alexchandel) * Date: 2020-07-14 21:49
heapq.py imports doctest in the last 4 lines to perform unit tests:

    if __name__ == "__main__":
    
        import doctest # pragma: no cover
        print(doctest.testmod()) # pragma: no cover

This disrupts dependency tracking modules and software, like modulegraph and pyinstaller, as doctest brings in many dependencies (including ctypes as of 3.8).

This functionality could be factored out into a separate unit-testing file.
msg373661 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-07-14 22:56
The idiom of a module running doctests on itself when executed as a script is a common idiom.

If modulegraph and pyinstaller can't cope a module importing another module from inside an if statement, that's a bug in them, not in the heapq module (and many others).

This requested change is a regression, taking away functionality. 3.8 and older are in feature freeze, so this could only apply to 3.9 and 3.10, and even then, I do not believe it should apply at all.
msg373663 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-07-14 23:50
I concur with Steven.
msg373809 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2020-07-17 09:52
I have no opinion on the proposed change.

The "disruption" alex c talks about is that this imports gets seen by modulegraph (and hence pyinstaller and py2app), which will then include doctest and all its dependencies in standalone bundles even though doctest isn't actually used.
msg373856 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-07-17 20:38
Ronald, can modulegraph be made smarter with respect to sections of code guarded by __name__ == '__main__'?    That Python idiom is old and pervasive.
msg373866 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-07-18 00:52
If not, could modulegraph add a flag to drop imports commonly found in main sections:  doctest, unittest, argparse, etc.?   

Asking the standard library to change seems like the tail wagging the dog — it only paints over the problem since third-party modules, other standard library modules, and user code commonly use main sections as well.
msg373880 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2020-07-18 07:27
modulegraph already knows where the import is done, and users of the library can use that information to make decisions. 

There's no need to make changes to either heapq.py or modulegraph.

For py2app I've made the choice to no be smart about inclusions and just try to include everything that may be imported because disk space is cheap these days.  I just exclude optional dependencies (in py2app, not modulegraph) when I've manually checked that it is safe to do so and the dependency is large.
msg373932 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-07-19 06:10
Can we close this?
History
Date User Action Args
2020-07-19 07:37:57ronaldoussorensetstatus: open -> closed
resolution: not a bug
stage: resolved
2020-07-19 06:10:43rhettingersetmessages: + msg373932
2020-07-18 07:27:54ronaldoussorensetmessages: + msg373880
2020-07-18 00:52:37rhettingersetmessages: + msg373866
2020-07-17 20:38:59rhettingersetmessages: + msg373856
2020-07-17 09:52:31ronaldoussorensetnosy: + ronaldoussoren
messages: + msg373809
2020-07-14 23:50:30rhettingersetnosy: + rhettinger
messages: + msg373663
2020-07-14 22:56:08steven.dapranosetnosy: + steven.daprano

messages: + msg373661
versions: - Python 3.5, Python 3.6, Python 3.7, Python 3.8
2020-07-14 21:49:24alexchandelcreate