File tree Expand file tree Collapse file tree 5 files changed +51
-3
lines changed Expand file tree Collapse file tree 5 files changed +51
-3
lines changed Original file line number Diff line number Diff line change @@ -3,10 +3,11 @@ Changelog
3
3
4
4
NOTE: isort follows the [ semver] ( https://semver.org/ ) versioning standard.
5
5
6
- ### 5.3.0 TBD
6
+ ### 5.3.0 Aug 4, 2020
7
7
- Implemented ability to treat all or select comments as code (issue #1357 )
8
8
- Implemented ability to use different configs for different file extensions (issue #1162 )
9
9
- Implemented ability to specify the types of imports (issue #1181 )
10
+ - Implemented ability to dedup import headings (issue #953 )
10
11
- Added experimental support for sorting literals (issue #1358 )
11
12
- Added experimental support for sorting and deduping groupings of assignments.
12
13
- Improved handling of deprecated single line variables for usage with Visual Studio Code (issue #1363 )
Original file line number Diff line number Diff line change @@ -673,6 +673,13 @@ def _build_arg_parser() -> argparse.ArgumentParser:
673
673
action = "append" ,
674
674
help = "Specifies what extensions isort can never be ran against." ,
675
675
)
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
+ )
676
683
677
684
# deprecated options
678
685
parser .add_argument (
Original file line number Diff line number Diff line change 1
1
import copy
2
2
import itertools
3
3
from functools import partial
4
- from typing import Iterable , List , Tuple
4
+ from typing import Iterable , List , Set , Tuple
5
5
6
6
from isort .format import format_simplified
7
7
@@ -46,6 +46,7 @@ def sorted_imports(
46
46
sections = base_sections + ("no_sections" ,)
47
47
48
48
output : List [str ] = []
49
+ seen_headings : Set [str ] = set ()
49
50
pending_lines_before = False
50
51
for section in sections :
51
52
straight_modules = parsed .imports [section ]["straight" ]
@@ -152,7 +153,9 @@ def sorted_imports(
152
153
continue
153
154
154
155
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 )
156
159
section_comment = f"# { section_title } "
157
160
if section_comment not in parsed .lines_without_imports [0 :1 ]:
158
161
section_output .insert (0 , section_comment )
Original file line number Diff line number Diff line change @@ -186,6 +186,7 @@ class _Config:
186
186
constants : FrozenSet [str ] = frozenset ()
187
187
classes : FrozenSet [str ] = frozenset ()
188
188
variables : FrozenSet [str ] = frozenset ()
189
+ dedup_headings : bool = False
189
190
190
191
def __post_init__ (self ):
191
192
py_version = self .py_version
Original file line number Diff line number Diff line change 1
1
"""A growing set of tests designed to ensure when isort implements a feature described in a ticket
2
2
it fully works as defined in the associated ticket.
3
3
"""
4
+ from functools import partial
4
5
from io import StringIO
5
6
6
7
import pytest
@@ -507,3 +508,38 @@ def test_isort_allows_setting_import_types_issue_1181():
507
508
)
508
509
== "from x import Big, variable, AA\n "
509
510
)
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 "
You can’t perform that action at this time.
0 commit comments