Skip to content

Commit 3b6b6e3

Browse files
porglezompDonJayamanne
authored andcommitted
Update isort to latest version (4.2.15) (microsoft#1135)
Fixes microsoft#1134 - "Sort Imports messing up some standard library modules"
1 parent fa16982 commit 3b6b6e3

File tree

7 files changed

+328
-154
lines changed

7 files changed

+328
-154
lines changed

pythonFiles/isort/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@
2525
from . import settings
2626
from .isort import SortImports
2727

28-
__version__ = "4.2.5"
28+
__version__ = "4.2.15"
File renamed without changes.

pythonFiles/isort/isort.py

Lines changed: 162 additions & 89 deletions
Large diffs are not rendered by default.

pythonFiles/isort/main.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
from isort import SortImports, __version__
3131
from isort.settings import DEFAULT_SECTIONS, default, from_path, should_skip
3232

33-
from .pie_slice import *
33+
from .pie_slice import itemsview
3434

3535

36-
INTRO = """
37-
/#######################################################################\\
36+
INTRO = r"""
37+
/#######################################################################\
3838
3939
`sMMy`
4040
.yyyy- `
@@ -130,7 +130,7 @@ def run(self):
130130
if incorrectly_sorted:
131131
wrong_sorted_files = True
132132
except IOError as e:
133-
print("WARNING: Unable to parse file {0} due to {1}".format(file_name, e))
133+
print("WARNING: Unable to parse file {0} due to {1}".format(python_file, e))
134134
if wrong_sorted_files:
135135
exit(1)
136136

@@ -164,37 +164,35 @@ def create_parser():
164164
help='Force sortImports to recognize a module as being part of the current python project.')
165165
parser.add_argument('--virtual-env', dest='virtual_env',
166166
help='Virtual environment to use for determining whether a package is third-party')
167-
parser.add_argument('-m', '--multi_line', dest='multi_line_output', type=int, choices=[0, 1, 2, 3, 4, 5],
167+
parser.add_argument('-m', '--multi-line', dest='multi_line_output', type=int, choices=[0, 1, 2, 3, 4, 5],
168168
help='Multi line output (0-grid, 1-vertical, 2-hanging, 3-vert-hanging, 4-vert-grid, '
169169
'5-vert-grid-grouped).')
170170
parser.add_argument('-i', '--indent', help='String to place for indents defaults to " " (4 spaces).',
171171
dest='indent', type=str)
172-
parser.add_argument('-a', '--add_import', dest='add_imports', action='append',
172+
parser.add_argument('-a', '--add-import', dest='add_imports', action='append',
173173
help='Adds the specified import line to all files, '
174174
'automatically determining correct placement.')
175-
parser.add_argument('-af', '--force_adds', dest='force_adds', action='store_true',
175+
parser.add_argument('-af', '--force-adds', dest='force_adds', action='store_true',
176176
help='Forces import adds even if the original file is empty.')
177-
parser.add_argument('-r', '--remove_import', dest='remove_imports', action='append',
177+
parser.add_argument('-r', '--remove-import', dest='remove_imports', action='append',
178178
help='Removes the specified import from all files.')
179-
parser.add_argument('-ls', '--length_sort', help='Sort imports by their string length.',
180-
dest='length_sort', action='store_true', default=False)
179+
parser.add_argument('-ls', '--length-sort', help='Sort imports by their string length.',
180+
dest='length_sort', action='store_true')
181181
parser.add_argument('-d', '--stdout', help='Force resulting output to stdout, instead of in-place.',
182182
dest='write_to_stdout', action='store_true')
183-
parser.add_argument('-c', '--check-only', action='store_true', default=False, dest="check",
183+
parser.add_argument('-c', '--check-only', action='store_true', dest="check",
184184
help='Checks the file for unsorted / unformatted imports and prints them to the '
185185
'command line without modifying the file.')
186-
parser.add_argument('-ws', '--enforce-white-space', action='store_true', default=False, dest="enforce_white_space",
187-
help='Tells isort to enforce white space difference when --check-only is being used.')
186+
parser.add_argument('-ws', '--ignore-whitespace', action='store_true', dest="ignore_whitespace",
187+
help='Tells isort to ignore whitespace differences when --check-only is being used.')
188188
parser.add_argument('-sl', '--force-single-line-imports', dest='force_single_line', action='store_true',
189189
help='Forces all from imports to appear on their own line')
190-
parser.add_argument('--force_single_line_imports', dest='force_single_line', action='store_true',
191-
help=argparse.SUPPRESS)
192190
parser.add_argument('-ds', '--no-sections', help='Put all imports into the same section bucket', dest='no_sections',
193191
action='store_true')
194192
parser.add_argument('-sd', '--section-default', dest='default_section',
195193
help='Sets the default section for imports (by default FIRSTPARTY) options: ' +
196194
str(DEFAULT_SECTIONS))
197-
parser.add_argument('-df', '--diff', dest='show_diff', default=False, action='store_true',
195+
parser.add_argument('-df', '--diff', dest='show_diff', action='store_true',
198196
help="Prints a diff of all the changes isort would make to a file, instead of "
199197
"changing it in place")
200198
parser.add_argument('-e', '--balanced', dest='balanced_wrapping', action='store_true',
@@ -218,22 +216,25 @@ def create_parser():
218216
help='Shows verbose output, such as when files are skipped or when a check is successful.')
219217
parser.add_argument('-q', '--quiet', action='store_true', dest="quiet",
220218
help='Shows extra quiet output, only errors are outputted.')
221-
parser.add_argument('-sp', '--settings-path', dest="settings_path",
219+
parser.add_argument('-sp', '--settings-path', dest="settings_path",
222220
help='Explicitly set the settings path instead of auto determining based on file location.')
223221
parser.add_argument('-ff', '--from-first', dest='from_first',
224222
help="Switches the typical ordering preference, showing from imports first then straight ones.")
225223
parser.add_argument('-wl', '--wrap-length', dest='wrap_length',
226224
help="Specifies how long lines that are wrapped should be, if not set line_length is used.")
227-
parser.add_argument('-fgw', '--force-grid-wrap', action='store_true', dest="force_grid_wrap",
228-
help='Force from imports to be grid wrapped regardless of line length')
229-
parser.add_argument('-fass', '--force-alphabetical-sort-within-sections', action='store_true',
225+
parser.add_argument('-fgw', '--force-grid-wrap', nargs='?', const=2, type=int, dest="force_grid_wrap",
226+
help='Force number of from imports (defaults to 2) to be grid wrapped regardless of line '
227+
'length')
228+
parser.add_argument('-fass', '--force-alphabetical-sort-within-sections', action='store_true',
230229
dest="force_alphabetical_sort", help='Force all imports to be sorted alphabetically within a '
231230
'section')
232-
parser.add_argument('-fas', '--force-alphabetical-sort', action='store_true', dest="force_alphabetical_sort",
231+
parser.add_argument('-fas', '--force-alphabetical-sort', action='store_true', dest="force_alphabetical_sort",
233232
help='Force all imports to be sorted as a single section')
234-
parser.add_argument('-fss', '--force-sort-within-sections', action='store_true', dest="force_sort_within_sections",
233+
parser.add_argument('-fss', '--force-sort-within-sections', action='store_true', dest="force_sort_within_sections",
235234
help='Force imports to be sorted by module, independent of import_type')
236235
parser.add_argument('-lbt', '--lines-between-types', dest='lines_between_types', type=int)
236+
parser.add_argument('-up', '--use-parentheses', dest='use_parentheses', action='store_true',
237+
help='Use parenthesis for line continuation on lenght limit instead of slashes.')
237238

238239
arguments = dict((key, value) for (key, value) in itemsview(vars(parser.parse_args())) if value)
239240
if 'dont_order_by_type' in arguments:
@@ -267,7 +268,7 @@ def main():
267268
if arguments.get('recursive', False):
268269
file_names = iter_source_code(file_names, config, skipped)
269270
num_skipped = 0
270-
if config.get('verbose', False) or config.get('show_logo', False):
271+
if config['verbose'] or config.get('show_logo', False):
271272
print(INTRO)
272273
for file_name in file_names:
273274
try:

pythonFiles/isort/natural.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def _atoi(text):
3333

3434

3535
def _natural_keys(text):
36-
return [_atoi(c) for c in re.split('(\d+)', text)]
36+
return [_atoi(c) for c in re.split(r'(\d+)', text)]
3737

3838

3939
def nsorted(to_sort, key=None):

pythonFiles/isort/pie_slice.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from __future__ import absolute_import
2323

2424
import abc
25+
import collections
2526
import functools
2627
import sys
2728
from numbers import Integral
@@ -67,6 +68,7 @@ class Form(with_metaclass(FormType, BaseForm)):
6768
class metaclass(meta):
6869
__call__ = type.__call__
6970
__init__ = type.__init__
71+
7072
def __new__(cls, name, this_bases, d):
7173
if this_bases is None:
7274
return type.__new__(cls, name, (), d)
@@ -118,6 +120,7 @@ def __instancecheck__(cls, instance):
118120
import builtins
119121
from urllib import parse
120122

123+
input = input
121124
integer_types = (int, )
122125

123126
def u(string):
@@ -428,7 +431,7 @@ def __eq__(self, other):
428431
if isinstance(other, OrderedDict):
429432
if len(self) != len(other):
430433
return False
431-
for p, q in zip(self.items(), other.items()):
434+
for p, q in zip(self.items(), other.items()):
432435
if p != q:
433436
return False
434437
return True
@@ -526,3 +529,66 @@ def cache_clear():
526529

527530
else:
528531
from functools import lru_cache
532+
533+
534+
class OrderedSet(collections.MutableSet):
535+
536+
def __init__(self, iterable=None):
537+
self.end = end = []
538+
end += [None, end, end]
539+
self.map = {}
540+
if iterable is not None:
541+
self |= iterable
542+
543+
def __len__(self):
544+
return len(self.map)
545+
546+
def __contains__(self, key):
547+
return key in self.map
548+
549+
def add(self, key):
550+
if key not in self.map:
551+
end = self.end
552+
curr = end[1]
553+
curr[2] = end[1] = self.map[key] = [key, curr, end]
554+
555+
def discard(self, key):
556+
if key in self.map:
557+
key, prev, next = self.map.pop(key)
558+
prev[2] = next
559+
next[1] = prev
560+
561+
def __iter__(self):
562+
end = self.end
563+
curr = end[2]
564+
while curr is not end:
565+
yield curr[0]
566+
curr = curr[2]
567+
568+
def __reversed__(self):
569+
end = self.end
570+
curr = end[1]
571+
while curr is not end:
572+
yield curr[0]
573+
curr = curr[1]
574+
575+
def pop(self, last=True):
576+
if not self:
577+
raise KeyError('set is empty')
578+
key = self.end[1][0] if last else self.end[2][0]
579+
self.discard(key)
580+
return key
581+
582+
def __repr__(self):
583+
if not self:
584+
return '%s()' % (self.__class__.__name__,)
585+
return '%s(%r)' % (self.__class__.__name__, list(self))
586+
587+
def __eq__(self, other):
588+
if isinstance(other, OrderedSet):
589+
return len(self) == len(other) and list(self) == list(other)
590+
return set(self) == set(other)
591+
592+
def update(self, other):
593+
for item in other:
594+
self.add(item)

0 commit comments

Comments
 (0)