From b5018c79b833ad114fdb202167d65cc012d09a5b Mon Sep 17 00:00:00 2001 From: mshafer-NI Date: Thu, 8 Sep 2022 16:55:30 -0500 Subject: [PATCH 1/5] setup and use settings file --- ni_python_styleguide/_config_constants.py | 1 + ni_python_styleguide/_fix.py | 20 ++++++------------- ni_python_styleguide/config.toml | 5 +++++ .../basic_example/output.py | 4 ++-- 4 files changed, 14 insertions(+), 16 deletions(-) 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..cdd8c3bc 100644 --- a/ni_python_styleguide/_fix.py +++ b/ni_python_styleguide/_fix.py @@ -7,6 +7,7 @@ import isort +from ni_python_styleguide import _config_constants from ni_python_styleguide import _acknowledge_existing_errors from ni_python_styleguide import _format from ni_python_styleguide import _utils @@ -67,11 +68,13 @@ 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) @@ -92,17 +95,6 @@ def _handle_multiple_import_lines(bad_file: pathlib.Path): 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..f227b1e2 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 \ No newline at end of file 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 From 8f0be362bb1aa3f387fd82c52363bb571fe9116e Mon Sep 17 00:00:00 2001 From: mshafer-NI Date: Thu, 8 Sep 2022 16:56:02 -0500 Subject: [PATCH 2/5] dog-food the fix --- ni_python_styleguide/_fix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ni_python_styleguide/_fix.py b/ni_python_styleguide/_fix.py index cdd8c3bc..0cae5040 100644 --- a/ni_python_styleguide/_fix.py +++ b/ni_python_styleguide/_fix.py @@ -7,8 +7,8 @@ import isort -from ni_python_styleguide import _config_constants 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 From f796a1b3ca73713ca691ea4bcd97bcc6516c9462 Mon Sep 17 00:00:00 2001 From: mshafer-NI Date: Thu, 8 Sep 2022 16:56:55 -0500 Subject: [PATCH 3/5] remove dead method --- ni_python_styleguide/_fix.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/ni_python_styleguide/_fix.py b/ni_python_styleguide/_fix.py index 0cae5040..7f3f84f0 100644 --- a/ni_python_styleguide/_fix.py +++ b/ni_python_styleguide/_fix.py @@ -79,20 +79,6 @@ def _sort_imports(file: pathlib.Path, app_import_names): 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) _format.format(file) From b94ee06e01c3cb05b673bd2753ffb87a7fce1cb3 Mon Sep 17 00:00:00 2001 From: mshafer-NI Date: Thu, 8 Sep 2022 17:04:26 -0500 Subject: [PATCH 4/5] newlines are nice --- ni_python_styleguide/config.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ni_python_styleguide/config.toml b/ni_python_styleguide/config.toml index f227b1e2..2df362e5 100644 --- a/ni_python_styleguide/config.toml +++ b/ni_python_styleguide/config.toml @@ -8,4 +8,4 @@ line-length = 100 [tool.isort] profile = "black" combine_as_imports = true -force_single_line = true \ No newline at end of file +force_single_line = true From 98c5c732529de11fcf4bfd1a23f93ce5a75de85a Mon Sep 17 00:00:00 2001 From: mshafer-NI Date: Thu, 8 Sep 2022 17:06:54 -0500 Subject: [PATCH 5/5] remove no longer needed import --- ni_python_styleguide/_fix.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ni_python_styleguide/_fix.py b/ni_python_styleguide/_fix.py index 7f3f84f0..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