diff options
author | Timothy Edmund Crosley <timothy.crosley@gmail.com> | 2018-01-24 15:43:33 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-24 15:43:33 -0800 |
commit | 01774c12ffc386fc7ec3226fa34852f6e7c71fbe (patch) | |
tree | 920f4e7097999bea7167848def50f78f233d8964 | |
parent | 8048b77e65eaed6780358498c03e296e6e650a29 (diff) | |
parent | fc85cc93b90bb3d2a6426728b35a5827ea14e717 (diff) | |
download | isort-01774c12ffc386fc7ec3226fa34852f6e7c71fbe.tar.gz |
Merge pull request #642 from akonsta/Fix606
Fix606
-rw-r--r-- | isort/isort.py | 5 | ||||
-rwxr-xr-x | isort/main.py | 2 | ||||
-rw-r--r-- | isort/settings.py | 1 | ||||
-rw-r--r-- | test_isort.py | 3 |
4 files changed, 10 insertions, 1 deletions
diff --git a/isort/isort.py b/isort/isort.py index 6040d60f..96f16f45 100644 --- a/isort/isort.py +++ b/isort/isort.py @@ -390,7 +390,10 @@ class SortImports(object): continue if module in self.as_map: - import_definition = "import {0} as {1}".format(module, self.as_map[module]) + import_definition = '' + if self.config['keep_direct_and_as_imports']: + import_definition = "import {0}\n".format(module) + import_definition += "import {0} as {1}".format(module, self.as_map[module]) else: import_definition = "import {0}".format(module) diff --git a/isort/main.py b/isort/main.py index c1d76619..b831a3ce 100755 --- a/isort/main.py +++ b/isort/main.py @@ -213,6 +213,8 @@ def create_parser(): dest='indent', type=str) parser.add_argument('-j', '--jobs', help='Number of files to process in parallel.', dest='jobs', type=int) + parser.add_argument('-k', '--keep-direct-and-as', dest='keep_direct_and_as_imports', action='store_true', + help="Turns off default behavior that removes direct imports when as imports exist.") parser.add_argument('-l', '--lines', help='[Deprecated] The max length of an import line (used for wrapping ' 'long imports).', dest='line_length', type=int) diff --git a/isort/settings.py b/isort/settings.py index c940726f..09feb5dc 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -124,6 +124,7 @@ default = {'force_to_top': [], 'lines_between_types': 0, 'combine_as_imports': False, 'combine_star': False, + 'keep_direct_and_as_imports': False, 'include_trailing_comma': False, 'from_first': False, 'verbose': False, diff --git a/test_isort.py b/test_isort.py index bec4db00..9bcaf4aa 100644 --- a/test_isort.py +++ b/test_isort.py @@ -1056,6 +1056,9 @@ def test_combined_from_and_as_imports(): "from translate.storage import base, factory\n" "from translate.storage.placeables import general, parse as rich_parse\n") assert SortImports(file_contents=test_input, combine_as_imports=True).output == test_input + test_input = ("import os \nimport os as _os") + test_output = ("import os\nimport os as _os\n") + assert SortImports(file_contents=test_input, keep_direct_and_as_imports=True).output == test_output def test_as_imports_with_line_length(): |