Skip to content

Commit 478eebd

Browse files
authored
Use black profile with isort to reduce manual work (#87)
* setup and use settings file * dog-food the fix * remove dead method * newlines are nice * remove no longer needed import
1 parent eebf9b4 commit 478eebd

File tree

4 files changed

+14
-31
lines changed

4 files changed

+14
-31
lines changed

ni_python_styleguide/_config_constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
FLAKE8_CONFIG_FILE = __FILE_DIR / "config.ini"
66
BLACK_CONFIG_FILE = __FILE_DIR / "config.toml"
7+
ISORT_CONFIG_FILE = BLACK_CONFIG_FILE

ni_python_styleguide/_fix.py

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import fileinput
21
import logging
32
import pathlib
43
from collections import defaultdict
@@ -8,6 +7,7 @@
87
import isort
98

109
from ni_python_styleguide import _acknowledge_existing_errors
10+
from ni_python_styleguide import _config_constants
1111
from ni_python_styleguide import _format
1212
from ni_python_styleguide import _utils
1313

@@ -67,42 +67,19 @@ def _split_imports_line(lines: str, *_, **__):
6767

6868
def _sort_imports(file: pathlib.Path, app_import_names):
6969
raw = file.read_text()
70+
isort_config = isort.Config(
71+
settings_file=str(_config_constants.ISORT_CONFIG_FILE),
72+
known_first_party=filter(None, app_import_names.split(",")),
73+
)
7074
output = isort.code(
7175
raw,
72-
multi_line_output=3,
73-
line_length=1,
74-
known_first_party=filter(None, app_import_names.split(",")),
76+
config=isort_config,
7577
)
7678
file.write_text(output)
7779

7880

79-
def _handle_multiple_import_lines(bad_file: pathlib.Path):
80-
multiline_string_checker = _utils.string_helpers.InMultiLineStringChecker(
81-
lines=bad_file.read_text(encoding=_utils.DEFAULT_ENCODING).splitlines()
82-
)
83-
with fileinput.FileInput(files=[str(bad_file)], inplace=True) as f:
84-
for line_no, line in enumerate(f):
85-
working_line = line
86-
if multiline_string_checker.in_multiline_string(line_no + 1):
87-
print(working_line, end="")
88-
continue
89-
working_line = _split_imports_line(working_line)
90-
print(working_line, end="")
91-
92-
9381
def _format_imports(file: pathlib.Path, app_import_names: Iterable[str]) -> None:
9482
_sort_imports(file, app_import_names=app_import_names)
95-
start_line, end_line = _utils.code_analysis.find_import_region(file)
96-
file_lines = file.read_text().splitlines()
97-
before = file_lines[:start_line]
98-
import_lines = file_lines[start_line:end_line]
99-
after = file_lines[end_line:]
100-
with _utils.temp_file.multi_access_tempfile(suffix=".py") as temp_py_file:
101-
temp_py_file.write_text("\n".join(import_lines))
102-
_format.format(temp_py_file, "--line-length=300") # condense any split lines
103-
_handle_multiple_import_lines(temp_py_file)
104-
handled_import_region = temp_py_file.read_text().splitlines()
105-
file.write_text("\n".join(before + handled_import_region + after))
10683
_format.format(file)
10784

10885

ni_python_styleguide/config.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@
44

55
[tool.black]
66
line-length = 100
7+
8+
[tool.isort]
9+
profile = "black"
10+
combine_as_imports = true
11+
force_single_line = true

tests/test_cli/fix_test_cases__snapshots/basic_example/output.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import pathlib
33
from os import access
44
from os import path
5-
from typing import (
5+
from typing import ( # noqa F401: un-used import comment that is actually used, should get removed in --aggressive (auto-generated noqa)
66
Hashable,
7-
) # noqa F401: un-used import comment that is actually used, should get removed in --aggressive (auto-generated noqa)
7+
)
88
from typing import Iterable
99
from typing import List
1010

0 commit comments

Comments
 (0)