homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Idlelib: document or move delayed imports
Type: enhancement Stage: needs patch
Components: IDLE Versions: Python 3.10
process
Status: open Resolution:
Dependencies: 27891 Superseder:
Assigned To: terry.reedy Nosy List: terry.reedy
Priority: normal Keywords:

Created on 2016年08月30日 01:54 by terry.reedy, last changed 2022年04月11日 14:58 by admin.

Messages (2)
msg273893 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016年08月30日 01:54
This issue depends and follows-up on #27891, consistently grouping and sorting imports in idlelib files.
PEP 8 (also) says 'all imports are put at the top of the file', though the 'consistency hobgloblin' rule allows for exceptions. Possible reasons include making circular imports work, delaying side-effects, and significantly reducing initial import time of the containing module. All are rare.
Idlelib currently has numerous exceptions, only a few of which have a documented reason. The others might have a good reason, but may be holdovers from when 'import at top' was absent or not observed. I propose to at least document the exceptions by putting comments at the top, in their sort location among other idlelib imports. Examples: 
# from idlelib import module # in EditorWindow
# from idlelib.other import SomeClass # in EditorWindow
Doing this will make it possible to see at a glance all the idlelib imports in a module.
I will remove at least one redundant delayed import and move some when I am sure there is no reason not to. I may ask original authors as to their reason for placing imports where they did.
Moving 'import X' from a function is easy and safe. Moving an import from a class requires deleting 'self.' from all references, so is not safe without extra care. At least for EditorWindow, I may leave this to when editing the class anyway.
msg273896 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016年08月30日 02:27
Another good reason for delaying an import is when the import is only needed for testing.
In module_x
...
def callable_x(parent) # htest #
 from tkinter import Toplevel
 box = Toplevel(parent)
 ...
if __name__ == '__main__':
 from unittest import main
 main('idlelib.idle_test.test_module_x', verbosity=2)
 from idlelib.idle_test.htest import run
 run(callable_x)
This should be part of 'Import Standards' in idlelib.README.
History
Date User Action Args
2022年04月11日 14:58:35adminsetgithub: 72079
2020年06月06日 22:08:56terry.reedysetversions: + Python 3.10, - Python 3.6
2016年08月30日 02:27:04terry.reedysetmessages: + msg273896
2016年08月30日 01:54:36terry.reedysetdependencies: + Consistently group and sort imports within idlelib modules.
2016年08月30日 01:54:22terry.reedycreate

AltStyle によって変換されたページ (->オリジナル) /