-
-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy path_builtin_formatter.py
More file actions
1829 lines (1553 loc) · 70.3 KB
/
Copy path_builtin_formatter.py
File metadata and controls
1829 lines (1553 loc) · 70.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Dependency-free formatter for generated Python code."""
from __future__ import annotations
import ast
import re
import sys
import tokenize
from collections import defaultdict
from enum import IntEnum
from io import StringIO
from typing import TYPE_CHECKING, Any, TypeGuard, cast
from datamodel_code_generator.util import load_toml
if TYPE_CHECKING:
from collections.abc import Iterator, Mapping, Sequence
from pathlib import Path
from typing import Protocol
class PythonVersion(Protocol):
@property
def version_key(self) -> tuple[int, int]:
raise NotImplementedError
class _SourceLocationNode(Protocol):
lineno: int
end_lineno: int | None
col_offset: int
end_col_offset: int | None
class _AliasSortCategory(IntEnum):
CONSTANT = 0
CLASS = 1
OTHER = 2
class _ImportCategory(IntEnum):
FUTURE = 0
STANDARD_LIBRARY = 1
THIRD_PARTY = 2
FIRST_PARTY = 3
LOCAL = 4
DEFAULT_LINE_LENGTH = 88
DEFAULT_KNOWN_FIRST_PARTY = frozenset({"datamodel_code_generator", "tests"})
MAX_TOP_LEVEL_BLANK_LINES = 2
MAX_SHORT_DEFAULT_OVERFLOW = 13
LONG_TARGET_PREFIX_LENGTH = 30
TYPE_ALIAS_INLINE_ARGUMENT_COUNT = 2
STRING_PREFIX_PATTERN = re.compile(r"(?i)^([rubf]*)(\"\"\"|'''|\"|')")
PEP695_TYPE_ALIAS_START_PATTERN = re.compile(r"^(?P<indent>\s*)type\s+(?P<target>[A-Za-z_]\w*(?:\[.*?\])?)\s*=")
PEP695_TYPE_ALIAS_PLACEHOLDER = "__datamodel_codegen_builtin_type_alias__"
_SOURCE_LINES_CACHE: list[tuple[str, list[str]]] = []
def _is_valid_builtin_line_length(line_length: Any) -> TypeGuard[int]:
return isinstance(line_length, int) and not isinstance(line_length, bool) and line_length > 0
def _line_indent(line: str) -> str:
return line[: len(line) - len(line.lstrip())]
def _indent_first_line(lines: Sequence[str], indent: str) -> Iterator[str]:
return (f"{indent}{line}" if index == 0 else line for index, line in enumerate(lines))
def _find_pyproject_toml(settings_path: Path) -> Path | None:
for path in (settings_path, *settings_path.parents):
pyproject_toml = path / "pyproject.toml"
if pyproject_toml.is_file():
return pyproject_toml
return None
def _get_builtin_line_length(
settings_path: Path,
explicit_line_length: int | None = None,
tool_config: Mapping[str, Any] | None = None,
) -> int:
if explicit_line_length is not None:
if _is_valid_builtin_line_length(explicit_line_length):
return explicit_line_length
msg = "builtin_format_line_length must be a positive integer"
raise ValueError(msg)
if tool_config is None:
if (pyproject_toml_path := _find_pyproject_toml(settings_path)) is None:
return DEFAULT_LINE_LENGTH
tool_config = load_toml(pyproject_toml_path).get("tool", {})
datamodel_codegen_config = tool_config.get("datamodel-codegen", {})
ruff_config = tool_config.get("ruff", {})
black_config = tool_config.get("black", {})
isort_config = tool_config.get("isort", {})
for line_length in (
datamodel_codegen_config.get("builtin-format-line-length"),
datamodel_codegen_config.get("builtin_format_line_length"),
ruff_config.get("line-length"),
black_config.get("line-length"),
isort_config.get("line_length"),
):
if _is_valid_builtin_line_length(line_length):
return line_length
return DEFAULT_LINE_LENGTH
def _get_builtin_known_first_party(
settings_path: Path,
tool_config: Mapping[str, Any] | None = None,
) -> frozenset[str]:
if tool_config is None:
if (pyproject_toml_path := _find_pyproject_toml(settings_path)) is None:
return DEFAULT_KNOWN_FIRST_PARTY
tool_config = load_toml(pyproject_toml_path).get("tool", {})
isort_config = tool_config.get("isort", {})
known_first_party = isort_config.get("known_first_party", [])
if not isinstance(known_first_party, list):
return DEFAULT_KNOWN_FIRST_PARTY
return DEFAULT_KNOWN_FIRST_PARTY | frozenset(item for item in known_first_party if isinstance(item, str))
def _get_builtin_string_normalization(
settings_path: Path,
*,
skip_string_normalization: bool,
tool_config: Mapping[str, Any] | None = None,
) -> bool:
if not skip_string_normalization:
return True
if tool_config is None:
if (pyproject_toml_path := _find_pyproject_toml(settings_path)) is None:
return False
tool_config = load_toml(pyproject_toml_path).get("tool", {})
black_config = tool_config.get("black", {})
skip_black_string_normalization = black_config.get("skip-string-normalization")
if isinstance(skip_black_string_normalization, bool):
return not skip_black_string_normalization
return False
def _format_alias(alias: ast.alias) -> str:
if alias.asname:
return f"{alias.name} as {alias.asname}"
return alias.name
def _alias_imported_name(alias: str) -> str:
if " as " in alias:
return alias.split(" as ", 1)[0]
return alias
def _alias_sort_key(alias: str) -> tuple[_AliasSortCategory, str]:
name = _alias_imported_name(alias)
if name.isupper():
category = _AliasSortCategory.CONSTANT
elif name[:1].isupper():
category = _AliasSortCategory.CLASS
else:
category = _AliasSortCategory.OTHER
return category, name.lower()
def _format_from_import(module: str, aliases: list[str], line_length: int) -> str:
line = f"from {module} import {', '.join(aliases)}"
if len(line) <= line_length and "*" not in aliases:
return line
imports = "\n".join(f" {alias}," for alias in aliases)
return f"from {module} import (\n{imports}\n)"
def _import_category(module: str, level: int, known_first_party: frozenset[str]) -> _ImportCategory:
if module == "__future__":
return _ImportCategory.FUTURE
if level:
return _ImportCategory.LOCAL
top_level_module = module.split(".", 1)[0]
if top_level_module in sys.stdlib_module_names:
return _ImportCategory.STANDARD_LIBRARY
if top_level_module in known_first_party:
return _ImportCategory.FIRST_PARTY
return _ImportCategory.THIRD_PARTY
def _has_inline_comment(lines: list[str], node: ast.AST) -> bool:
lineno = getattr(node, "lineno", 1)
end_lineno = getattr(node, "end_lineno", None) or lineno
return any(_has_comment_token(line) for line in lines[lineno - 1 : end_lineno])
def _has_comment_token(line: str) -> bool:
if "#" not in line:
return False
try:
tokens = tokenize.generate_tokens(StringIO(line).readline)
return any(token.type == tokenize.COMMENT for token in tokens)
except tokenize.TokenError:
return "#" in line
def _needs_pep695_type_alias_placeholders(python_version: PythonVersion | None) -> bool:
return (
python_version is not None
and python_version.version_key >= (3, 12)
and (3, 10) <= sys.version_info[:2] < (3, 12)
)
def _pep695_type_alias_statement_end(lines: list[str], start_index: int) -> int:
bracket_depth = 0
try:
tokens = tokenize.generate_tokens(StringIO("\n".join(lines[start_index:])).readline)
for token in tokens:
if token.type == tokenize.OP:
if token.string in {"(", "[", "{"}:
bracket_depth += 1
elif token.string in {")", "]", "}"}:
bracket_depth -= 1
if token.type == tokenize.NEWLINE and bracket_depth <= 0:
return start_index + token.end[0] - 1
except tokenize.TokenError:
return start_index
return start_index
def _replace_pep695_type_aliases_with_placeholders(code: str) -> str:
lines = code.splitlines()
placeholder_lines = list(lines)
line_index = 0
while line_index < len(lines):
line = lines[line_index]
if not PEP695_TYPE_ALIAS_START_PATTERN.match(line):
line_index += 1
continue
indent = _line_indent(line)
end_index = _pep695_type_alias_statement_end(lines, line_index)
placeholder_lines[line_index] = f"{indent}{PEP695_TYPE_ALIAS_PLACEHOLDER} = None"
for continuation_index in range(line_index + 1, end_index + 1):
placeholder_lines[continuation_index] = ""
line_index = end_index + 1
return "\n".join(placeholder_lines)
def _parse_builtin_code(code: str, python_version: PythonVersion | None) -> ast.Module | None:
feature_version = python_version.version_key if python_version is not None else None
try:
return ast.parse(code, feature_version=feature_version)
except SyntaxError:
if not _needs_pep695_type_alias_placeholders(python_version):
return None
placeholder_code = _replace_pep695_type_aliases_with_placeholders(code)
if placeholder_code == code:
return None
try:
return ast.parse(placeholder_code, feature_version=feature_version)
except SyntaxError:
return None
def _format_import_node_without_reordering(
node: ast.Import | ast.ImportFrom,
lines: list[str],
known_first_party: frozenset[str] = DEFAULT_KNOWN_FIRST_PARTY,
) -> tuple[_ImportCategory, str]:
end_lineno = node.end_lineno or node.lineno
raw_import = "\n".join(lines[node.lineno - 1 : end_lineno])
return _import_node_category(node, known_first_party), raw_import
def _import_node_category(node: ast.Import | ast.ImportFrom, known_first_party: frozenset[str]) -> _ImportCategory:
match node:
case ast.Import():
return min(_import_category(alias.name, 0, known_first_party) for alias in node.names)
case ast.ImportFrom():
return _import_category(node.module or "", node.level, known_first_party)
msg = f"Unsupported import node: {type(node).__name__}"
raise TypeError(msg)
def _format_import_node(
node: ast.Import | ast.ImportFrom,
line_length: int,
known_first_party: frozenset[str] = DEFAULT_KNOWN_FIRST_PARTY,
) -> tuple[_ImportCategory, str]:
category = _import_node_category(node, known_first_party)
match node:
case ast.Import():
lines = [f"import {_format_alias(alias)}" for alias in sorted(node.names, key=lambda alias: alias.name)]
return category, "\n".join(lines)
case ast.ImportFrom():
module = "." * node.level + (node.module or "")
if any(alias.asname is not None for alias in node.names):
lines = [
_format_from_import(module, [_format_alias(alias)], line_length)
for alias in sorted(node.names, key=lambda alias: _alias_sort_key(_format_alias(alias)))
]
return category, "\n".join(lines)
aliases = sorted((_format_alias(alias) for alias in node.names), key=_alias_sort_key)
return category, _format_from_import(module, aliases, line_length)
raise AssertionError # pragma: no cover
def _from_import_key(node: ast.ImportFrom) -> tuple[int, str]:
return node.level, node.module or ""
def _modules_with_aliased_imports(import_nodes: list[ast.Import | ast.ImportFrom]) -> set[tuple[int, str]]:
return {
_from_import_key(node)
for node in import_nodes
if isinstance(node, ast.ImportFrom) and any(alias.asname is not None for alias in node.names)
}
def _can_merge_from_imports(node: ast.ImportFrom, aliased_modules: set[tuple[int, str]]) -> bool:
return node.level == 0 and _from_import_key(node) not in aliased_modules
def _iter_aliased_from_import_lines(
module: str,
aliases: list[tuple[int, str, bool]],
line_length: int,
) -> Iterator[str]:
sorted_aliases = sorted(aliases, key=lambda item: _alias_sort_key(item[1]))
chunk: list[str] = []
chunk_group: int | None = None
for group_index, alias, is_aliased in sorted_aliases:
if is_aliased:
if chunk:
yield _format_from_import(module, chunk, line_length)
chunk = []
chunk_group = None
yield _format_from_import(module, [alias], line_length)
continue
if chunk and chunk_group != group_index:
yield _format_from_import(module, chunk, line_length)
chunk = []
chunk.append(alias)
chunk_group = group_index
if chunk:
yield _format_from_import(module, chunk, line_length)
def _import_line_sort_key(line: str) -> tuple[int, int, str, int, str, str]:
if not line.startswith("from "):
return 0, 0, line.lower(), 0, "", line
module, _, imported = line.removeprefix("from ").partition(" import ")
relative_level = len(module) - len(module.lstrip("."))
alias_category, alias_name = _alias_sort_key(imported.split(",", 1)[0].strip())
if relative_level:
return 1, -relative_level, module[relative_level:].lower(), int(alias_category), alias_name, line.lower()
return 1, 0, module.lower(), int(alias_category), alias_name, line.lower()
def _build_builtin_import_block(
import_nodes: list[ast.Import | ast.ImportFrom],
line_length: int,
lines: list[str],
known_first_party: frozenset[str],
) -> str:
categorized_lines: defaultdict[_ImportCategory, set[str]] = defaultdict(set)
grouped_from_imports: defaultdict[tuple[_ImportCategory, int, str], set[str]] = defaultdict(set)
aliased_from_imports: defaultdict[tuple[_ImportCategory, int, str], list[tuple[int, str, bool]]] = defaultdict(list)
aliased_modules = _modules_with_aliased_imports(import_nodes)
for node_index, node in enumerate(import_nodes):
if _has_inline_comment(lines, node):
category, raw_import = _format_import_node_without_reordering(node, lines, known_first_party)
categorized_lines[category].add(raw_import)
continue
if isinstance(node, ast.ImportFrom) and _from_import_key(node) in aliased_modules:
module = "." * node.level + (node.module or "")
category = _import_category(node.module or "", node.level, known_first_party)
for alias in node.names:
aliased_from_imports[category, node.level, module].append((
node_index,
_format_alias(alias),
alias.asname is not None,
))
continue
if isinstance(node, ast.ImportFrom) and _can_merge_from_imports(node, aliased_modules):
module = node.module or ""
category = _import_category(module, node.level, known_first_party)
for alias in node.names:
grouped_from_imports[category, node.level, module].add(_format_alias(alias))
continue
category, import_line = _format_import_node(node, line_length, known_first_party)
categorized_lines[category].add(import_line)
for (category, _level, module), aliases in grouped_from_imports.items():
categorized_lines[category].add(_format_from_import(module, sorted(aliases, key=_alias_sort_key), line_length))
for (category, _level, module), aliases in aliased_from_imports.items():
for import_line in _iter_aliased_from_import_lines(module, aliases, line_length):
categorized_lines[category].add(import_line)
groups = [
"\n".join(sorted(categorized_lines[category], key=_import_line_sort_key))
for category in sorted(categorized_lines)
]
return "\n\n".join(group for group in groups if group)
def _is_name_or_attr(node: ast.AST, name: str) -> bool:
if isinstance(node, ast.Name):
return node.id == name
return isinstance(node, ast.Attribute) and node.attr == name
def _is_type_checking_if(node: ast.AST) -> TypeGuard[ast.If]:
return isinstance(node, ast.If) and _is_name_or_attr(node.test, "TYPE_CHECKING")
def _splitlines_no_ff(source: str) -> list[str]:
"""Split source lines like the Python parser, without form-feed splitting."""
index = 0
lines: list[str] = []
next_line = ""
while index < len(source):
character = source[index]
next_line += character
index += 1
if character == "\r" and index < len(source) and source[index] == "\n":
next_line += "\n"
index += 1
if character in "\r\n":
lines.append(next_line)
next_line = ""
if next_line:
lines.append(next_line)
return lines
def _source_lines(source: str) -> list[str]:
if _SOURCE_LINES_CACHE:
cached_source, cached_lines = _SOURCE_LINES_CACHE[0]
if source is cached_source or source == cached_source:
return cached_lines
lines = _splitlines_no_ff(source)
_SOURCE_LINES_CACHE[:] = [(source, lines)]
return lines
def _source_segment_from_cached_lines(source: str, node: ast.AST) -> str | None:
if not (
hasattr(node, "lineno")
and hasattr(node, "end_lineno")
and hasattr(node, "col_offset")
and hasattr(node, "end_col_offset")
):
return None
location_node = cast("_SourceLocationNode", node)
lineno = location_node.lineno
end_lineno = location_node.end_lineno
col_offset = location_node.col_offset
end_col_offset = location_node.end_col_offset
if end_lineno is None or end_col_offset is None:
return None
lineno -= 1
end_lineno -= 1
lines = _source_lines(source)
if end_lineno == lineno:
return lines[lineno].encode()[col_offset:end_col_offset].decode()
first = lines[lineno].encode()[col_offset:].decode()
last = lines[end_lineno].encode()[:end_col_offset].decode()
return "".join([first, *lines[lineno + 1 : end_lineno], last])
def _source_segment(source: str, node: ast.AST) -> str:
return _source_segment_from_cached_lines(source, node) or ast.unparse(node)
def _inline_source_segment(source: str, node: ast.AST) -> str:
if isinstance(node, ast.Lambda):
return re.sub(r"^lambda\s+:", "lambda:", ast.unparse(node))
return _source_segment(source, node)
def _format_call_argument(keyword: ast.keyword, source: str) -> str:
if keyword.arg is None:
return f"**{_source_segment(source, keyword.value)}"
return f"{keyword.arg}={_inline_source_segment(source, keyword.value)}"
def _format_dict_literal(
dict_node: ast.Dict,
indent: str,
source: str,
line_length: int = DEFAULT_LINE_LENGTH,
) -> str:
entries: list[str] = []
entry_indent = f"{indent} "
has_trailing_comma = len(dict_node.keys) > 1
for key, value in zip(dict_node.keys, dict_node.values, strict=True):
trailing_comma = "," if has_trailing_comma else ""
if key is None:
entries.append(f"{entry_indent}**{_source_segment(source, value)}{trailing_comma}")
continue
key_source = _source_segment(source, key)
value_source = _source_segment(source, value)
entry = f"{entry_indent}{key_source}: {value_source}{trailing_comma}"
if isinstance(value, ast.Dict) and len(entry) > line_length:
nested_lines = _format_dict_literal(value, entry_indent, source, line_length).splitlines()
entries.append(f"{entry_indent}{key_source}: {nested_lines[0]}")
entries.extend(nested_lines[1:-1])
entries.append(f"{nested_lines[-1]}{trailing_comma}")
else:
entries.append(entry)
return "{\n" + "\n".join(entries) + f"\n{indent}}}"
def _format_list_literal(list_node: ast.List, indent: str, source: str) -> str:
element_indent = f"{indent} "
has_trailing_comma = len(list_node.elts) > 1
elements = "\n".join(
f"{element_indent}{_source_segment(source, element)}{',' if has_trailing_comma else ''}"
for element in list_node.elts
)
return f"[\n{elements}\n{indent}]"
def _split_escaped_string_literal(content: str, max_length: int) -> list[str]:
chunks: list[str] = []
remaining = content
while len(remaining) > max_length:
split_index = remaining.rfind(" ", 0, max_length + 1)
if split_index <= 0:
split_index = max_length
if remaining[split_index - 1] == "\\":
split_index -= 1
chunks.append(remaining[:split_index])
remaining = remaining[split_index:]
chunks.append(remaining)
return chunks
def _format_wrapped_string_literal(value: str, indent: str, line_length: int) -> str:
value_repr = repr(value)
quote = value_repr[0]
escaped = value_repr[1:-1] if quote in {"'", '"'} and value_repr.endswith(quote) else value_repr
literal_indent = f"{indent} "
max_content_length = line_length - len(literal_indent) - 2
literal_lines = "\n".join(
f"{literal_indent}{quote}{chunk}{quote}" for chunk in _split_escaped_string_literal(escaped, max_content_length)
)
return f"(\n{literal_lines}\n{indent})"
def _format_call_argument_for_block(
keyword: ast.keyword,
indent: str,
line_length: int,
source: str,
*,
wrap_string_literal: bool,
) -> str:
if keyword.arg is None:
return f"**{_source_segment(source, keyword.value)}"
argument = f"{keyword.arg}={_inline_source_segment(source, keyword.value)}"
if len(f"{indent}{argument}") <= line_length:
return argument
formatted_argument = argument
match keyword.value:
case ast.Constant(value=str() as value) if wrap_string_literal:
formatted_argument = f"{keyword.arg}={_format_wrapped_string_literal(value, indent, line_length)}"
case ast.Dict() as value:
formatted_argument = f"{keyword.arg}={_format_dict_literal(value, indent, source, line_length)}"
case ast.Lambda(body=ast.Call() as value):
formatted_value = _format_call(
value,
indent,
line_length,
source,
wrap_string_literal=wrap_string_literal,
)
formatted_argument = f"{keyword.arg}=lambda: {formatted_value}"
case ast.Call() as value:
formatted_value = _format_call(
value,
indent,
line_length,
source,
wrap_string_literal=wrap_string_literal,
)
formatted_argument = f"{keyword.arg}={formatted_value}"
return formatted_argument
def _format_call( # noqa: PLR0913
call: ast.Call,
indent: str,
line_length: int,
source: str,
*,
force_trailing_comma: bool = False,
wrap_string_literal: bool = False,
) -> str:
call_name = _source_segment(source, call.func)
arguments = [_source_segment(source, argument) for argument in call.args]
continuation_indent = f"{indent} "
arguments.extend(
_format_call_argument_for_block(
keyword,
continuation_indent,
line_length,
source,
wrap_string_literal=wrap_string_literal,
)
for keyword in call.keywords
)
if not arguments:
return f"{call_name}()"
joined_arguments = ", ".join(arguments)
if "\n" not in joined_arguments and len(f"{continuation_indent}{joined_arguments}") <= line_length:
return f"{call_name}(\n{continuation_indent}{joined_arguments}\n{indent})"
has_trailing_comma = force_trailing_comma or len(arguments) > 1
argument_lines = "\n".join(
f"{continuation_indent}{argument}{',' if has_trailing_comma else ''}" for argument in arguments
)
return f"{call_name}(\n{argument_lines}\n{indent})"
def _format_constrained_call(call: ast.Call, indent: str, line_length: int, source: str) -> str:
call_name = _source_segment(source, call.func)
arguments = [_source_segment(source, argument) for argument in call.args]
arguments.extend(_format_call_argument(keyword, source) for keyword in call.keywords)
if not arguments:
return f"{call_name}()"
inline_call = f"{call_name}({', '.join(arguments)})"
if len(f"{indent}{inline_call}") <= line_length:
return inline_call
continuation_indent = f"{indent} "
has_trailing_comma = len(arguments) > 1
argument_lines = "\n".join(
f"{continuation_indent}{argument}{',' if has_trailing_comma else ''}" for argument in arguments
)
return f"{call_name}(\n{argument_lines}\n{indent})"
def _is_call(node: ast.AST | None, name: str) -> TypeGuard[ast.Call]:
return isinstance(node, ast.Call) and _is_name_or_attr(node.func, name)
def _has_attribute_root(node: ast.AST, name: str) -> bool:
while isinstance(node, ast.Attribute):
node = node.value
return isinstance(node, ast.Name) and node.id == name
def _is_datetime_module_call(node: ast.AST | None) -> TypeGuard[ast.Call]:
return isinstance(node, ast.Call) and _has_attribute_root(node.func, "datetime_module")
def _is_annotated(node: ast.AST) -> TypeGuard[ast.Subscript]:
return isinstance(node, ast.Subscript) and _is_name_or_attr(node.value, "Annotated")
def _is_list_of_annotated(node: ast.AST) -> TypeGuard[ast.Subscript]:
return isinstance(node, ast.Subscript) and _is_name_or_attr(node.value, "list") and _is_annotated(node.slice)
def _is_union(node: ast.AST) -> TypeGuard[ast.Subscript]:
return isinstance(node, ast.Subscript) and _is_name_or_attr(node.value, "Union")
_CONSTRAINED_CALL_NAMES = frozenset({"conbytes", "condecimal", "confloat", "conint", "conlist", "conset", "constr"})
def _is_constrained_string_call(node: ast.AST | None) -> TypeGuard[ast.Call]:
return _is_call(node, "constr")
def _is_constrained_call(node: ast.AST | None) -> TypeGuard[ast.Call]:
return any(_is_call(node, name) for name in _CONSTRAINED_CALL_NAMES)
def _contains_constrained_string_call(node: ast.AST) -> bool:
return any(_is_constrained_string_call(child) for child in ast.walk(node))
def _contains_annotated(node: ast.AST) -> bool:
return any(_is_annotated(child) for child in ast.walk(node))
def _contains_list_of_annotated(node: ast.AST) -> bool:
return any(_is_list_of_annotated(child) for child in ast.walk(node))
def _is_simple_union_annotation(node: ast.AST) -> bool:
if isinstance(node, ast.BinOp) and isinstance(node.op, ast.BitOr):
return _is_simple_union_annotation(node.left) and _is_simple_union_annotation(node.right)
return isinstance(node, ast.Name | ast.Attribute) or (isinstance(node, ast.Constant) and node.value is None)
def _should_format_constrained_call_union(
annotation: ast.AST,
value: ast.AST | None,
annotation_prefix: str,
line_length: int,
source: str,
) -> TypeGuard[ast.BinOp]:
if not isinstance(annotation, ast.BinOp) or not isinstance(annotation.op, ast.BitOr) or value is None:
return False
if not (_is_constrained_call(annotation.left) or _is_constrained_call(annotation.right)):
return False
constrained_call = annotation.left if _is_constrained_call(annotation.left) else annotation.right
return (
not _is_call(value, "Field")
or len(annotation_prefix) > line_length
or len(_source_segment(source, constrained_call)) > line_length - 24
)
def _can_parenthesize_field_value(annotation: ast.AST, target: str) -> bool:
if isinstance(annotation, ast.BinOp):
return False
if _is_constrained_string_call(annotation):
return target != "root"
return not _contains_constrained_string_call(annotation)
def _should_format_union_annotation(
annotation: ast.AST,
value: ast.AST | None,
target_prefix: str,
annotation_prefix: str,
line_length: int,
) -> TypeGuard[ast.BinOp]:
return (
isinstance(annotation, ast.BinOp)
and isinstance(annotation.op, ast.BitOr)
and value is not None
and not (isinstance(value, ast.Constant) and isinstance(value.value, str))
and (
(
len(annotation_prefix) > line_length
and (_contains_annotated(annotation) or _is_simple_union_annotation(annotation))
)
or _contains_list_of_annotated(annotation)
or (
len(f"{annotation_prefix} = {ast.unparse(value)}") > line_length
and len(target_prefix) > LONG_TARGET_PREFIX_LENGTH
and (_contains_annotated(annotation) or _is_simple_union_annotation(annotation))
)
or (
len(f"{annotation_prefix} = (") > line_length
and (_contains_annotated(annotation) or _is_simple_union_annotation(annotation))
)
)
)
def _iter_bit_or_elements(node: ast.AST) -> list[ast.AST]:
if isinstance(node, ast.BinOp) and isinstance(node.op, ast.BitOr):
return [*_iter_bit_or_elements(node.left), *_iter_bit_or_elements(node.right)]
return [node]
def _format_bit_or_element(
element: ast.AST,
indent: str,
line_length: int,
source: str,
prefix: str = "",
) -> list[str]:
element_source = _source_segment(source, element)
if not isinstance(element, ast.Subscript) or len(f"{indent}{prefix}{element_source}") <= line_length:
return [f"{indent}{prefix}{element_source}"]
value = _source_segment(source, element.value)
elements = _iter_subscript_elements(element)
trailing_comma = isinstance(element.slice, ast.Tuple)
formatted_lines = [f"{indent}{prefix}{value}["]
formatted_lines.extend(
f"{indent} {_source_segment(source, subscript_element)}{',' if trailing_comma else ''}"
for subscript_element in elements
)
formatted_lines.append(f"{indent}]")
return formatted_lines
def _format_bit_or_elements(annotation: ast.BinOp, indent: str, line_length: int, source: str) -> list[str]:
elements = _iter_bit_or_elements(annotation)
formatted_elements = _format_bit_or_element(elements[0], indent, line_length, source)
for element in elements[1:]:
formatted_elements.extend(_format_bit_or_element(element, indent, line_length, source, "| "))
return formatted_elements
def _format_parenthesized_bit_or_annotation(
annotation: ast.BinOp,
indent: str,
line_length: int,
source: str,
) -> str:
annotation_source = _source_segment(source, annotation)
if len(f"{indent} {annotation_source}") <= line_length:
formatted_annotation = f"{indent} {annotation_source}"
else:
formatted_annotation = "\n".join(_format_bit_or_elements(annotation, f"{indent} ", line_length, source))
return f"(\n{formatted_annotation}\n{indent})"
def _should_format_field_bit_or_annotation_assignment(
statement: ast.AnnAssign,
field_prefix: str,
annotation: str,
line_length: int,
) -> bool:
value_node = statement.value
if (
not isinstance(statement.annotation, ast.BinOp)
or not isinstance(statement.annotation.op, ast.BitOr)
or not _is_call(value_node, "Field")
):
return False
return len(f"{field_prefix}{annotation} = (") > line_length
def _should_format_field_bit_or_value_assignment(
statement: ast.AnnAssign,
value_prefix: str,
line_length: int,
source: str,
) -> bool:
value_node = statement.value
if (
not isinstance(statement.annotation, ast.BinOp)
or not isinstance(statement.annotation.op, ast.BitOr)
or not _is_call(value_node, "Field")
):
return False
call_start = f"{_source_segment(source, value_node.func)}("
return len(f"{value_prefix}{call_start}") > line_length and len(f"{value_prefix}(") <= line_length
def _format_parenthesized_field_value(
statement: ast.AnnAssign,
value_prefix: str,
line_length: int,
source: str,
*,
wrap_string_literal: bool,
) -> str:
value_node = statement.value
assert _is_call(value_node, "Field")
indent = value_prefix[: len(value_prefix) - len(value_prefix.lstrip())]
value = _source_segment(source, value_node)
if len(f"{indent} {value}") <= line_length:
return f"{value_prefix}(\n{indent} {value}\n{indent})"
formatted_value = _format_call(
value_node,
f"{indent} ",
line_length,
source,
wrap_string_literal=wrap_string_literal,
)
return f"{value_prefix}(\n{indent} {formatted_value}\n{indent})"
def _should_format_string_bit_or_annotation_assignment(
statement: ast.AnnAssign,
field_prefix: str,
annotation: str,
line_length: int,
source: str,
) -> bool:
return (
isinstance(statement.annotation, ast.BinOp)
and isinstance(statement.annotation.op, ast.BitOr)
and isinstance(statement.value, ast.Constant)
and isinstance(statement.value.value, str)
and len(f"{field_prefix}{annotation} = {_source_segment(source, statement.value)}") > line_length
and len(f"{field_prefix}{annotation} = (") > line_length
)
def _format_bit_or_annotation_assignment(
statement: ast.AnnAssign,
field_prefix: str,
line_length: int,
source: str,
) -> str:
value_node = statement.value
assert isinstance(statement.annotation, ast.BinOp)
assert value_node is not None
indent = field_prefix[: len(field_prefix) - len(field_prefix.lstrip())]
value = _source_segment(source, value_node)
formatted_annotation = _format_parenthesized_bit_or_annotation(
statement.annotation,
indent,
line_length,
source,
)
return f"{field_prefix}{formatted_annotation} = {value}"
def _iter_subscript_elements(node: ast.Subscript) -> list[ast.AST]:
if isinstance(node.slice, ast.Tuple):
return list(node.slice.elts)
return [node.slice]
def _format_annotated( # noqa: PLR0913
annotation: ast.Subscript,
indent: str,
line_length: int,
source: str,
closing_suffix: str = "",
*,
wrap_string_literal: bool = False,
) -> str:
continuation_indent = f"{indent} "
inline_elements = [_source_segment(source, element) for element in _iter_subscript_elements(annotation)]
joined_elements = ", ".join(inline_elements)
if len(f"{continuation_indent}{joined_elements}") <= line_length:
return f"Annotated[\n{continuation_indent}{joined_elements}\n{indent}]{closing_suffix}"
formatted_lines: list[str] = ["Annotated["]
for element in _iter_subscript_elements(annotation):
if _is_call(element, "Field") or _is_call(element, "Meta"):
inline_field = _source_segment(source, element)
if len(f"{continuation_indent}{inline_field},") <= line_length:
formatted_lines.append(f"{continuation_indent}{inline_field},")
continue
call_lines = _format_call(
element,
continuation_indent,
line_length,
source,
wrap_string_literal=wrap_string_literal,
).splitlines()
call_lines[-1] = f"{call_lines[-1]},"
formatted_lines.extend(_indent_first_line(call_lines, continuation_indent))
else:
formatted_lines.append(f"{continuation_indent}{_source_segment(source, element)},")
formatted_lines.append(f"{indent}]{closing_suffix}")
return "\n".join(formatted_lines)
def _config_dict_assignment(statement: ast.stmt) -> tuple[ast.Assign, ast.Call] | None:
if isinstance(statement, ast.Assign) and len(statement.targets) == 1 and _is_call(statement.value, "ConfigDict"):
return statement, statement.value
return None
def _format_generated_annotation_assignment( # noqa: PLR0911, PLR0912
statement: ast.AnnAssign,
line: str,
line_length: int,
source: str,
*,
wrap_string_literal: bool,
) -> str | None:
assert isinstance(statement.target, ast.Name)
indent = _line_indent(line)
target = statement.target.id
annotation = _source_segment(source, statement.annotation)
target_prefix = f"{indent}{target}: "
annotation_prefix = f"{target_prefix}{annotation}"
value_prefix = f"{annotation_prefix} = "
if statement.value is None and _is_list_of_annotated(statement.annotation):
return f"{target_prefix}{_format_list_of_annotated(statement.annotation, indent, line_length, source)}"
if _should_format_constrained_call_union(
statement.annotation,
statement.value,
annotation_prefix,
line_length,
source,