Skip to content

Commit

Permalink
GH#953 Add support for deduplicating headings
Browse files Browse the repository at this point in the history
- Fixes PyCQA#953
  • Loading branch information
DylanYoung committed Sep 27, 2019
1 parent cdaae07 commit 4a6dbab
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
15 changes: 10 additions & 5 deletions isort/isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def __init__(
if key.startswith("import_heading") and value
]

self.seen_headings = set()

self.line_separator = self.determine_line_separator(file_contents)

self.in_lines = file_contents.split(self.line_separator)
Expand Down Expand Up @@ -753,11 +755,14 @@ def by_module(line: str) -> str:
)
if section_title:
section_comment = "# {}".format(section_title)
if (
section_comment not in self.out_lines[0:1]
and section_comment not in self.in_lines[0:1]
):
section_output.insert(0, section_comment)
if section_title in self.seen_headings:
if section_comment in self.out_lines[0:1]:
self.out_lines = self.out_lines[1:]
else:
if section_comment not in self.out_lines[0:1]:
section_output.insert(0, section_comment)
if self.config['dedup_headings']:
self.seen_headings.add(section_title)

if pending_lines_before or not no_lines_before:
output += [""] * self.config["lines_between_sections"]
Expand Down
8 changes: 7 additions & 1 deletion isort/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ def parse_args(argv: Optional[Sequence[str]] = None) -> Dict[str, Any]:
action="store_true",
help="Only order imports alphabetically, do not attempt type ordering",
)
parser.add_argument(
"--dont-dedup-headings",
action="store_false",
dest="dedup_headings",
help="Tells isort to repeat headings even if they have already been output.",
)
parser.add_argument(
"-e",
"--balanced",
Expand Down Expand Up @@ -606,7 +612,7 @@ def parse_args(argv: Optional[Sequence[str]] = None) -> Dict[str, Any]:
)

arguments = {
key: value for key, value in vars(parser.parse_args(argv)).items() if value
key: value for key, value in vars(parser.parse_args(argv)).items() if value is not None
}
if "dont_order_by_type" in arguments:
arguments["order_by_type"] = False
Expand Down
1 change: 1 addition & 0 deletions isort/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ def _get_default(py_version: Optional[str]) -> Dict[str, Any]:
"ignore_comments": False,
"safety_excludes": True,
"case_sensitive": False,
"dedup_headings": True,
}


Expand Down

0 comments on commit 4a6dbab

Please sign in to comment.