Skip to content

Commit bfbaa6b

Browse files
committed
Issue python#27891: Consistently group and sort imports within idlelib modules.
1 parent 89b1162 commit bfbaa6b

47 files changed

Lines changed: 197 additions & 122 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Lib/idlelib/README.txt‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,23 @@ Help
228228

229229
<No menu>
230230
Center Insert # eEW.center_insert_event
231-
231+
232+
233+
CODE STYLE -- Generally PEP 8.
234+
235+
import
236+
------
237+
Put import at the top, unless there is a good reason otherwise.
238+
PEP 8 says to group stdlib, 3rd-party dependencies, and package imports.
239+
For idlelib, the groups are general stdlib, tkinter, and idlelib.
240+
Sort modules within each group, except that tkinter.ttk follows tkinter.
241+
Sort 'from idlelib import mod1' and 'from idlelib.mod2 import object'
242+
together by module, ignoring within module objects.
243+
Put 'import __main__' after other idlelib imports.
244+
245+
Imports only needed for testing are not at the top but are put in the
246+
htest function def or the "if __name__ == '__main__'" clause.
247+
248+
Within module imports like "from idlelib.mod import class" may cause
249+
circular imports to deadlock. Even without this, circular imports may
250+
require at least one of the imports to be delayed until a function call.

‎Lib/idlelib/autocomplete.py‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,27 @@
44
a window with all available names, for the user to select from.
55
"""
66
import os
7-
import sys
87
import string
8+
import sys
99

10-
from idlelib.config import idleConf
11-
12-
# This string includes all chars that may be in an identifier
13-
ID_CHARS = string.ascii_letters + string.digits + "_"
14-
15-
# These constants represent the two different types of completions
10+
# These constants represent the two different types of completions.
11+
# They must be defined here so autocomple_w can import them.
1612
COMPLETE_ATTRIBUTES, COMPLETE_FILES = range(1, 2+1)
1713

1814
from idlelib import autocomplete_w
15+
from idlelib.config import idleConf
1916
from idlelib.hyperparser import HyperParser
20-
2117
import __main__
2218

19+
# This string includes all chars that may be in an identifier.
20+
# TODO Update this here and elsewhere.
21+
ID_CHARS = string.ascii_letters + string.digits + "_"
22+
2323
SEPS = os.sep
2424
if os.altsep: # e.g. '/' on Windows...
2525
SEPS += os.altsep
2626

27+
2728
class AutoComplete:
2829

2930
menudefs = [

‎Lib/idlelib/autocomplete_w.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"""
44
from tkinter import *
55
from tkinter.ttk import Scrollbar
6-
from idlelib.multicall import MC_SHIFT
6+
77
from idlelib.autocomplete import COMPLETE_FILES, COMPLETE_ATTRIBUTES
8+
from idlelib.multicall import MC_SHIFT
89

910
HIDE_VIRTUAL_EVENT_NAME = "<<autocompletewindow-hide>>"
1011
HIDE_SEQUENCES = ("<FocusOut>", "<ButtonPress>")

‎Lib/idlelib/autoexpand.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
1313
This is an extension file and there is only one instance of AutoExpand.
1414
'''
15-
import string
1615
import re
16+
import string
1717

1818
###$ event <<expand-word>>
1919
###$ win <Alt-slash>
@@ -100,7 +100,6 @@ def getprevword(self):
100100
i = i-1
101101
return line[i:]
102102

103-
104103
if __name__ == '__main__':
105104
import unittest
106105
unittest.main('idlelib.idle_test.test_autoexpand', verbosity=2)

‎Lib/idlelib/browser.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
"""
1212

1313
import os
14-
import sys
1514
import pyclbr
15+
import sys
1616

17+
from idlelib.config import idleConf
1718
from idlelib import pyshell
18-
from idlelib.windows import ListedToplevel
1919
from idlelib.tree import TreeNode, TreeItem, ScrolledCanvas
20-
from idlelib.config import idleConf
20+
from idlelib.windows import ListedToplevel
2121

2222
file_open = None # Method...Item and Class...Item use this.
2323
# Normally pyshell.flist.open, but there is no pyshell.flist for htest.

‎Lib/idlelib/calltips.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
which disappear when you type a closing parenthesis.
66
77
"""
8-
import __main__
98
import inspect
109
import re
1110
import sys
@@ -14,6 +13,7 @@
1413

1514
from idlelib import calltip_w
1615
from idlelib.hyperparser import HyperParser
16+
import __main__
1717

1818
class CallTips:
1919

‎Lib/idlelib/codecontext.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
not open blocks are not shown in the context hints pane.
1010
1111
"""
12-
import tkinter
13-
from tkinter.constants import TOP, LEFT, X, W, SUNKEN
1412
import re
1513
from sys import maxsize as INFINITY
14+
15+
import tkinter
16+
from tkinter.constants import TOP, LEFT, X, W, SUNKEN
17+
1618
from idlelib.config import idleConf
1719

1820
BLOCKOPENERS = {"class", "def", "elif", "else", "except", "finally", "for",

‎Lib/idlelib/colorizer.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import time
2-
import re
3-
import keyword
41
import builtins
5-
from idlelib.delegator import Delegator
2+
import keyword
3+
import re
4+
import time
5+
66
from idlelib.config import idleConf
7+
from idlelib.delegator import Delegator
78

89
DEBUG = False
910

‎Lib/idlelib/config.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
"""
1919
# TODOs added Oct 2014, tjr
2020

21+
from configparser import ConfigParser
2122
import os
2223
import sys
2324

24-
from configparser import ConfigParser
2525
from tkinter.font import Font, nametofont
2626

2727
class InvalidConfigType(Exception): pass

‎Lib/idlelib/configdialog.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
"""
1212
from tkinter import *
1313
from tkinter.ttk import Scrollbar
14-
import tkinter.messagebox as tkMessageBox
1514
import tkinter.colorchooser as tkColorChooser
1615
import tkinter.font as tkFont
16+
import tkinter.messagebox as tkMessageBox
1717

1818
from idlelib.config import idleConf
19-
from idlelib.dynoption import DynOptionMenu
2019
from idlelib.config_key import GetKeysDialog
20+
from idlelib.dynoption import DynOptionMenu
21+
from idlelib import macosx
2122
from idlelib.query import SectionName, HelpSource
2223
from idlelib.tabbedpages import TabbedPageSet
2324
from idlelib.textview import view_text
24-
from idlelib import macosx
2525

2626
class ConfigDialog(Toplevel):
2727

0 commit comments

Comments
 (0)