summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Crosley <timothy.crosley@gmail.com>2020-01-07 12:01:42 -0800
committerTimothy Crosley <timothy.crosley@gmail.com>2020-01-07 12:01:42 -0800
commitad49f79cfd88807190e20d664a799e8a660a79d8 (patch)
tree842935f99d75709a0a77504e15edabdf299bb671
parent70c904d8c951df3faaa8ee3573c8bb9eb7203814 (diff)
downloadisort-feature/fix-issue-969.tar.gz
Fix issue #969: Add support for single line exclusionsfeature/fix-issue-969
-rw-r--r--isort/main.py7
-rw-r--r--isort/output.py6
-rw-r--r--isort/profiles.py7
-rw-r--r--isort/settings.py1
-rw-r--r--tests/test_isort.py20
5 files changed, 38 insertions, 3 deletions
diff --git a/isort/main.py b/isort/main.py
index b9b86687..459e786e 100644
--- a/isort/main.py
+++ b/isort/main.py
@@ -423,6 +423,13 @@ def parse_args(argv: Optional[Sequence[str]] = None) -> Dict[str, Any]:
help="Forces all from imports to appear on their own line",
)
parser.add_argument(
+ "--nsl",
+ "--single-line-exclusions",
+ help="One or more modules to exclude from the single line rule.",
+ dest="single_line_exclusions",
+ action="append",
+ )
+ parser.add_argument(
"--sp",
"--settings-path",
dest="settings_path",
diff --git a/isort/output.py b/isort/output.py
index cd925c91..27fbd397 100644
--- a/isort/output.py
+++ b/isort/output.py
@@ -239,7 +239,9 @@ def _with_from_imports(
import_start = f"from {module} {import_type} "
from_imports = list(parsed.imports[section]["from"][module])
- if not config.no_inline_sort or config.force_single_line:
+ if not config.no_inline_sort or (
+ config.force_single_line and module not in config.single_line_exclusions
+ ):
from_imports = sorting.naturally(
from_imports,
key=lambda key: sorting.module_key(
@@ -288,7 +290,7 @@ def _with_from_imports(
config,
)
from_imports = []
- elif config.force_single_line:
+ elif config.force_single_line and module not in config.single_line_exclusions:
import_statement = ""
while from_imports:
from_import = from_imports.pop(0)
diff --git a/isort/profiles.py b/isort/profiles.py
index 1210c8ef..910da935 100644
--- a/isort/profiles.py
+++ b/isort/profiles.py
@@ -15,7 +15,12 @@ django = {
"line_length": 79,
}
pycharm = {"multi_line_output": 3, "force_grid_wrap": 2}
-google = {"force_single_line": True, "force_sort_within_sections": True, "lexicographical": True}
+google = {
+ "force_single_line": True,
+ "force_sort_within_sections": True,
+ "lexicographical": True,
+ "single_line_exclusions": ("typing",),
+}
open_stack = {
"force_single_line": True,
"force_sort_within_sections": True,
diff --git a/isort/settings.py b/isort/settings.py
index 04b488f1..f51d78c8 100644
--- a/isort/settings.py
+++ b/isort/settings.py
@@ -142,6 +142,7 @@ class _Config:
remove_imports: FrozenSet[str] = frozenset()
reverse_relative: bool = False
force_single_line: bool = False
+ single_line_exclusions: Tuple[str, ...] = ()
default_section: str = "FIRSTPARTY"
import_headings: Dict[str, str] = field(default_factory=dict)
balanced_wrapping: bool = False
diff --git a/tests/test_isort.py b/tests/test_isort.py
index 0228b414..efff42c4 100644
--- a/tests/test_isort.py
+++ b/tests/test_isort.py
@@ -4702,3 +4702,23 @@ from flask_security.signals import user_confirmed # noqa
from flask_security.signals import user_registered # noqa
"""
assert SortImports(file_contents=test_input).output == expected_output
+
+
+def test_single_line_exclusions():
+ test_input = """
+# start comment
+from os import path, system
+from typing import List, TypeVar
+"""
+ expected_output = """
+# start comment
+from os import path
+from os import system
+from typing import List, TypeVar
+"""
+ assert (
+ SortImports(
+ file_contents=test_input, force_single_line=True, single_line_exclusions=("typing",)
+ ).output
+ == expected_output
+ )