Skip to content

Commit 23c232e

Browse files
chore: fix code formmating on the codebase
- Close #2447
1 parent 95f39d7 commit 23c232e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1321
-486
lines changed

isort/_vendored/tomli/_parser.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
import string
22
import warnings
33
from types import MappingProxyType
4-
from typing import IO, Any, Callable, Dict, FrozenSet, Iterable, NamedTuple, Optional, Tuple
4+
from typing import (
5+
IO,
6+
Any,
7+
Callable,
8+
Dict,
9+
FrozenSet,
10+
Iterable,
11+
NamedTuple,
12+
Optional,
13+
Tuple,
14+
)
515

616
from ._re import (
717
RE_DATETIME,
@@ -122,7 +132,9 @@ def loads(s: str, *, parse_float: ParseFloat = float) -> Dict[str, Any]: # noqa
122132
except IndexError:
123133
break
124134
if char != "\n":
125-
raise suffixed_err(src, pos, "Expected newline or end of document after a statement")
135+
raise suffixed_err(
136+
src, pos, "Expected newline or end of document after a statement"
137+
)
126138
pos += 1
127139

128140
return out.data.dict
@@ -266,7 +278,9 @@ def skip_comment(src: str, pos: Pos) -> Pos:
266278
except IndexError:
267279
char = None
268280
if char == "#":
269-
return skip_until(src, pos + 1, "\n", error_on=ILLEGAL_COMMENT_CHARS, error_on_eof=False)
281+
return skip_until(
282+
src, pos + 1, "\n", error_on=ILLEGAL_COMMENT_CHARS, error_on_eof=False
283+
)
270284
return pos
271285

272286

@@ -318,13 +332,17 @@ def create_list_rule(src: str, pos: Pos, out: Output) -> Tuple[Pos, Key]:
318332
return pos + 2, key
319333

320334

321-
def key_value_rule(src: str, pos: Pos, out: Output, header: Key, parse_float: ParseFloat) -> Pos:
335+
def key_value_rule(
336+
src: str, pos: Pos, out: Output, header: Key, parse_float: ParseFloat
337+
) -> Pos:
322338
pos, key, value = parse_key_value_pair(src, pos, parse_float)
323339
key_parent, key_stem = key[:-1], key[-1]
324340
abs_key_parent = header + key_parent
325341

326342
if out.flags.is_(abs_key_parent, Flags.FROZEN):
327-
raise suffixed_err(src, pos, f"Can not mutate immutable namespace {abs_key_parent}")
343+
raise suffixed_err(
344+
src, pos, f"Can not mutate immutable namespace {abs_key_parent}"
345+
)
328346
# Containers in the relative path can't be opened with the table syntax after this
329347
out.flags.set_for_relative_key(header, key, Flags.EXPLICIT_NEST)
330348
try:
@@ -340,7 +358,9 @@ def key_value_rule(src: str, pos: Pos, out: Output, header: Key, parse_float: Pa
340358
return pos
341359

342360

343-
def parse_key_value_pair(src: str, pos: Pos, parse_float: ParseFloat) -> Tuple[Pos, Key, Any]:
361+
def parse_key_value_pair(
362+
src: str, pos: Pos, parse_float: ParseFloat
363+
) -> Tuple[Pos, Key, Any]:
344364
pos, key = parse_key(src, pos)
345365
try:
346366
char: Optional[str] = src[pos]
@@ -498,7 +518,9 @@ def parse_hex_char(src: str, pos: Pos, hex_len: int) -> Tuple[Pos, str]:
498518
def parse_literal_str(src: str, pos: Pos) -> Tuple[Pos, str]:
499519
pos += 1 # Skip starting apostrophe
500520
start_pos = pos
501-
pos = skip_until(src, pos, "'", error_on=ILLEGAL_LITERAL_STR_CHARS, error_on_eof=True)
521+
pos = skip_until(
522+
src, pos, "'", error_on=ILLEGAL_LITERAL_STR_CHARS, error_on_eof=True
523+
)
502524
return pos + 1, src[start_pos:pos] # Skip ending apostrophe
503525

504526

@@ -565,7 +587,9 @@ def parse_basic_str(src: str, pos: Pos, *, multiline: bool) -> Tuple[Pos, str]:
565587
pos += 1
566588

567589

568-
def parse_value(src: str, pos: Pos, parse_float: ParseFloat) -> Tuple[Pos, Any]: # noqa: C901
590+
def parse_value(
591+
src: str, pos: Pos, parse_float: ParseFloat
592+
) -> Tuple[Pos, Any]: # noqa: C901
569593
try:
570594
char: Optional[str] = src[pos]
571595
except IndexError:

isort/_vendored/tomli/_re.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ def match_to_datetime(match: "re.Match") -> Union[datetime, date]:
6969
hour, minute, sec = int(hour_str), int(minute_str), int(sec_str)
7070
micros = int(micros_str.ljust(6, "0")) if micros_str else 0
7171
if offset_sign_str:
72-
tz: Optional[tzinfo] = cached_tz(offset_hour_str, offset_minute_str, offset_sign_str)
72+
tz: Optional[tzinfo] = cached_tz(
73+
offset_hour_str, offset_minute_str, offset_sign_str
74+
)
7375
elif zulu_time:
7476
tz = timezone.utc
7577
else: # local date-time

isort/api.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@
3434
FileSkipSetting,
3535
IntroducedSyntaxErrors,
3636
)
37-
from .format import ask_whether_to_apply_changes_to_file, create_terminal_printer, show_unified_diff
37+
from .format import (
38+
ask_whether_to_apply_changes_to_file,
39+
create_terminal_printer,
40+
show_unified_diff,
41+
)
3842
from .io import Empty, File
3943
from .place import module as place_module # noqa: F401
4044
from .place import module_with_reason as place_module_with_reason # noqa: F401
@@ -222,7 +226,13 @@ def sort_stream(
222226
if config.atomic:
223227
_internal_output.seek(0)
224228
try:
225-
compile(_internal_output.read(), content_source, "exec", flags=0, dont_inherit=True)
229+
compile(
230+
_internal_output.read(),
231+
content_source,
232+
"exec",
233+
flags=0,
234+
dont_inherit=True,
235+
)
226236
_internal_output.seek(0)
227237
except SyntaxError: # pragma: no cover
228238
if extension not in CYTHON_EXTENSIONS:
@@ -273,7 +283,9 @@ def check_stream(
273283
disregard_skip=disregard_skip,
274284
)
275285
printer = create_terminal_printer(
276-
color=config.color_output, error=config.format_error, success=config.format_success
286+
color=config.color_output,
287+
error=config.format_error,
288+
success=config.format_success,
277289
)
278290
if not changed:
279291
if config.verbose and not config.only_modified:
@@ -359,9 +371,13 @@ def _in_memory_output_stream_context() -> Iterator[TextIO]:
359371

360372

361373
@contextlib.contextmanager
362-
def _file_output_stream_context(filename: str | Path, source_file: File) -> Iterator[TextIO]:
374+
def _file_output_stream_context(
375+
filename: str | Path, source_file: File
376+
) -> Iterator[TextIO]:
363377
tmp_file = _tmp_file(source_file)
364-
with tmp_file.open("w+", encoding=source_file.encoding, newline="") as output_stream:
378+
with tmp_file.open(
379+
"w+", encoding=source_file.encoding, newline=""
380+
) as output_stream:
365381
shutil.copymode(filename, tmp_file)
366382
yield output_stream
367383

@@ -449,7 +465,9 @@ def sort_file(
449465
file_output=output_stream.read(),
450466
file_path=actual_file_path,
451467
output=(
452-
None if show_diff is True else cast(TextIO, show_diff)
468+
None
469+
if show_diff is True
470+
else cast(TextIO, show_diff)
453471
),
454472
color_output=config.color_output,
455473
)
@@ -497,7 +515,10 @@ def sort_file(
497515
source_file.stream.close()
498516

499517
except ExistingSyntaxErrors:
500-
warn(f"{actual_file_path} unable to sort due to existing syntax errors", stacklevel=2)
518+
warn(
519+
f"{actual_file_path} unable to sort due to existing syntax errors",
520+
stacklevel=2,
521+
)
501522
except IntroducedSyntaxErrors: # pragma: no cover
502523
warn(
503524
f"{actual_file_path} unable to sort as isort introduces new syntax errors",
@@ -568,7 +589,9 @@ def find_imports_in_stream(
568589
key = f"{identified_import.module}.{identified_import.attribute}"
569590
elif unique == ImportKey.MODULE:
570591
key = identified_import.module
571-
elif unique == ImportKey.PACKAGE: # pragma: no branch # type checking ensures this
592+
elif (
593+
unique == ImportKey.PACKAGE
594+
): # pragma: no branch # type checking ensures this
572595
key = identified_import.module.split(".")[0]
573596

574597
if key and key not in seen:

isort/core.py

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212
from .settings import FILE_SKIP_COMMENTS
1313

1414
CIMPORT_IDENTIFIERS = ("cimport ", "cimport*", "from.cimport")
15-
IMPORT_START_IDENTIFIERS = ("from ", "from.import", "import ", "import*", *CIMPORT_IDENTIFIERS)
15+
IMPORT_START_IDENTIFIERS = (
16+
"from ",
17+
"from.import",
18+
"import ",
19+
"import*",
20+
*CIMPORT_IDENTIFIERS,
21+
)
1622
DOCSTRING_INDICATORS = ('"""', "'''")
1723
COMMENT_INDICATORS = (*DOCSTRING_INDICATORS, "'", '"', "#")
1824
CODE_SORT_COMMENTS = (
@@ -53,7 +59,9 @@ def process(
5359
was provided in the input_stream, otherwise `False`.
5460
"""
5561
line_separator: str = config.line_ending
56-
add_imports: list[str] = [format_natural(addition) for addition in config.add_imports]
62+
add_imports: list[str] = [
63+
format_natural(addition) for addition in config.add_imports
64+
]
5765
import_section: str = ""
5866
next_import_section: str = ""
5967
next_cimports: bool = False
@@ -88,15 +96,17 @@ def process(
8896
if line == "# isort: on\n":
8997
isort_off = False
9098
new_input += line
91-
elif line in ("# isort: split\n", "# isort: off\n", None) or str(line).endswith(
92-
"# isort: split\n"
93-
):
99+
elif line in ("# isort: split\n", "# isort: off\n", None) or str(
100+
line
101+
).endswith("# isort: split\n"):
94102
if line == "# isort: off\n":
95103
isort_off = True
96104
if current:
97105
if add_imports:
98106
add_line_separator = line_separator or "\n"
99-
current += add_line_separator + add_line_separator.join(add_imports)
107+
current += add_line_separator + add_line_separator.join(
108+
add_imports
109+
)
100110
add_imports = []
101111
parsed = parse.file_contents(current, config=config)
102112
verbose_output += parsed.verbose_output
@@ -160,7 +170,10 @@ def process(
160170
stripped_line = line.strip()
161171
if stripped_line and not line_separator:
162172
line_separator = (
163-
line[len(line.rstrip()) :].replace(" ", "").replace("\t", "").replace("\f", "")
173+
line[len(line.rstrip()) :]
174+
.replace(" ", "")
175+
.replace("\t", "")
176+
.replace("\f", "")
164177
)
165178

166179
for file_skip_comment in FILE_SKIP_COMMENTS:
@@ -176,9 +189,9 @@ def process(
176189
elif stripped_line.startswith("# isort: dont-add-imports"):
177190
add_imports = []
178191
elif stripped_line.startswith("# isort: dont-add-import:"):
179-
import_not_to_add = stripped_line.split("# isort: dont-add-import:", 1)[
180-
1
181-
].strip()
192+
import_not_to_add = stripped_line.split(
193+
"# isort: dont-add-import:", 1
194+
)[1].strip()
182195
add_imports = [
183196
import_to_add
184197
for import_to_add in add_imports
@@ -201,7 +214,9 @@ def process(
201214
first_comment_index_end = index - 1
202215

203216
was_in_quote = bool(in_quote)
204-
if ((not stripped_line.startswith("#") or in_quote) and '"' in line) or "'" in line:
217+
if (
218+
(not stripped_line.startswith("#") or in_quote) and '"' in line
219+
) or "'" in line:
205220
char_index = 0
206221
if first_comment_index_start == -1 and line.startswith(('"', "'")):
207222
first_comment_index_start = index
@@ -311,7 +326,9 @@ def process(
311326
line = input_stream.readline()
312327

313328
if not line: # end of file without closing parenthesis
314-
raise ExistingSyntaxErrors("Parenthesis is not closed")
329+
raise ExistingSyntaxErrors(
330+
"Parenthesis is not closed"
331+
)
315332

316333
stripped_line = line.strip().split("#")[0]
317334
import_statement += line
@@ -342,7 +359,9 @@ def process(
342359
if cimport_statement != cimports or (
343360
new_indent != indent
344361
and import_section
345-
and (not did_contain_imports or len(new_indent) < len(indent))
362+
and (
363+
not did_contain_imports or len(new_indent) < len(indent)
364+
)
346365
):
347366
indent = new_indent
348367
if import_section:
@@ -356,7 +375,9 @@ def process(
356375
else:
357376
if new_indent != indent:
358377
if import_section and did_contain_imports:
359-
import_statement = indent + import_statement.lstrip()
378+
import_statement = (
379+
indent + import_statement.lstrip()
380+
)
360381
else:
361382
indent = new_indent
362383
import_section += import_statement
@@ -381,10 +402,14 @@ def process(
381402
and not was_in_quote
382403
and not import_section
383404
and not line.lstrip().startswith(COMMENT_INDICATORS)
384-
and not (line.rstrip().endswith(DOCSTRING_INDICATORS) and "=" not in line)
405+
and not (
406+
line.rstrip().endswith(DOCSTRING_INDICATORS) and "=" not in line
407+
)
385408
):
386409
add_line_separator = line_separator or "\n"
387-
import_section = add_line_separator.join(add_imports) + add_line_separator
410+
import_section = (
411+
add_line_separator.join(add_imports) + add_line_separator
412+
)
388413
if end_of_file and index != 0:
389414
output_stream.write(add_line_separator)
390415
contains_imports = True
@@ -395,9 +420,15 @@ def process(
395420
next_import_section = ""
396421

397422
if import_section:
398-
if add_imports and (contains_imports or not config.append_only) and not indent:
423+
if (
424+
add_imports
425+
and (contains_imports or not config.append_only)
426+
and not indent
427+
):
399428
import_section = (
400-
line_separator.join(add_imports) + line_separator + import_section
429+
line_separator.join(add_imports)
430+
+ line_separator
431+
+ import_section
401432
)
402433
contains_imports = True
403434
add_imports = []
@@ -420,7 +451,8 @@ def process(
420451

421452
if indent:
422453
import_section = "".join(
423-
line[len(indent) :] for line in import_section.splitlines(keepends=True)
454+
line[len(indent) :]
455+
for line in import_section.splitlines(keepends=True)
424456
)
425457

426458
parsed_content = parse.file_contents(import_section, config=config)
@@ -466,7 +498,12 @@ def process(
466498
output_stream.write(line)
467499
not_imports = False
468500

469-
if stripped_line and not in_quote and not import_section and not next_import_section:
501+
if (
502+
stripped_line
503+
and not in_quote
504+
and not import_section
505+
and not next_import_section
506+
):
470507
if stripped_line == "yield":
471508
while not stripped_line or stripped_line == "yield":
472509
new_line = input_stream.readline()
@@ -501,12 +538,16 @@ def _indented_config(config: Config, indent: str) -> Config:
501538
line_length=max(config.line_length - len(indent), 0),
502539
wrap_length=max(config.wrap_length - len(indent), 0),
503540
lines_after_imports=1,
504-
import_headings=config.import_headings if config.indented_import_headings else {},
541+
import_headings=(
542+
config.import_headings if config.indented_import_headings else {}
543+
),
505544
import_footers=config.import_footers if config.indented_import_headings else {},
506545
)
507546

508547

509-
def _has_changed(before: str, after: str, line_separator: str, ignore_whitespace: bool) -> bool:
548+
def _has_changed(
549+
before: str, after: str, line_separator: str, ignore_whitespace: bool
550+
) -> bool:
510551
if ignore_whitespace:
511552
return (
512553
remove_whitespace(before, line_separator=line_separator).strip()

0 commit comments

Comments
 (0)