Skip to content

Commit 9ea344c

Browse files
Fixed #953: Added support for deduping headings.
1 parent 496e1c8 commit 9ea344c

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ Changelog
33

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

6-
### 5.3.0 TBD
6+
### 5.3.0 Aug 4, 2020
77
- Implemented ability to treat all or select comments as code (issue #1357)
88
- Implemented ability to use different configs for different file extensions (issue #1162)
99
- Implemented ability to specify the types of imports (issue #1181)
10+
- Implemented ability to dedup import headings (issue #953)
1011
- Added experimental support for sorting literals (issue #1358)
1112
- Added experimental support for sorting and deduping groupings of assignments.
1213
- Improved handling of deprecated single line variables for usage with Visual Studio Code (issue #1363)

isort/main.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,13 @@ def _build_arg_parser() -> argparse.ArgumentParser:
673673
action="append",
674674
help="Specifies what extensions isort can never be ran against.",
675675
)
676+
parser.add_argument(
677+
"--dedup-headings",
678+
dest="dedup_headings",
679+
action="store_true",
680+
help="Tells isort to only show an identical custom import heading comment once, even if"
681+
" there are multiple sections with the comment set.",
682+
)
676683

677684
# deprecated options
678685
parser.add_argument(

isort/output.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import copy
22
import itertools
33
from functools import partial
4-
from typing import Iterable, List, Tuple
4+
from typing import Iterable, List, Set, Tuple
55

66
from isort.format import format_simplified
77

@@ -46,6 +46,7 @@ def sorted_imports(
4646
sections = base_sections + ("no_sections",)
4747

4848
output: List[str] = []
49+
seen_headings: Set[str] = set()
4950
pending_lines_before = False
5051
for section in sections:
5152
straight_modules = parsed.imports[section]["straight"]
@@ -152,7 +153,9 @@ def sorted_imports(
152153
continue
153154

154155
section_title = config.import_headings.get(section_name.lower(), "")
155-
if section_title:
156+
if section_title and section_title not in seen_headings:
157+
if config.dedup_headings:
158+
seen_headings.add(section_title)
156159
section_comment = f"# {section_title}"
157160
if section_comment not in parsed.lines_without_imports[0:1]:
158161
section_output.insert(0, section_comment)

isort/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ class _Config:
186186
constants: FrozenSet[str] = frozenset()
187187
classes: FrozenSet[str] = frozenset()
188188
variables: FrozenSet[str] = frozenset()
189+
dedup_headings: bool = False
189190

190191
def __post_init__(self):
191192
py_version = self.py_version

tests/test_ticketed_features.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""A growing set of tests designed to ensure when isort implements a feature described in a ticket
22
it fully works as defined in the associated ticket.
33
"""
4+
from functools import partial
45
from io import StringIO
56

67
import pytest
@@ -507,3 +508,38 @@ def test_isort_allows_setting_import_types_issue_1181():
507508
)
508509
== "from x import Big, variable, AA\n"
509510
)
511+
512+
513+
def test_isort_enables_deduping_section_headers_issue_953():
514+
"""isort should provide a way to only have identical import headings show up once.
515+
See: https://github.com/timothycrosley/isort/issues/953
516+
"""
517+
isort_code = partial(
518+
isort.code,
519+
config=Config(
520+
import_heading_firstparty="Local imports.",
521+
import_heading_localfolder="Local imports.",
522+
dedup_headings=True,
523+
known_first_party=["isort"],
524+
),
525+
)
526+
527+
assert (
528+
isort_code("from . import something")
529+
== """# Local imports.
530+
from . import something
531+
"""
532+
)
533+
assert (
534+
isort_code(
535+
"""from isort import y
536+
537+
from . import something"""
538+
)
539+
== """# Local imports.
540+
from isort import y
541+
542+
from . import something
543+
"""
544+
)
545+
assert isort_code("import os") == "import os\n"

0 commit comments

Comments
 (0)