Skip to content

Commit

Permalink
Merge pull request #1085 from timothycrosley/feature/fix-issue-969
Browse files Browse the repository at this point in the history
Fix issue #969: Add support for single line exclusions
  • Loading branch information
timothycrosley authored Jan 7, 2020
2 parents 3c2d797 + ad49f79 commit 9b654e3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
7 changes: 7 additions & 0 deletions isort/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,13 @@ def parse_args(argv: Optional[Sequence[str]] = None) -> Dict[str, Any]:
action="store_true",
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",
Expand Down
6 changes: 4 additions & 2 deletions isort/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion isort/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
"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,
Expand Down
1 change: 1 addition & 0 deletions isort/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions tests/test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -4702,3 +4702,23 @@ def test_noqa_issue_1065() -> None:
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
)

0 comments on commit 9b654e3

Please sign in to comment.