Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
change_count,
original_line_count,
line_separator,
sections,
section_comments,
) = parse.file_contents(TEST_CONTENTS, config=Config())
assert "\n".join(in_lines) == TEST_CONTENTS
assert "import" not in "\n".join(out_lines)
assert import_index == 1
assert change_count == -2
assert original_line_count == len(in_lines)
auto_pytest_magic(parse.import_type)
auto_pytest_magic(parse.skip_line)
auto_pytest_magic(parse._strip_syntax)
auto_pytest_magic(parse._infer_line_separator)
imports,
categorized_comments,
change_count,
original_line_count,
line_separator,
sections,
section_comments,
) = parse.file_contents(TEST_CONTENTS, config=Config())
assert "\n".join(in_lines) == TEST_CONTENTS
assert "import" not in "\n".join(out_lines)
assert import_index == 1
assert change_count == -2
assert original_line_count == len(in_lines)
auto_pytest_magic(parse.import_type)
auto_pytest_magic(parse.skip_line)
auto_pytest_magic(parse._strip_syntax)
auto_pytest_magic(parse._infer_line_separator)
categorized_comments,
change_count,
original_line_count,
line_separator,
sections,
section_comments,
) = parse.file_contents(TEST_CONTENTS, config=Config())
assert "\n".join(in_lines) == TEST_CONTENTS
assert "import" not in "\n".join(out_lines)
assert import_index == 1
assert change_count == -2
assert original_line_count == len(in_lines)
auto_pytest_magic(parse.import_type)
auto_pytest_magic(parse.skip_line)
auto_pytest_magic(parse._strip_syntax)
auto_pytest_magic(parse._infer_line_separator)
original_line_count,
line_separator,
sections,
section_comments,
) = parse.file_contents(TEST_CONTENTS, config=Config())
assert "\n".join(in_lines) == TEST_CONTENTS
assert "import" not in "\n".join(out_lines)
assert import_index == 1
assert change_count == -2
assert original_line_count == len(in_lines)
auto_pytest_magic(parse.import_type)
auto_pytest_magic(parse.skip_line)
auto_pytest_magic(parse._strip_syntax)
auto_pytest_magic(parse._infer_line_separator)
formatted_output[output_at:0] = output
imports_tail = output_at + len(output)
while [
character.strip() for character in formatted_output[imports_tail : imports_tail + 1]
] == [""]:
formatted_output.pop(imports_tail)
if len(formatted_output) > imports_tail:
next_construct = ""
_in_quote: str = ""
tail = formatted_output[imports_tail:]
for index, line in enumerate(tail):
in_quote = _in_quote
should_skip, _in_quote, *_ = parse.skip_line(
line,
in_quote=_in_quote,
in_top_comment=False,
index=len(formatted_output),
section_comments=parsed.section_comments,
first_comment_index_start=parsed.first_comment_index_start,
first_comment_index_end=parsed.first_comment_index_end,
)
if not should_skip and line.strip():
if (
line.strip().startswith("#")
and len(tail) > (index + 1)
and tail[index + 1].strip()
):
continue
next_construct = line
file_path: Optional[Path] = None,
disregard_skip: bool = False,
**config_kwargs,
) -> bool:
config = _config(config=config, **config_kwargs)
sorted_output = sorted_imports(
file_contents=file_contents,
extension=extension,
config=config,
file_path=file_path,
disregard_skip=disregard_skip,
**config_kwargs,
)
if config.ignore_whitespace:
line_separator = config.line_ending or parse._infer_line_separator(file_contents)
compare_in = remove_whitespace(file_contents, line_separator=line_separator).strip()
compare_out = remove_whitespace(sorted_output, line_separator=line_separator).strip()
else:
compare_in = file_contents.strip()
compare_out = sorted_output.strip()
if compare_out == compare_in:
if config.verbose:
print(f"SUCCESS: {file_path or ''} Everything Looks Good!")
return True
else:
print(f"ERROR: {file_path or ''} Imports are incorrectly sorted.")
if show_diff:
show_unified_diff(
file_input=file_contents, file_output=sorted_output, file_path=file_path
)