Skip to content

Commit

Permalink
Fixed #953: Added support for deduping headings.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Aug 5, 2020
1 parent 496e1c8 commit 9ea344c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ Changelog

NOTE: isort follows the [semver](https://semver.org/) versioning standard.

### 5.3.0 TBD
### 5.3.0 Aug 4, 2020
- Implemented ability to treat all or select comments as code (issue #1357)
- Implemented ability to use different configs for different file extensions (issue #1162)
- Implemented ability to specify the types of imports (issue #1181)
- Implemented ability to dedup import headings (issue #953)
- Added experimental support for sorting literals (issue #1358)
- Added experimental support for sorting and deduping groupings of assignments.
- Improved handling of deprecated single line variables for usage with Visual Studio Code (issue #1363)
Expand Down
7 changes: 7 additions & 0 deletions isort/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,13 @@ def _build_arg_parser() -> argparse.ArgumentParser:
action="append",
help="Specifies what extensions isort can never be ran against.",
)
parser.add_argument(
"--dedup-headings",
dest="dedup_headings",
action="store_true",
help="Tells isort to only show an identical custom import heading comment once, even if"
" there are multiple sections with the comment set.",
)

# deprecated options
parser.add_argument(
Expand Down
7 changes: 5 additions & 2 deletions isort/output.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import copy
import itertools
from functools import partial
from typing import Iterable, List, Tuple
from typing import Iterable, List, Set, Tuple

from isort.format import format_simplified

Expand Down Expand Up @@ -46,6 +46,7 @@ def sorted_imports(
sections = base_sections + ("no_sections",)

output: List[str] = []
seen_headings: Set[str] = set()
pending_lines_before = False
for section in sections:
straight_modules = parsed.imports[section]["straight"]
Expand Down Expand Up @@ -152,7 +153,9 @@ def sorted_imports(
continue

section_title = config.import_headings.get(section_name.lower(), "")
if section_title:
if section_title and section_title not in seen_headings:
if config.dedup_headings:
seen_headings.add(section_title)
section_comment = f"# {section_title}"
if section_comment not in parsed.lines_without_imports[0:1]:
section_output.insert(0, section_comment)
Expand Down
1 change: 1 addition & 0 deletions isort/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ class _Config:
constants: FrozenSet[str] = frozenset()
classes: FrozenSet[str] = frozenset()
variables: FrozenSet[str] = frozenset()
dedup_headings: bool = False

def __post_init__(self):
py_version = self.py_version
Expand Down
36 changes: 36 additions & 0 deletions tests/test_ticketed_features.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""A growing set of tests designed to ensure when isort implements a feature described in a ticket
it fully works as defined in the associated ticket.
"""
from functools import partial
from io import StringIO

import pytest
Expand Down Expand Up @@ -507,3 +508,38 @@ def test_isort_allows_setting_import_types_issue_1181():
)
== "from x import Big, variable, AA\n"
)


def test_isort_enables_deduping_section_headers_issue_953():
"""isort should provide a way to only have identical import headings show up once.
See: https://github.com/timothycrosley/isort/issues/953
"""
isort_code = partial(
isort.code,
config=Config(
import_heading_firstparty="Local imports.",
import_heading_localfolder="Local imports.",
dedup_headings=True,
known_first_party=["isort"],
),
)

assert (
isort_code("from . import something")
== """# Local imports.
from . import something
"""
)
assert (
isort_code(
"""from isort import y
from . import something"""
)
== """# Local imports.
from isort import y
from . import something
"""
)
assert isort_code("import os") == "import os\n"

0 comments on commit 9ea344c

Please sign in to comment.