Skip to content

Commit

Permalink
Merge pull request #1629 from gofr/simplify-section_key-sort
Browse files Browse the repository at this point in the history
Simplify section_key helper function arguments
  • Loading branch information
timothycrosley committed Jan 3, 2021
2 parents 05d8542 + 2f9fe86 commit 98fad11
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 32 deletions.
12 changes: 1 addition & 11 deletions isort/output.py
Expand Up @@ -94,17 +94,7 @@ def sorted_imports(
# only_sections options is not imposed if force_sort_within_sections is True
new_section_output = sorting.naturally(
new_section_output,
key=partial(
sorting.section_key,
case_sensitive=config.case_sensitive,
honor_case_in_force_sorted_sections=config.honor_case_in_force_sorted_sections,
order_by_type=config.order_by_type,
force_to_top=config.force_to_top,
lexicographical=config.lexicographical,
length_sort=config.length_sort,
reverse_relative=config.reverse_relative,
group_by_package=config.group_by_package,
),
key=partial(sorting.section_key, config=config),
)

# uncollapse comments
Expand Down
32 changes: 11 additions & 21 deletions isort/sorting.py
Expand Up @@ -51,52 +51,42 @@ def module_key(
return f"{module_name in config.force_to_top and 'A' or 'B'}{prefix}{_length_sort_maybe}"


def section_key(
line: str,
case_sensitive: bool,
honor_case_in_force_sorted_sections: bool,
order_by_type: bool,
force_to_top: List[str],
lexicographical: bool = False,
length_sort: bool = False,
reverse_relative: bool = False,
group_by_package: bool = False,
) -> str:
def section_key(line: str, config: Config) -> str:
section = "B"

if reverse_relative and line.startswith("from ."):
if config.reverse_relative and line.startswith("from ."):
match = re.match(r"^from (\.+)\s*(.*)", line)
if match: # pragma: no cover - regex always matches if line starts with "from ."
line = f"from {' '.join(match.groups())}"
if group_by_package and line.strip().startswith("from"):
if config.group_by_package and line.strip().startswith("from"):
line = line.split(" import", 1)[0]

if lexicographical:
if config.lexicographical:
line = _import_line_intro_re.sub("", _import_line_midline_import_re.sub(".", line))
else:
line = re.sub("^from ", "", line)
line = re.sub("^import ", "", line)
if line.split(" ")[0] in force_to_top:
if line.split(" ")[0] in config.force_to_top:
section = "A"
# * If honor_case_in_force_sorted_sections is true, and case_sensitive and
# order_by_type are different, only ignore case in part of the line.
# * Otherwise, let order_by_type decide the sorting of the whole line. This
# is only "correct" if case_sensitive and order_by_type have the same value.
if honor_case_in_force_sorted_sections and case_sensitive != order_by_type:
if config.honor_case_in_force_sorted_sections and config.case_sensitive != config.order_by_type:
split_module = line.split(" import ", 1)
if len(split_module) > 1:
module_name, names = split_module
if not case_sensitive:
if not config.case_sensitive:
module_name = module_name.lower()
if not order_by_type:
if not config.order_by_type:
names = names.lower()
line = " import ".join([module_name, names])
elif not case_sensitive:
elif not config.case_sensitive:
line = line.lower()
elif not order_by_type:
elif not config.order_by_type:
line = line.lower()

return f"{section}{len(line) if length_sort else ''}{line}"
return f"{section}{len(line) if config.length_sort else ''}{line}"


def naturally(to_sort: Iterable[str], key: Optional[Callable[[str], Any]] = None) -> List[str]:
Expand Down

0 comments on commit 98fad11

Please sign in to comment.