From c942b9ac08b7b1b34e81c10ffa2eb68218a9adef Mon Sep 17 00:00:00 2001 From: Andrew Konstantaras Date: Wed, 24 Jan 2018 14:59:00 -0800 Subject: Fix 606, adding -k parameter to keep both direct and as imports --- isort/isort.py | 2 ++ isort/main.py | 2 ++ isort/settings.py | 1 + 3 files changed, 5 insertions(+) diff --git a/isort/isort.py b/isort/isort.py index 945b5489..f5e97831 100644 --- a/isort/isort.py +++ b/isort/isort.py @@ -391,6 +391,8 @@ class SortImports(object): continue if module in self.as_map: + if self.config['keep_direct_and_as_imports']: + import_definition = "import {0}".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, -- cgit v1.2.1 From cd5d6fd29f8b61356914d1ba3ed5362a559dedc6 Mon Sep 17 00:00:00 2001 From: Andrew Konstantaras Date: Wed, 24 Jan 2018 15:05:45 -0800 Subject: Fix 606, adding -k parameter to keep both direct and as imports and included a test --- test_isort.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test_isort.py b/test_isort.py index a2b79648..d0068580 100644 --- a/test_isort.py +++ b/test_isort.py @@ -1056,6 +1056,11 @@ 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 \n" + "import os as _os") + test_output = ("import os \n" + "import 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(): -- cgit v1.2.1 From f831d631fdfd348ad959339d571dab77002fdb6f Mon Sep 17 00:00:00 2001 From: Andrew Konstantaras Date: Wed, 24 Jan 2018 15:15:36 -0800 Subject: Fix 606, adding -k parameter to keep both direct and as imports and included a test, removed extra space in test --- test_isort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_isort.py b/test_isort.py index d0068580..28672c3c 100644 --- a/test_isort.py +++ b/test_isort.py @@ -1058,7 +1058,7 @@ def test_combined_from_and_as_imports(): assert SortImports(file_contents=test_input, combine_as_imports=True).output == test_input test_input = ("import os \n" "import os as _os") - test_output = ("import os \n" + test_output = ("import os\n" "import os as _os\n") assert SortImports(file_contents=test_input, keep_direct_and_as_imports=True).output == test_output -- cgit v1.2.1 From 8c3d31c61f065695497c50eb5c77343e569629d2 Mon Sep 17 00:00:00 2001 From: Andrew Konstantaras Date: Wed, 24 Jan 2018 15:19:05 -0800 Subject: Fix 606, adding -k parameter to keep both direct and as imports and included a test, removed extra space in test --- test_isort.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test_isort.py b/test_isort.py index 28672c3c..feaf7ccb 100644 --- a/test_isort.py +++ b/test_isort.py @@ -1056,10 +1056,8 @@ 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 \n" - "import os as _os") - test_output = ("import os\n" - "import os as _os\n") + 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 -- cgit v1.2.1 From fc85cc93b90bb3d2a6426728b35a5827ea14e717 Mon Sep 17 00:00:00 2001 From: Andrew Konstantaras Date: Wed, 24 Jan 2018 15:29:17 -0800 Subject: Fix 606, adding -k parameter to keep both direct and as imports and included a test, removed extra space in test. Add import_definition concat --- isort/isort.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/isort/isort.py b/isort/isort.py index f5e97831..c952557d 100644 --- a/isort/isort.py +++ b/isort/isort.py @@ -391,9 +391,10 @@ class SortImports(object): continue if module in self.as_map: + import_definition = '' if self.config['keep_direct_and_as_imports']: - import_definition = "import {0}".format(module) - import_definition = "import {0} as {1}".format(module, self.as_map[module]) + 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) -- cgit v1.2.1