diff options
author | Timothy Crosley <timothy.crosley@gmail.com> | 2015-02-05 17:39:59 -0500 |
---|---|---|
committer | Timothy Crosley <timothy.crosley@gmail.com> | 2015-02-05 17:39:59 -0500 |
commit | fdbd2e34ab0c753ef7e0af9a8aaa713efe23980a (patch) | |
tree | 8b5630f9e142884f5c10650e7d667377602d7e99 | |
parent | 0a6a4af6e523a803a521713120ecbeed6622621e (diff) | |
download | isort-fdbd2e34ab0c753ef7e0af9a8aaa713efe23980a.tar.gz |
Enforce consistency
-rw-r--r-- | isort/isort.py | 10 | ||||
-rw-r--r-- | test_isort.py | 2 |
2 files changed, 9 insertions, 3 deletions
diff --git a/isort/isort.py b/isort/isort.py index f08dbc05..9703de8f 100644 --- a/isort/isort.py +++ b/isort/isort.py @@ -258,7 +258,7 @@ class SortImports(object): return self.index == self.number_of_lines @staticmethod - def _module_key(module_name, config, sub_imports=False): + def _module_key(module_name, config, sub_imports=False, already_sorted=None): prefix = "" module_name = str(module_name) if sub_imports and config['order_by_type']: @@ -269,6 +269,11 @@ class SortImports(object): else: prefix = "C" module_name = module_name.lower() + if already_sorted: + if module_name in already_sorted: + module_name = moudle_name + "a" + already_sorted.append(module_name) + return "{0}{1}{2}".format(module_name in config['force_to_top'] and "A" or "B", prefix, config['length_sort'] and (str(len(module_name)) + ":" + module_name) or module_name) @@ -426,7 +431,8 @@ class SortImports(object): straight_modules = list(self.imports[section]['straight']) straight_modules = natsorted(straight_modules, key=lambda key: self._module_key(key, self.config)) from_modules = list(self.imports[section]['from'].keys()) - from_modules = natsorted(from_modules, key=lambda key: self._module_key(key, self.config)) + already_sorted = [] + from_modules = natsorted(from_modules, key=lambda key: self._module_key(key, self.config, already_sorted)) section_output = [] if self.config.get('from_first', False): diff --git a/test_isort.py b/test_isort.py index 63b3a816..2aeb474c 100644 --- a/test_isort.py +++ b/test_isort.py @@ -1251,4 +1251,4 @@ def test_top_comments(): def test_consistency(): """Ensures consistency of handling even when dealing with non ordered-by-type imports""" test_input = "from sqlalchemy.dialects.postgresql import ARRAY, array\n" - assert SortImports(file_contents=test_input, order_by_type=True).output == test_input + assert SortImports(file_contents=test_input, order_by_type=False).output == test_input |