diff --git a/ni_python_styleguide/_config_constants.py b/ni_python_styleguide/_config_constants.py index 289052f9..9ba30695 100644 --- a/ni_python_styleguide/_config_constants.py +++ b/ni_python_styleguide/_config_constants.py @@ -4,3 +4,4 @@ FLAKE8_CONFIG_FILE = __FILE_DIR / "config.ini" BLACK_CONFIG_FILE = __FILE_DIR / "config.toml" +ISORT_CONFIG_FILE = BLACK_CONFIG_FILE diff --git a/ni_python_styleguide/_fix.py b/ni_python_styleguide/_fix.py index 478ecb9e..07957f85 100644 --- a/ni_python_styleguide/_fix.py +++ b/ni_python_styleguide/_fix.py @@ -1,4 +1,3 @@ -import fileinput import logging import pathlib from collections import defaultdict @@ -8,6 +7,7 @@ import isort from ni_python_styleguide import _acknowledge_existing_errors +from ni_python_styleguide import _config_constants from ni_python_styleguide import _format from ni_python_styleguide import _utils @@ -67,42 +67,19 @@ def _split_imports_line(lines: str, *_, **__): def _sort_imports(file: pathlib.Path, app_import_names): raw = file.read_text() + isort_config = isort.Config( + settings_file=str(_config_constants.ISORT_CONFIG_FILE), + known_first_party=filter(None, app_import_names.split(",")), + ) output = isort.code( raw, - multi_line_output=3, - line_length=1, - known_first_party=filter(None, app_import_names.split(",")), + config=isort_config, ) file.write_text(output) -def _handle_multiple_import_lines(bad_file: pathlib.Path): - multiline_string_checker = _utils.string_helpers.InMultiLineStringChecker( - lines=bad_file.read_text(encoding=_utils.DEFAULT_ENCODING).splitlines() - ) - with fileinput.FileInput(files=[str(bad_file)], inplace=True) as f: - for line_no, line in enumerate(f): - working_line = line - if multiline_string_checker.in_multiline_string(line_no + 1): - print(working_line, end="") - continue - working_line = _split_imports_line(working_line) - print(working_line, end="") - - def _format_imports(file: pathlib.Path, app_import_names: Iterable[str]) -> None: _sort_imports(file, app_import_names=app_import_names) - start_line, end_line = _utils.code_analysis.find_import_region(file) - file_lines = file.read_text().splitlines() - before = file_lines[:start_line] - import_lines = file_lines[start_line:end_line] - after = file_lines[end_line:] - with _utils.temp_file.multi_access_tempfile(suffix=".py") as temp_py_file: - temp_py_file.write_text("\n".join(import_lines)) - _format.format(temp_py_file, "--line-length=300") # condense any split lines - _handle_multiple_import_lines(temp_py_file) - handled_import_region = temp_py_file.read_text().splitlines() - file.write_text("\n".join(before + handled_import_region + after)) _format.format(file) diff --git a/ni_python_styleguide/config.toml b/ni_python_styleguide/config.toml index ff7e9c2b..2df362e5 100644 --- a/ni_python_styleguide/config.toml +++ b/ni_python_styleguide/config.toml @@ -4,3 +4,8 @@ [tool.black] line-length = 100 + +[tool.isort] +profile = "black" +combine_as_imports = true +force_single_line = true diff --git a/tests/test_cli/fix_test_cases__snapshots/basic_example/output.py b/tests/test_cli/fix_test_cases__snapshots/basic_example/output.py index 139568e0..3fe1aa45 100644 --- a/tests/test_cli/fix_test_cases__snapshots/basic_example/output.py +++ b/tests/test_cli/fix_test_cases__snapshots/basic_example/output.py @@ -2,9 +2,9 @@ import pathlib from os import access from os import path -from typing import ( +from typing import ( # noqa F401: un-used import comment that is actually used, should get removed in --aggressive (auto-generated noqa) Hashable, -) # noqa F401: un-used import comment that is actually used, should get removed in --aggressive (auto-generated noqa) +) from typing import Iterable from typing import List