]> git.madduck.net Git - etc/vim.git/blob - src/black/linegen.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Tests for unicode identifiers (#2816)
[etc/vim.git] / src / black / linegen.py
1 """
2 Generating lines of code.
3 """
4 from functools import partial, wraps
5 import sys
6 from typing import Collection, Iterator, List, Optional, Set, Union
7
8 from black.nodes import WHITESPACE, RARROW, STATEMENT, STANDALONE_COMMENT
9 from black.nodes import ASSIGNMENTS, OPENING_BRACKETS, CLOSING_BRACKETS
10 from black.nodes import Visitor, syms, first_child_is_arith, ensure_visible
11 from black.nodes import is_docstring, is_empty_tuple, is_one_tuple, is_one_tuple_between
12 from black.nodes import is_name_token, is_lpar_token, is_rpar_token
13 from black.nodes import is_walrus_assignment, is_yield, is_vararg, is_multiline_string
14 from black.nodes import is_stub_suite, is_stub_body, is_atom_with_invisible_parens
15 from black.nodes import wrap_in_parentheses
16 from black.brackets import max_delimiter_priority_in_atom
17 from black.brackets import DOT_PRIORITY, COMMA_PRIORITY
18 from black.lines import Line, line_to_string, is_line_short_enough
19 from black.lines import can_omit_invisible_parens, can_be_split, append_leaves
20 from black.comments import generate_comments, list_comments, FMT_OFF
21 from black.numerics import normalize_numeric_literal
22 from black.strings import get_string_prefix, fix_docstring
23 from black.strings import normalize_string_prefix, normalize_string_quotes
24 from black.trans import Transformer, CannotTransform, StringMerger, StringSplitter
25 from black.trans import StringParenWrapper, StringParenStripper, hug_power_op
26 from black.mode import Mode, Feature, Preview
27
28 from blib2to3.pytree import Node, Leaf
29 from blib2to3.pgen2 import token
30
31
32 # types
33 LeafID = int
34 LN = Union[Leaf, Node]
35
36
37 class CannotSplit(CannotTransform):
38     """A readable split that fits the allotted line length is impossible."""
39
40
41 # This isn't a dataclass because @dataclass + Generic breaks mypyc.
42 # See also https://github.com/mypyc/mypyc/issues/827.
43 class LineGenerator(Visitor[Line]):
44     """Generates reformatted Line objects.  Empty lines are not emitted.
45
46     Note: destroys the tree it's visiting by mutating prefixes of its leaves
47     in ways that will no longer stringify to valid Python code on the tree.
48     """
49
50     def __init__(self, mode: Mode) -> None:
51         self.mode = mode
52         self.current_line: Line
53         self.__post_init__()
54
55     def line(self, indent: int = 0) -> Iterator[Line]:
56         """Generate a line.
57
58         If the line is empty, only emit if it makes sense.
59         If the line is too long, split it first and then generate.
60
61         If any lines were generated, set up a new current_line.
62         """
63         if not self.current_line:
64             self.current_line.depth += indent
65             return  # Line is empty, don't emit. Creating a new one unnecessary.
66
67         complete_line = self.current_line
68         self.current_line = Line(mode=self.mode, depth=complete_line.depth + indent)
69         yield complete_line
70
71     def visit_default(self, node: LN) -> Iterator[Line]:
72         """Default `visit_*()` implementation. Recurses to children of `node`."""
73         if isinstance(node, Leaf):
74             any_open_brackets = self.current_line.bracket_tracker.any_open_brackets()
75             for comment in generate_comments(node):
76                 if any_open_brackets:
77                     # any comment within brackets is subject to splitting
78                     self.current_line.append(comment)
79                 elif comment.type == token.COMMENT:
80                     # regular trailing comment
81                     self.current_line.append(comment)
82                     yield from self.line()
83
84                 else:
85                     # regular standalone comment
86                     yield from self.line()
87
88                     self.current_line.append(comment)
89                     yield from self.line()
90
91             normalize_prefix(node, inside_brackets=any_open_brackets)
92             if self.mode.string_normalization and node.type == token.STRING:
93                 node.value = normalize_string_prefix(node.value)
94                 node.value = normalize_string_quotes(node.value)
95             if node.type == token.NUMBER:
96                 normalize_numeric_literal(node)
97             if node.type not in WHITESPACE:
98                 self.current_line.append(node)
99         yield from super().visit_default(node)
100
101     def visit_INDENT(self, node: Leaf) -> Iterator[Line]:
102         """Increase indentation level, maybe yield a line."""
103         # In blib2to3 INDENT never holds comments.
104         yield from self.line(+1)
105         yield from self.visit_default(node)
106
107     def visit_DEDENT(self, node: Leaf) -> Iterator[Line]:
108         """Decrease indentation level, maybe yield a line."""
109         # The current line might still wait for trailing comments.  At DEDENT time
110         # there won't be any (they would be prefixes on the preceding NEWLINE).
111         # Emit the line then.
112         yield from self.line()
113
114         # While DEDENT has no value, its prefix may contain standalone comments
115         # that belong to the current indentation level.  Get 'em.
116         yield from self.visit_default(node)
117
118         # Finally, emit the dedent.
119         yield from self.line(-1)
120
121     def visit_stmt(
122         self, node: Node, keywords: Set[str], parens: Set[str]
123     ) -> Iterator[Line]:
124         """Visit a statement.
125
126         This implementation is shared for `if`, `while`, `for`, `try`, `except`,
127         `def`, `with`, `class`, `assert`, and assignments.
128
129         The relevant Python language `keywords` for a given statement will be
130         NAME leaves within it. This methods puts those on a separate line.
131
132         `parens` holds a set of string leaf values immediately after which
133         invisible parens should be put.
134         """
135         normalize_invisible_parens(node, parens_after=parens)
136         for child in node.children:
137             if is_name_token(child) and child.value in keywords:
138                 yield from self.line()
139
140             yield from self.visit(child)
141
142     def visit_match_case(self, node: Node) -> Iterator[Line]:
143         """Visit either a match or case statement."""
144         normalize_invisible_parens(node, parens_after=set())
145
146         yield from self.line()
147         for child in node.children:
148             yield from self.visit(child)
149
150     def visit_suite(self, node: Node) -> Iterator[Line]:
151         """Visit a suite."""
152         if self.mode.is_pyi and is_stub_suite(node):
153             yield from self.visit(node.children[2])
154         else:
155             yield from self.visit_default(node)
156
157     def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
158         """Visit a statement without nested statements."""
159         if first_child_is_arith(node):
160             wrap_in_parentheses(node, node.children[0], visible=False)
161         is_suite_like = node.parent and node.parent.type in STATEMENT
162         if is_suite_like:
163             if self.mode.is_pyi and is_stub_body(node):
164                 yield from self.visit_default(node)
165             else:
166                 yield from self.line(+1)
167                 yield from self.visit_default(node)
168                 yield from self.line(-1)
169
170         else:
171             if (
172                 not self.mode.is_pyi
173                 or not node.parent
174                 or not is_stub_suite(node.parent)
175             ):
176                 yield from self.line()
177             yield from self.visit_default(node)
178
179     def visit_async_stmt(self, node: Node) -> Iterator[Line]:
180         """Visit `async def`, `async for`, `async with`."""
181         yield from self.line()
182
183         children = iter(node.children)
184         for child in children:
185             yield from self.visit(child)
186
187             if child.type == token.ASYNC:
188                 break
189
190         internal_stmt = next(children)
191         for child in internal_stmt.children:
192             yield from self.visit(child)
193
194     def visit_decorators(self, node: Node) -> Iterator[Line]:
195         """Visit decorators."""
196         for child in node.children:
197             yield from self.line()
198             yield from self.visit(child)
199
200     def visit_power(self, node: Node) -> Iterator[Line]:
201         for idx, leaf in enumerate(node.children[:-1]):
202             next_leaf = node.children[idx + 1]
203
204             if not isinstance(leaf, Leaf):
205                 continue
206
207             value = leaf.value.lower()
208             if (
209                 leaf.type == token.NUMBER
210                 and next_leaf.type == syms.trailer
211                 # Ensure that we are in an attribute trailer
212                 and next_leaf.children[0].type == token.DOT
213                 # It shouldn't wrap hexadecimal, binary and octal literals
214                 and not value.startswith(("0x", "0b", "0o"))
215                 # It shouldn't wrap complex literals
216                 and "j" not in value
217             ):
218                 wrap_in_parentheses(node, leaf)
219
220         yield from self.visit_default(node)
221
222     def visit_SEMI(self, leaf: Leaf) -> Iterator[Line]:
223         """Remove a semicolon and put the other statement on a separate line."""
224         yield from self.line()
225
226     def visit_ENDMARKER(self, leaf: Leaf) -> Iterator[Line]:
227         """End of file. Process outstanding comments and end with a newline."""
228         yield from self.visit_default(leaf)
229         yield from self.line()
230
231     def visit_STANDALONE_COMMENT(self, leaf: Leaf) -> Iterator[Line]:
232         if not self.current_line.bracket_tracker.any_open_brackets():
233             yield from self.line()
234         yield from self.visit_default(leaf)
235
236     def visit_factor(self, node: Node) -> Iterator[Line]:
237         """Force parentheses between a unary op and a binary power:
238
239         -2 ** 8 -> -(2 ** 8)
240         """
241         _operator, operand = node.children
242         if (
243             operand.type == syms.power
244             and len(operand.children) == 3
245             and operand.children[1].type == token.DOUBLESTAR
246         ):
247             lpar = Leaf(token.LPAR, "(")
248             rpar = Leaf(token.RPAR, ")")
249             index = operand.remove() or 0
250             node.insert_child(index, Node(syms.atom, [lpar, operand, rpar]))
251         yield from self.visit_default(node)
252
253     def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
254         if is_docstring(leaf) and "\\\n" not in leaf.value:
255             # We're ignoring docstrings with backslash newline escapes because changing
256             # indentation of those changes the AST representation of the code.
257             docstring = normalize_string_prefix(leaf.value)
258             prefix = get_string_prefix(docstring)
259             docstring = docstring[len(prefix) :]  # Remove the prefix
260             quote_char = docstring[0]
261             # A natural way to remove the outer quotes is to do:
262             #   docstring = docstring.strip(quote_char)
263             # but that breaks on """""x""" (which is '""x').
264             # So we actually need to remove the first character and the next two
265             # characters but only if they are the same as the first.
266             quote_len = 1 if docstring[1] != quote_char else 3
267             docstring = docstring[quote_len:-quote_len]
268             docstring_started_empty = not docstring
269
270             if is_multiline_string(leaf):
271                 indent = " " * 4 * self.current_line.depth
272                 docstring = fix_docstring(docstring, indent)
273             else:
274                 docstring = docstring.strip()
275
276             if docstring:
277                 # Add some padding if the docstring starts / ends with a quote mark.
278                 if docstring[0] == quote_char:
279                     docstring = " " + docstring
280                 if docstring[-1] == quote_char:
281                     docstring += " "
282                 if docstring[-1] == "\\":
283                     backslash_count = len(docstring) - len(docstring.rstrip("\\"))
284                     if backslash_count % 2:
285                         # Odd number of tailing backslashes, add some padding to
286                         # avoid escaping the closing string quote.
287                         docstring += " "
288             elif not docstring_started_empty:
289                 docstring = " "
290
291             # We could enforce triple quotes at this point.
292             quote = quote_char * quote_len
293             leaf.value = prefix + quote + docstring + quote
294
295         yield from self.visit_default(leaf)
296
297     def __post_init__(self) -> None:
298         """You are in a twisty little maze of passages."""
299         self.current_line = Line(mode=self.mode)
300
301         v = self.visit_stmt
302         Ø: Set[str] = set()
303         self.visit_assert_stmt = partial(v, keywords={"assert"}, parens={"assert", ","})
304         self.visit_if_stmt = partial(
305             v, keywords={"if", "else", "elif"}, parens={"if", "elif"}
306         )
307         self.visit_while_stmt = partial(v, keywords={"while", "else"}, parens={"while"})
308         self.visit_for_stmt = partial(v, keywords={"for", "else"}, parens={"for", "in"})
309         self.visit_try_stmt = partial(
310             v, keywords={"try", "except", "else", "finally"}, parens=Ø
311         )
312         self.visit_except_clause = partial(v, keywords={"except"}, parens=Ø)
313         self.visit_with_stmt = partial(v, keywords={"with"}, parens=Ø)
314         self.visit_funcdef = partial(v, keywords={"def"}, parens=Ø)
315         self.visit_classdef = partial(v, keywords={"class"}, parens=Ø)
316         self.visit_expr_stmt = partial(v, keywords=Ø, parens=ASSIGNMENTS)
317         self.visit_return_stmt = partial(v, keywords={"return"}, parens={"return"})
318         self.visit_import_from = partial(v, keywords=Ø, parens={"import"})
319         self.visit_del_stmt = partial(v, keywords=Ø, parens={"del"})
320         self.visit_async_funcdef = self.visit_async_stmt
321         self.visit_decorated = self.visit_decorators
322
323         # PEP 634
324         self.visit_match_stmt = self.visit_match_case
325         self.visit_case_block = self.visit_match_case
326
327
328 def transform_line(
329     line: Line, mode: Mode, features: Collection[Feature] = ()
330 ) -> Iterator[Line]:
331     """Transform a `line`, potentially splitting it into many lines.
332
333     They should fit in the allotted `line_length` but might not be able to.
334
335     `features` are syntactical features that may be used in the output.
336     """
337     if line.is_comment:
338         yield line
339         return
340
341     line_str = line_to_string(line)
342
343     ll = mode.line_length
344     sn = mode.string_normalization
345     string_merge = StringMerger(ll, sn)
346     string_paren_strip = StringParenStripper(ll, sn)
347     string_split = StringSplitter(ll, sn)
348     string_paren_wrap = StringParenWrapper(ll, sn)
349
350     transformers: List[Transformer]
351     if (
352         not line.contains_uncollapsable_type_comments()
353         and not line.should_split_rhs
354         and not line.magic_trailing_comma
355         and (
356             is_line_short_enough(line, line_length=mode.line_length, line_str=line_str)
357             or line.contains_unsplittable_type_ignore()
358         )
359         and not (line.inside_brackets and line.contains_standalone_comments())
360     ):
361         # Only apply basic string preprocessing, since lines shouldn't be split here.
362         if Preview.string_processing in mode:
363             transformers = [string_merge, string_paren_strip]
364         else:
365             transformers = []
366     elif line.is_def:
367         transformers = [left_hand_split]
368     else:
369
370         def _rhs(
371             self: object, line: Line, features: Collection[Feature]
372         ) -> Iterator[Line]:
373             """Wraps calls to `right_hand_split`.
374
375             The calls increasingly `omit` right-hand trailers (bracket pairs with
376             content), meaning the trailers get glued together to split on another
377             bracket pair instead.
378             """
379             for omit in generate_trailers_to_omit(line, mode.line_length):
380                 lines = list(
381                     right_hand_split(line, mode.line_length, features, omit=omit)
382                 )
383                 # Note: this check is only able to figure out if the first line of the
384                 # *current* transformation fits in the line length.  This is true only
385                 # for simple cases.  All others require running more transforms via
386                 # `transform_line()`.  This check doesn't know if those would succeed.
387                 if is_line_short_enough(lines[0], line_length=mode.line_length):
388                     yield from lines
389                     return
390
391             # All splits failed, best effort split with no omits.
392             # This mostly happens to multiline strings that are by definition
393             # reported as not fitting a single line, as well as lines that contain
394             # trailing commas (those have to be exploded).
395             yield from right_hand_split(
396                 line, line_length=mode.line_length, features=features
397             )
398
399         # HACK: nested functions (like _rhs) compiled by mypyc don't retain their
400         # __name__ attribute which is needed in `run_transformer` further down.
401         # Unfortunately a nested class breaks mypyc too. So a class must be created
402         # via type ... https://github.com/mypyc/mypyc/issues/884
403         rhs = type("rhs", (), {"__call__": _rhs})()
404
405         if Preview.string_processing in mode:
406             if line.inside_brackets:
407                 transformers = [
408                     string_merge,
409                     string_paren_strip,
410                     string_split,
411                     delimiter_split,
412                     standalone_comment_split,
413                     string_paren_wrap,
414                     rhs,
415                 ]
416             else:
417                 transformers = [
418                     string_merge,
419                     string_paren_strip,
420                     string_split,
421                     string_paren_wrap,
422                     rhs,
423                 ]
424         else:
425             if line.inside_brackets:
426                 transformers = [delimiter_split, standalone_comment_split, rhs]
427             else:
428                 transformers = [rhs]
429     # It's always safe to attempt hugging of power operations and pretty much every line
430     # could match.
431     transformers.append(hug_power_op)
432
433     for transform in transformers:
434         # We are accumulating lines in `result` because we might want to abort
435         # mission and return the original line in the end, or attempt a different
436         # split altogether.
437         try:
438             result = run_transformer(line, transform, mode, features, line_str=line_str)
439         except CannotTransform:
440             continue
441         else:
442             yield from result
443             break
444
445     else:
446         yield line
447
448
449 def left_hand_split(line: Line, _features: Collection[Feature] = ()) -> Iterator[Line]:
450     """Split line into many lines, starting with the first matching bracket pair.
451
452     Note: this usually looks weird, only use this for function definitions.
453     Prefer RHS otherwise.  This is why this function is not symmetrical with
454     :func:`right_hand_split` which also handles optional parentheses.
455     """
456     tail_leaves: List[Leaf] = []
457     body_leaves: List[Leaf] = []
458     head_leaves: List[Leaf] = []
459     current_leaves = head_leaves
460     matching_bracket: Optional[Leaf] = None
461     for leaf in line.leaves:
462         if (
463             current_leaves is body_leaves
464             and leaf.type in CLOSING_BRACKETS
465             and leaf.opening_bracket is matching_bracket
466         ):
467             current_leaves = tail_leaves if body_leaves else head_leaves
468         current_leaves.append(leaf)
469         if current_leaves is head_leaves:
470             if leaf.type in OPENING_BRACKETS:
471                 matching_bracket = leaf
472                 current_leaves = body_leaves
473     if not matching_bracket:
474         raise CannotSplit("No brackets found")
475
476     head = bracket_split_build_line(head_leaves, line, matching_bracket)
477     body = bracket_split_build_line(body_leaves, line, matching_bracket, is_body=True)
478     tail = bracket_split_build_line(tail_leaves, line, matching_bracket)
479     bracket_split_succeeded_or_raise(head, body, tail)
480     for result in (head, body, tail):
481         if result:
482             yield result
483
484
485 def right_hand_split(
486     line: Line,
487     line_length: int,
488     features: Collection[Feature] = (),
489     omit: Collection[LeafID] = (),
490 ) -> Iterator[Line]:
491     """Split line into many lines, starting with the last matching bracket pair.
492
493     If the split was by optional parentheses, attempt splitting without them, too.
494     `omit` is a collection of closing bracket IDs that shouldn't be considered for
495     this split.
496
497     Note: running this function modifies `bracket_depth` on the leaves of `line`.
498     """
499     tail_leaves: List[Leaf] = []
500     body_leaves: List[Leaf] = []
501     head_leaves: List[Leaf] = []
502     current_leaves = tail_leaves
503     opening_bracket: Optional[Leaf] = None
504     closing_bracket: Optional[Leaf] = None
505     for leaf in reversed(line.leaves):
506         if current_leaves is body_leaves:
507             if leaf is opening_bracket:
508                 current_leaves = head_leaves if body_leaves else tail_leaves
509         current_leaves.append(leaf)
510         if current_leaves is tail_leaves:
511             if leaf.type in CLOSING_BRACKETS and id(leaf) not in omit:
512                 opening_bracket = leaf.opening_bracket
513                 closing_bracket = leaf
514                 current_leaves = body_leaves
515     if not (opening_bracket and closing_bracket and head_leaves):
516         # If there is no opening or closing_bracket that means the split failed and
517         # all content is in the tail.  Otherwise, if `head_leaves` are empty, it means
518         # the matching `opening_bracket` wasn't available on `line` anymore.
519         raise CannotSplit("No brackets found")
520
521     tail_leaves.reverse()
522     body_leaves.reverse()
523     head_leaves.reverse()
524     head = bracket_split_build_line(head_leaves, line, opening_bracket)
525     body = bracket_split_build_line(body_leaves, line, opening_bracket, is_body=True)
526     tail = bracket_split_build_line(tail_leaves, line, opening_bracket)
527     bracket_split_succeeded_or_raise(head, body, tail)
528     if (
529         Feature.FORCE_OPTIONAL_PARENTHESES not in features
530         # the opening bracket is an optional paren
531         and opening_bracket.type == token.LPAR
532         and not opening_bracket.value
533         # the closing bracket is an optional paren
534         and closing_bracket.type == token.RPAR
535         and not closing_bracket.value
536         # it's not an import (optional parens are the only thing we can split on
537         # in this case; attempting a split without them is a waste of time)
538         and not line.is_import
539         # there are no standalone comments in the body
540         and not body.contains_standalone_comments(0)
541         # and we can actually remove the parens
542         and can_omit_invisible_parens(body, line_length, omit_on_explode=omit)
543     ):
544         omit = {id(closing_bracket), *omit}
545         try:
546             yield from right_hand_split(line, line_length, features=features, omit=omit)
547             return
548
549         except CannotSplit as e:
550             if not (
551                 can_be_split(body)
552                 or is_line_short_enough(body, line_length=line_length)
553             ):
554                 raise CannotSplit(
555                     "Splitting failed, body is still too long and can't be split."
556                 ) from e
557
558             elif head.contains_multiline_strings() or tail.contains_multiline_strings():
559                 raise CannotSplit(
560                     "The current optional pair of parentheses is bound to fail to"
561                     " satisfy the splitting algorithm because the head or the tail"
562                     " contains multiline strings which by definition never fit one"
563                     " line."
564                 ) from e
565
566     ensure_visible(opening_bracket)
567     ensure_visible(closing_bracket)
568     for result in (head, body, tail):
569         if result:
570             yield result
571
572
573 def bracket_split_succeeded_or_raise(head: Line, body: Line, tail: Line) -> None:
574     """Raise :exc:`CannotSplit` if the last left- or right-hand split failed.
575
576     Do nothing otherwise.
577
578     A left- or right-hand split is based on a pair of brackets. Content before
579     (and including) the opening bracket is left on one line, content inside the
580     brackets is put on a separate line, and finally content starting with and
581     following the closing bracket is put on a separate line.
582
583     Those are called `head`, `body`, and `tail`, respectively. If the split
584     produced the same line (all content in `head`) or ended up with an empty `body`
585     and the `tail` is just the closing bracket, then it's considered failed.
586     """
587     tail_len = len(str(tail).strip())
588     if not body:
589         if tail_len == 0:
590             raise CannotSplit("Splitting brackets produced the same line")
591
592         elif tail_len < 3:
593             raise CannotSplit(
594                 f"Splitting brackets on an empty body to save {tail_len} characters is"
595                 " not worth it"
596             )
597
598
599 def bracket_split_build_line(
600     leaves: List[Leaf], original: Line, opening_bracket: Leaf, *, is_body: bool = False
601 ) -> Line:
602     """Return a new line with given `leaves` and respective comments from `original`.
603
604     If `is_body` is True, the result line is one-indented inside brackets and as such
605     has its first leaf's prefix normalized and a trailing comma added when expected.
606     """
607     result = Line(mode=original.mode, depth=original.depth)
608     if is_body:
609         result.inside_brackets = True
610         result.depth += 1
611         if leaves:
612             # Since body is a new indent level, remove spurious leading whitespace.
613             normalize_prefix(leaves[0], inside_brackets=True)
614             # Ensure a trailing comma for imports and standalone function arguments, but
615             # be careful not to add one after any comments or within type annotations.
616             no_commas = (
617                 original.is_def
618                 and opening_bracket.value == "("
619                 and not any(leaf.type == token.COMMA for leaf in leaves)
620                 # In particular, don't add one within a parenthesized return annotation.
621                 # Unfortunately the indicator we're in a return annotation (RARROW) may
622                 # be defined directly in the parent node, the parent of the parent ...
623                 # and so on depending on how complex the return annotation is.
624                 # This isn't perfect and there's some false negatives but they are in
625                 # contexts were a comma is actually fine.
626                 and not any(
627                     node.prev_sibling.type == RARROW
628                     for node in (
629                         leaves[0].parent,
630                         getattr(leaves[0].parent, "parent", None),
631                     )
632                     if isinstance(node, Node) and isinstance(node.prev_sibling, Leaf)
633                 )
634             )
635
636             if original.is_import or no_commas:
637                 for i in range(len(leaves) - 1, -1, -1):
638                     if leaves[i].type == STANDALONE_COMMENT:
639                         continue
640
641                     if leaves[i].type != token.COMMA:
642                         new_comma = Leaf(token.COMMA, ",")
643                         leaves.insert(i + 1, new_comma)
644                     break
645
646     # Populate the line
647     for leaf in leaves:
648         result.append(leaf, preformatted=True)
649         for comment_after in original.comments_after(leaf):
650             result.append(comment_after, preformatted=True)
651     if is_body and should_split_line(result, opening_bracket):
652         result.should_split_rhs = True
653     return result
654
655
656 def dont_increase_indentation(split_func: Transformer) -> Transformer:
657     """Normalize prefix of the first leaf in every line returned by `split_func`.
658
659     This is a decorator over relevant split functions.
660     """
661
662     @wraps(split_func)
663     def split_wrapper(line: Line, features: Collection[Feature] = ()) -> Iterator[Line]:
664         for line in split_func(line, features):
665             normalize_prefix(line.leaves[0], inside_brackets=True)
666             yield line
667
668     return split_wrapper
669
670
671 @dont_increase_indentation
672 def delimiter_split(line: Line, features: Collection[Feature] = ()) -> Iterator[Line]:
673     """Split according to delimiters of the highest priority.
674
675     If the appropriate Features are given, the split will add trailing commas
676     also in function signatures and calls that contain `*` and `**`.
677     """
678     try:
679         last_leaf = line.leaves[-1]
680     except IndexError:
681         raise CannotSplit("Line empty") from None
682
683     bt = line.bracket_tracker
684     try:
685         delimiter_priority = bt.max_delimiter_priority(exclude={id(last_leaf)})
686     except ValueError:
687         raise CannotSplit("No delimiters found") from None
688
689     if delimiter_priority == DOT_PRIORITY:
690         if bt.delimiter_count_with_priority(delimiter_priority) == 1:
691             raise CannotSplit("Splitting a single attribute from its owner looks wrong")
692
693     current_line = Line(
694         mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets
695     )
696     lowest_depth = sys.maxsize
697     trailing_comma_safe = True
698
699     def append_to_line(leaf: Leaf) -> Iterator[Line]:
700         """Append `leaf` to current line or to new line if appending impossible."""
701         nonlocal current_line
702         try:
703             current_line.append_safe(leaf, preformatted=True)
704         except ValueError:
705             yield current_line
706
707             current_line = Line(
708                 mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets
709             )
710             current_line.append(leaf)
711
712     for leaf in line.leaves:
713         yield from append_to_line(leaf)
714
715         for comment_after in line.comments_after(leaf):
716             yield from append_to_line(comment_after)
717
718         lowest_depth = min(lowest_depth, leaf.bracket_depth)
719         if leaf.bracket_depth == lowest_depth:
720             if is_vararg(leaf, within={syms.typedargslist}):
721                 trailing_comma_safe = (
722                     trailing_comma_safe and Feature.TRAILING_COMMA_IN_DEF in features
723                 )
724             elif is_vararg(leaf, within={syms.arglist, syms.argument}):
725                 trailing_comma_safe = (
726                     trailing_comma_safe and Feature.TRAILING_COMMA_IN_CALL in features
727                 )
728
729         leaf_priority = bt.delimiters.get(id(leaf))
730         if leaf_priority == delimiter_priority:
731             yield current_line
732
733             current_line = Line(
734                 mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets
735             )
736     if current_line:
737         if (
738             trailing_comma_safe
739             and delimiter_priority == COMMA_PRIORITY
740             and current_line.leaves[-1].type != token.COMMA
741             and current_line.leaves[-1].type != STANDALONE_COMMENT
742         ):
743             new_comma = Leaf(token.COMMA, ",")
744             current_line.append(new_comma)
745         yield current_line
746
747
748 @dont_increase_indentation
749 def standalone_comment_split(
750     line: Line, features: Collection[Feature] = ()
751 ) -> Iterator[Line]:
752     """Split standalone comments from the rest of the line."""
753     if not line.contains_standalone_comments(0):
754         raise CannotSplit("Line does not have any standalone comments")
755
756     current_line = Line(
757         mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets
758     )
759
760     def append_to_line(leaf: Leaf) -> Iterator[Line]:
761         """Append `leaf` to current line or to new line if appending impossible."""
762         nonlocal current_line
763         try:
764             current_line.append_safe(leaf, preformatted=True)
765         except ValueError:
766             yield current_line
767
768             current_line = Line(
769                 line.mode, depth=line.depth, inside_brackets=line.inside_brackets
770             )
771             current_line.append(leaf)
772
773     for leaf in line.leaves:
774         yield from append_to_line(leaf)
775
776         for comment_after in line.comments_after(leaf):
777             yield from append_to_line(comment_after)
778
779     if current_line:
780         yield current_line
781
782
783 def normalize_prefix(leaf: Leaf, *, inside_brackets: bool) -> None:
784     """Leave existing extra newlines if not `inside_brackets`. Remove everything
785     else.
786
787     Note: don't use backslashes for formatting or you'll lose your voting rights.
788     """
789     if not inside_brackets:
790         spl = leaf.prefix.split("#")
791         if "\\" not in spl[0]:
792             nl_count = spl[-1].count("\n")
793             if len(spl) > 1:
794                 nl_count -= 1
795             leaf.prefix = "\n" * nl_count
796             return
797
798     leaf.prefix = ""
799
800
801 def normalize_invisible_parens(node: Node, parens_after: Set[str]) -> None:
802     """Make existing optional parentheses invisible or create new ones.
803
804     `parens_after` is a set of string leaf values immediately after which parens
805     should be put.
806
807     Standardizes on visible parentheses for single-element tuples, and keeps
808     existing visible parentheses for other tuples and generator expressions.
809     """
810     for pc in list_comments(node.prefix, is_endmarker=False):
811         if pc.value in FMT_OFF:
812             # This `node` has a prefix with `# fmt: off`, don't mess with parens.
813             return
814     check_lpar = False
815     for index, child in enumerate(list(node.children)):
816         # Fixes a bug where invisible parens are not properly stripped from
817         # assignment statements that contain type annotations.
818         if isinstance(child, Node) and child.type == syms.annassign:
819             normalize_invisible_parens(child, parens_after=parens_after)
820
821         # Add parentheses around long tuple unpacking in assignments.
822         if (
823             index == 0
824             and isinstance(child, Node)
825             and child.type == syms.testlist_star_expr
826         ):
827             check_lpar = True
828
829         if check_lpar:
830             if child.type == syms.atom:
831                 if maybe_make_parens_invisible_in_atom(child, parent=node):
832                     wrap_in_parentheses(node, child, visible=False)
833             elif is_one_tuple(child):
834                 wrap_in_parentheses(node, child, visible=True)
835             elif node.type == syms.import_from:
836                 # "import from" nodes store parentheses directly as part of
837                 # the statement
838                 if is_lpar_token(child):
839                     assert is_rpar_token(node.children[-1])
840                     # make parentheses invisible
841                     child.value = ""
842                     node.children[-1].value = ""
843                 elif child.type != token.STAR:
844                     # insert invisible parentheses
845                     node.insert_child(index, Leaf(token.LPAR, ""))
846                     node.append_child(Leaf(token.RPAR, ""))
847                 break
848
849             elif not (isinstance(child, Leaf) and is_multiline_string(child)):
850                 wrap_in_parentheses(node, child, visible=False)
851
852         check_lpar = isinstance(child, Leaf) and child.value in parens_after
853
854
855 def maybe_make_parens_invisible_in_atom(node: LN, parent: LN) -> bool:
856     """If it's safe, make the parens in the atom `node` invisible, recursively.
857     Additionally, remove repeated, adjacent invisible parens from the atom `node`
858     as they are redundant.
859
860     Returns whether the node should itself be wrapped in invisible parentheses.
861
862     """
863
864     if (
865         node.type != syms.atom
866         or is_empty_tuple(node)
867         or is_one_tuple(node)
868         or (is_yield(node) and parent.type != syms.expr_stmt)
869         or max_delimiter_priority_in_atom(node) >= COMMA_PRIORITY
870     ):
871         return False
872
873     if is_walrus_assignment(node):
874         if parent.type in [
875             syms.annassign,
876             syms.expr_stmt,
877             syms.assert_stmt,
878             syms.return_stmt,
879             # these ones aren't useful to end users, but they do please fuzzers
880             syms.for_stmt,
881             syms.del_stmt,
882         ]:
883             return False
884
885     first = node.children[0]
886     last = node.children[-1]
887     if is_lpar_token(first) and is_rpar_token(last):
888         middle = node.children[1]
889         # make parentheses invisible
890         first.value = ""
891         last.value = ""
892         maybe_make_parens_invisible_in_atom(middle, parent=parent)
893
894         if is_atom_with_invisible_parens(middle):
895             # Strip the invisible parens from `middle` by replacing
896             # it with the child in-between the invisible parens
897             middle.replace(middle.children[1])
898
899         return False
900
901     return True
902
903
904 def should_split_line(line: Line, opening_bracket: Leaf) -> bool:
905     """Should `line` be immediately split with `delimiter_split()` after RHS?"""
906
907     if not (opening_bracket.parent and opening_bracket.value in "[{("):
908         return False
909
910     # We're essentially checking if the body is delimited by commas and there's more
911     # than one of them (we're excluding the trailing comma and if the delimiter priority
912     # is still commas, that means there's more).
913     exclude = set()
914     trailing_comma = False
915     try:
916         last_leaf = line.leaves[-1]
917         if last_leaf.type == token.COMMA:
918             trailing_comma = True
919             exclude.add(id(last_leaf))
920         max_priority = line.bracket_tracker.max_delimiter_priority(exclude=exclude)
921     except (IndexError, ValueError):
922         return False
923
924     return max_priority == COMMA_PRIORITY and (
925         (line.mode.magic_trailing_comma and trailing_comma)
926         # always explode imports
927         or opening_bracket.parent.type in {syms.atom, syms.import_from}
928     )
929
930
931 def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[LeafID]]:
932     """Generate sets of closing bracket IDs that should be omitted in a RHS.
933
934     Brackets can be omitted if the entire trailer up to and including
935     a preceding closing bracket fits in one line.
936
937     Yielded sets are cumulative (contain results of previous yields, too).  First
938     set is empty, unless the line should explode, in which case bracket pairs until
939     the one that needs to explode are omitted.
940     """
941
942     omit: Set[LeafID] = set()
943     if not line.magic_trailing_comma:
944         yield omit
945
946     length = 4 * line.depth
947     opening_bracket: Optional[Leaf] = None
948     closing_bracket: Optional[Leaf] = None
949     inner_brackets: Set[LeafID] = set()
950     for index, leaf, leaf_length in line.enumerate_with_length(reversed=True):
951         length += leaf_length
952         if length > line_length:
953             break
954
955         has_inline_comment = leaf_length > len(leaf.value) + len(leaf.prefix)
956         if leaf.type == STANDALONE_COMMENT or has_inline_comment:
957             break
958
959         if opening_bracket:
960             if leaf is opening_bracket:
961                 opening_bracket = None
962             elif leaf.type in CLOSING_BRACKETS:
963                 prev = line.leaves[index - 1] if index > 0 else None
964                 if (
965                     prev
966                     and prev.type == token.COMMA
967                     and leaf.opening_bracket is not None
968                     and not is_one_tuple_between(
969                         leaf.opening_bracket, leaf, line.leaves
970                     )
971                 ):
972                     # Never omit bracket pairs with trailing commas.
973                     # We need to explode on those.
974                     break
975
976                 inner_brackets.add(id(leaf))
977         elif leaf.type in CLOSING_BRACKETS:
978             prev = line.leaves[index - 1] if index > 0 else None
979             if prev and prev.type in OPENING_BRACKETS:
980                 # Empty brackets would fail a split so treat them as "inner"
981                 # brackets (e.g. only add them to the `omit` set if another
982                 # pair of brackets was good enough.
983                 inner_brackets.add(id(leaf))
984                 continue
985
986             if closing_bracket:
987                 omit.add(id(closing_bracket))
988                 omit.update(inner_brackets)
989                 inner_brackets.clear()
990                 yield omit
991
992             if (
993                 prev
994                 and prev.type == token.COMMA
995                 and leaf.opening_bracket is not None
996                 and not is_one_tuple_between(leaf.opening_bracket, leaf, line.leaves)
997             ):
998                 # Never omit bracket pairs with trailing commas.
999                 # We need to explode on those.
1000                 break
1001
1002             if leaf.value:
1003                 opening_bracket = leaf.opening_bracket
1004                 closing_bracket = leaf
1005
1006
1007 def run_transformer(
1008     line: Line,
1009     transform: Transformer,
1010     mode: Mode,
1011     features: Collection[Feature],
1012     *,
1013     line_str: str = "",
1014 ) -> List[Line]:
1015     if not line_str:
1016         line_str = line_to_string(line)
1017     result: List[Line] = []
1018     for transformed_line in transform(line, features):
1019         if str(transformed_line).strip("\n") == line_str:
1020             raise CannotTransform("Line transformer returned an unchanged result")
1021
1022         result.extend(transform_line(transformed_line, mode=mode, features=features))
1023
1024     if (
1025         transform.__class__.__name__ != "rhs"
1026         or not line.bracket_tracker.invisible
1027         or any(bracket.value for bracket in line.bracket_tracker.invisible)
1028         or line.contains_multiline_strings()
1029         or result[0].contains_uncollapsable_type_comments()
1030         or result[0].contains_unsplittable_type_ignore()
1031         or is_line_short_enough(result[0], line_length=mode.line_length)
1032         # If any leaves have no parents (which _can_ occur since
1033         # `transform(line)` potentially destroys the line's underlying node
1034         # structure), then we can't proceed. Doing so would cause the below
1035         # call to `append_leaves()` to fail.
1036         or any(leaf.parent is None for leaf in line.leaves)
1037     ):
1038         return result
1039
1040     line_copy = line.clone()
1041     append_leaves(line_copy, line, line.leaves)
1042     features_fop = set(features) | {Feature.FORCE_OPTIONAL_PARENTHESES}
1043     second_opinion = run_transformer(
1044         line_copy, transform, mode, features_fop, line_str=line_str
1045     )
1046     if all(
1047         is_line_short_enough(ln, line_length=mode.line_length) for ln in second_opinion
1048     ):
1049         result = second_opinion
1050     return result