Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
section_output.extend([""] * config.lines_between_types)
section_output = _with_from_imports(
parsed,
config,
from_modules,
section,
section_output,
sort_ignore_case,
remove_imports,
)
if config.force_sort_within_sections:
# Remove comments
section_output = [line for line in section_output if not line.startswith("#")]
section_output = sorting.naturally(
section_output,
key=partial(
sorting.section_key,
order_by_type=config.order_by_type,
force_to_top=config.force_to_top,
lexicographical=config.lexicographical,
),
)
# Add comments back
all_comments = copied_comments["above"]["from"]
all_comments.update(copied_comments["above"]["straight"])
comment_indexes = {}
for module, comment_list in all_comments.items():
for idx, line in enumerate(section_output):
if module in line:
for section in sections:
parsed.imports["no_sections"]["straight"].extend(
parsed.imports[section].get("straight", [])
)
parsed.imports["no_sections"]["from"].update(parsed.imports[section].get("from", {}))
sections = ("no_sections",)
output: List[str] = []
pending_lines_before = False
for section in sections:
straight_modules = parsed.imports[section]["straight"]
straight_modules = sorting.naturally(
straight_modules, key=lambda key: sorting.module_key(key, config, section_name=section)
)
from_modules = parsed.imports[section]["from"]
from_modules = sorting.naturally(
from_modules, key=lambda key: sorting.module_key(key, config, section_name=section)
)
if config.force_sort_within_sections:
copied_comments = copy.deepcopy(parsed.categorized_comments)
section_output: List[str] = []
if config.from_first:
section_output = _with_from_imports(
parsed,
config,
from_modules,
section,
section_output,
sort_ignore_case,
remove_imports,
sections: Iterable[str] = itertools.chain(parsed.sections, config.forced_separate)
if config.no_sections:
parsed.imports["no_sections"] = {"straight": [], "from": {}}
for section in sections:
parsed.imports["no_sections"]["straight"].extend(
parsed.imports[section].get("straight", [])
)
parsed.imports["no_sections"]["from"].update(parsed.imports[section].get("from", {}))
sections = ("no_sections",)
output: List[str] = []
pending_lines_before = False
for section in sections:
straight_modules = parsed.imports[section]["straight"]
straight_modules = sorting.naturally(
straight_modules, key=lambda key: sorting.module_key(key, config, section_name=section)
)
from_modules = parsed.imports[section]["from"]
from_modules = sorting.naturally(
from_modules, key=lambda key: sorting.module_key(key, config, section_name=section)
)
if config.force_sort_within_sections:
copied_comments = copy.deepcopy(parsed.categorized_comments)
section_output: List[str] = []
if config.from_first:
section_output = _with_from_imports(
parsed,
config,
from_modules,
and parsed.imports[section]["from"][module][from_import]
):
new_section_output.append(
wrap.line(single_import_line, parsed.line_separator, config)
)
from_comments = parsed.categorized_comments["straight"].get(
f"{module}.{from_import}"
)
new_section_output.extend(
with_comments(
from_comments,
wrap.line(import_start + as_import, parsed.line_separator, config),
removed=config.ignore_comments,
comment_prefix=config.comment_prefix,
)
for as_import in sorting.naturally(as_imports[from_import])
)
else:
new_section_output.append(
wrap.line(single_import_line, parsed.line_separator, config)
)
comments = None
else:
while from_imports and from_imports[0] in as_imports:
from_import = from_imports.pop(0)
as_imports[from_import] = sorting.naturally(as_imports[from_import])
from_comments = parsed.categorized_comments["straight"].get(
f"{module}.{from_import}"
)
above_comments = parsed.categorized_comments["above"]["from"].pop(module, None)
if above_comments:
if new_section_output and config.ensure_newline_before_comments:
config: Config,
from_modules: Iterable[str],
section: str,
section_output: List[str],
ignore_case: bool,
remove_imports: List[str],
) -> List[str]:
new_section_output = section_output.copy()
for module in from_modules:
if module in remove_imports:
continue
import_start = f"from {module} import "
from_imports = list(parsed.imports[section]["from"][module])
if not config.no_inline_sort or config.force_single_line:
from_imports = sorting.naturally(
from_imports,
key=lambda key: sorting.module_key(
key, config, True, ignore_case, section_name=section
),
)
if remove_imports:
from_imports = [
line for line in from_imports if f"{module}.{line}" not in remove_imports
]
sub_modules = [f"{module}.{from_import}" for from_import in from_imports]
as_imports = {
from_import: [
f"{from_import} as {as_module}" for as_module in parsed.as_map[sub_module]
]
for from_import, sub_module in zip(from_imports, sub_modules)
from_imports = [
line for line in from_imports if f"{module}.{line}" not in remove_imports
]
sub_modules = [f"{module}.{from_import}" for from_import in from_imports]
as_imports = {
from_import: [
f"{from_import} as {as_module}" for as_module in parsed.as_map[sub_module]
]
for from_import, sub_module in zip(from_imports, sub_modules)
if sub_module in parsed.as_map
}
if config.combine_as_imports and not ("*" in from_imports and config.combine_star):
if not config.no_inline_sort:
for as_import in as_imports:
as_imports[as_import] = sorting.naturally(as_imports[as_import])
for from_import in copy.copy(from_imports):
if from_import in as_imports:
idx = from_imports.index(from_import)
if (
config.keep_direct_and_as_imports
and parsed.imports[section]["from"][module][from_import]
):
from_imports[(idx + 1) : (idx + 1)] = as_imports.pop(from_import)
else:
from_imports[idx : (idx + 1)] = as_imports.pop(from_import)
while from_imports:
comments = parsed.categorized_comments["from"].pop(module, ())
if "*" in from_imports and config.combine_star:
import_statement = wrap.line(
with_comments(
from_comments,
wrap.line(import_start + as_import, parsed.line_separator, config),
removed=config.ignore_comments,
comment_prefix=config.comment_prefix,
)
for as_import in sorting.naturally(as_imports[from_import])
)
else:
new_section_output.append(
wrap.line(single_import_line, parsed.line_separator, config)
)
comments = None
else:
while from_imports and from_imports[0] in as_imports:
from_import = from_imports.pop(0)
as_imports[from_import] = sorting.naturally(as_imports[from_import])
from_comments = parsed.categorized_comments["straight"].get(
f"{module}.{from_import}"
)
above_comments = parsed.categorized_comments["above"]["from"].pop(module, None)
if above_comments:
if new_section_output and config.ensure_newline_before_comments:
new_section_output.append("")
new_section_output.extend(above_comments)
if (
config.keep_direct_and_as_imports
and parsed.imports[section]["from"][module][from_import]
):
new_section_output.append(
with_comments(
from_comments,