]> git.madduck.net Git - etc/vim.git/blob - src/black/lines.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:

Bump furo from 2022.3.4 to 2022.4.7 in /docs (#3003)
[etc/vim.git] / src / black / lines.py
1 from dataclasses import dataclass, field
2 import itertools
3 import sys
4 from typing import (
5     Callable,
6     Dict,
7     Iterator,
8     List,
9     Optional,
10     Sequence,
11     Tuple,
12     TypeVar,
13     cast,
14 )
15
16 from blib2to3.pytree import Node, Leaf
17 from blib2to3.pgen2 import token
18
19 from black.brackets import BracketTracker, DOT_PRIORITY
20 from black.mode import Mode, Preview
21 from black.nodes import STANDALONE_COMMENT, TEST_DESCENDANTS
22 from black.nodes import BRACKETS, OPENING_BRACKETS, CLOSING_BRACKETS
23 from black.nodes import syms, whitespace, replace_child, child_towards
24 from black.nodes import is_multiline_string, is_import, is_type_comment
25 from black.nodes import is_one_sequence_between
26
27 # types
28 T = TypeVar("T")
29 Index = int
30 LeafID = int
31
32
33 @dataclass
34 class Line:
35     """Holds leaves and comments. Can be printed with `str(line)`."""
36
37     mode: Mode
38     depth: int = 0
39     leaves: List[Leaf] = field(default_factory=list)
40     # keys ordered like `leaves`
41     comments: Dict[LeafID, List[Leaf]] = field(default_factory=dict)
42     bracket_tracker: BracketTracker = field(default_factory=BracketTracker)
43     inside_brackets: bool = False
44     should_split_rhs: bool = False
45     magic_trailing_comma: Optional[Leaf] = None
46
47     def append(self, leaf: Leaf, preformatted: bool = False) -> None:
48         """Add a new `leaf` to the end of the line.
49
50         Unless `preformatted` is True, the `leaf` will receive a new consistent
51         whitespace prefix and metadata applied by :class:`BracketTracker`.
52         Trailing commas are maybe removed, unpacked for loop variables are
53         demoted from being delimiters.
54
55         Inline comments are put aside.
56         """
57         has_value = leaf.type in BRACKETS or bool(leaf.value.strip())
58         if not has_value:
59             return
60
61         if token.COLON == leaf.type and self.is_class_paren_empty:
62             del self.leaves[-2:]
63         if self.leaves and not preformatted:
64             # Note: at this point leaf.prefix should be empty except for
65             # imports, for which we only preserve newlines.
66             leaf.prefix += whitespace(
67                 leaf, complex_subscript=self.is_complex_subscript(leaf)
68             )
69         if self.inside_brackets or not preformatted:
70             self.bracket_tracker.mark(leaf)
71             if self.mode.magic_trailing_comma:
72                 if self.has_magic_trailing_comma(leaf):
73                     self.magic_trailing_comma = leaf
74             elif self.has_magic_trailing_comma(leaf, ensure_removable=True):
75                 self.remove_trailing_comma()
76         if not self.append_comment(leaf):
77             self.leaves.append(leaf)
78
79     def append_safe(self, leaf: Leaf, preformatted: bool = False) -> None:
80         """Like :func:`append()` but disallow invalid standalone comment structure.
81
82         Raises ValueError when any `leaf` is appended after a standalone comment
83         or when a standalone comment is not the first leaf on the line.
84         """
85         if self.bracket_tracker.depth == 0:
86             if self.is_comment:
87                 raise ValueError("cannot append to standalone comments")
88
89             if self.leaves and leaf.type == STANDALONE_COMMENT:
90                 raise ValueError(
91                     "cannot append standalone comments to a populated line"
92                 )
93
94         self.append(leaf, preformatted=preformatted)
95
96     @property
97     def is_comment(self) -> bool:
98         """Is this line a standalone comment?"""
99         return len(self.leaves) == 1 and self.leaves[0].type == STANDALONE_COMMENT
100
101     @property
102     def is_decorator(self) -> bool:
103         """Is this line a decorator?"""
104         return bool(self) and self.leaves[0].type == token.AT
105
106     @property
107     def is_import(self) -> bool:
108         """Is this an import line?"""
109         return bool(self) and is_import(self.leaves[0])
110
111     @property
112     def is_class(self) -> bool:
113         """Is this line a class definition?"""
114         return (
115             bool(self)
116             and self.leaves[0].type == token.NAME
117             and self.leaves[0].value == "class"
118         )
119
120     @property
121     def is_stub_class(self) -> bool:
122         """Is this line a class definition with a body consisting only of "..."?"""
123         return self.is_class and self.leaves[-3:] == [
124             Leaf(token.DOT, ".") for _ in range(3)
125         ]
126
127     @property
128     def is_def(self) -> bool:
129         """Is this a function definition? (Also returns True for async defs.)"""
130         try:
131             first_leaf = self.leaves[0]
132         except IndexError:
133             return False
134
135         try:
136             second_leaf: Optional[Leaf] = self.leaves[1]
137         except IndexError:
138             second_leaf = None
139         return (first_leaf.type == token.NAME and first_leaf.value == "def") or (
140             first_leaf.type == token.ASYNC
141             and second_leaf is not None
142             and second_leaf.type == token.NAME
143             and second_leaf.value == "def"
144         )
145
146     @property
147     def is_class_paren_empty(self) -> bool:
148         """Is this a class with no base classes but using parentheses?
149
150         Those are unnecessary and should be removed.
151         """
152         return (
153             bool(self)
154             and len(self.leaves) == 4
155             and self.is_class
156             and self.leaves[2].type == token.LPAR
157             and self.leaves[2].value == "("
158             and self.leaves[3].type == token.RPAR
159             and self.leaves[3].value == ")"
160         )
161
162     @property
163     def is_triple_quoted_string(self) -> bool:
164         """Is the line a triple quoted string?"""
165         return (
166             bool(self)
167             and self.leaves[0].type == token.STRING
168             and self.leaves[0].value.startswith(('"""', "'''"))
169         )
170
171     def contains_standalone_comments(self, depth_limit: int = sys.maxsize) -> bool:
172         """If so, needs to be split before emitting."""
173         for leaf in self.leaves:
174             if leaf.type == STANDALONE_COMMENT and leaf.bracket_depth <= depth_limit:
175                 return True
176
177         return False
178
179     def contains_uncollapsable_type_comments(self) -> bool:
180         ignored_ids = set()
181         try:
182             last_leaf = self.leaves[-1]
183             ignored_ids.add(id(last_leaf))
184             if last_leaf.type == token.COMMA or (
185                 last_leaf.type == token.RPAR and not last_leaf.value
186             ):
187                 # When trailing commas or optional parens are inserted by Black for
188                 # consistency, comments after the previous last element are not moved
189                 # (they don't have to, rendering will still be correct).  So we ignore
190                 # trailing commas and invisible.
191                 last_leaf = self.leaves[-2]
192                 ignored_ids.add(id(last_leaf))
193         except IndexError:
194             return False
195
196         # A type comment is uncollapsable if it is attached to a leaf
197         # that isn't at the end of the line (since that could cause it
198         # to get associated to a different argument) or if there are
199         # comments before it (since that could cause it to get hidden
200         # behind a comment.
201         comment_seen = False
202         for leaf_id, comments in self.comments.items():
203             for comment in comments:
204                 if is_type_comment(comment):
205                     if comment_seen or (
206                         not is_type_comment(comment, " ignore")
207                         and leaf_id not in ignored_ids
208                     ):
209                         return True
210
211                 comment_seen = True
212
213         return False
214
215     def contains_unsplittable_type_ignore(self) -> bool:
216         if not self.leaves:
217             return False
218
219         # If a 'type: ignore' is attached to the end of a line, we
220         # can't split the line, because we can't know which of the
221         # subexpressions the ignore was meant to apply to.
222         #
223         # We only want this to apply to actual physical lines from the
224         # original source, though: we don't want the presence of a
225         # 'type: ignore' at the end of a multiline expression to
226         # justify pushing it all onto one line. Thus we
227         # (unfortunately) need to check the actual source lines and
228         # only report an unsplittable 'type: ignore' if this line was
229         # one line in the original code.
230
231         # Grab the first and last line numbers, skipping generated leaves
232         first_line = next((leaf.lineno for leaf in self.leaves if leaf.lineno != 0), 0)
233         last_line = next(
234             (leaf.lineno for leaf in reversed(self.leaves) if leaf.lineno != 0), 0
235         )
236
237         if first_line == last_line:
238             # We look at the last two leaves since a comma or an
239             # invisible paren could have been added at the end of the
240             # line.
241             for node in self.leaves[-2:]:
242                 for comment in self.comments.get(id(node), []):
243                     if is_type_comment(comment, " ignore"):
244                         return True
245
246         return False
247
248     def contains_multiline_strings(self) -> bool:
249         return any(is_multiline_string(leaf) for leaf in self.leaves)
250
251     def has_magic_trailing_comma(
252         self, closing: Leaf, ensure_removable: bool = False
253     ) -> bool:
254         """Return True if we have a magic trailing comma, that is when:
255         - there's a trailing comma here
256         - it's not a one-tuple
257         - it's not a single-element subscript
258         Additionally, if ensure_removable:
259         - it's not from square bracket indexing
260         """
261         if not (
262             closing.type in CLOSING_BRACKETS
263             and self.leaves
264             and self.leaves[-1].type == token.COMMA
265         ):
266             return False
267
268         if closing.type == token.RBRACE:
269             return True
270
271         if closing.type == token.RSQB:
272             if (
273                 Preview.one_element_subscript in self.mode
274                 and closing.parent
275                 and closing.parent.type == syms.trailer
276                 and closing.opening_bracket
277                 and is_one_sequence_between(
278                     closing.opening_bracket,
279                     closing,
280                     self.leaves,
281                     brackets=(token.LSQB, token.RSQB),
282                 )
283             ):
284                 return False
285
286             if not ensure_removable:
287                 return True
288             comma = self.leaves[-1]
289             return bool(comma.parent and comma.parent.type == syms.listmaker)
290
291         if self.is_import:
292             return True
293
294         if closing.opening_bracket is not None and not is_one_sequence_between(
295             closing.opening_bracket, closing, self.leaves
296         ):
297             return True
298
299         return False
300
301     def append_comment(self, comment: Leaf) -> bool:
302         """Add an inline or standalone comment to the line."""
303         if (
304             comment.type == STANDALONE_COMMENT
305             and self.bracket_tracker.any_open_brackets()
306         ):
307             comment.prefix = ""
308             return False
309
310         if comment.type != token.COMMENT:
311             return False
312
313         if not self.leaves:
314             comment.type = STANDALONE_COMMENT
315             comment.prefix = ""
316             return False
317
318         last_leaf = self.leaves[-1]
319         if (
320             last_leaf.type == token.RPAR
321             and not last_leaf.value
322             and last_leaf.parent
323             and len(list(last_leaf.parent.leaves())) <= 3
324             and not is_type_comment(comment)
325         ):
326             # Comments on an optional parens wrapping a single leaf should belong to
327             # the wrapped node except if it's a type comment. Pinning the comment like
328             # this avoids unstable formatting caused by comment migration.
329             if len(self.leaves) < 2:
330                 comment.type = STANDALONE_COMMENT
331                 comment.prefix = ""
332                 return False
333
334             last_leaf = self.leaves[-2]
335         self.comments.setdefault(id(last_leaf), []).append(comment)
336         return True
337
338     def comments_after(self, leaf: Leaf) -> List[Leaf]:
339         """Generate comments that should appear directly after `leaf`."""
340         return self.comments.get(id(leaf), [])
341
342     def remove_trailing_comma(self) -> None:
343         """Remove the trailing comma and moves the comments attached to it."""
344         trailing_comma = self.leaves.pop()
345         trailing_comma_comments = self.comments.pop(id(trailing_comma), [])
346         self.comments.setdefault(id(self.leaves[-1]), []).extend(
347             trailing_comma_comments
348         )
349
350     def is_complex_subscript(self, leaf: Leaf) -> bool:
351         """Return True iff `leaf` is part of a slice with non-trivial exprs."""
352         open_lsqb = self.bracket_tracker.get_open_lsqb()
353         if open_lsqb is None:
354             return False
355
356         subscript_start = open_lsqb.next_sibling
357
358         if isinstance(subscript_start, Node):
359             if subscript_start.type == syms.listmaker:
360                 return False
361
362             if subscript_start.type == syms.subscriptlist:
363                 subscript_start = child_towards(subscript_start, leaf)
364         return subscript_start is not None and any(
365             n.type in TEST_DESCENDANTS for n in subscript_start.pre_order()
366         )
367
368     def enumerate_with_length(
369         self, reversed: bool = False
370     ) -> Iterator[Tuple[Index, Leaf, int]]:
371         """Return an enumeration of leaves with their length.
372
373         Stops prematurely on multiline strings and standalone comments.
374         """
375         op = cast(
376             Callable[[Sequence[Leaf]], Iterator[Tuple[Index, Leaf]]],
377             enumerate_reversed if reversed else enumerate,
378         )
379         for index, leaf in op(self.leaves):
380             length = len(leaf.prefix) + len(leaf.value)
381             if "\n" in leaf.value:
382                 return  # Multiline strings, we can't continue.
383
384             for comment in self.comments_after(leaf):
385                 length += len(comment.value)
386
387             yield index, leaf, length
388
389     def clone(self) -> "Line":
390         return Line(
391             mode=self.mode,
392             depth=self.depth,
393             inside_brackets=self.inside_brackets,
394             should_split_rhs=self.should_split_rhs,
395             magic_trailing_comma=self.magic_trailing_comma,
396         )
397
398     def __str__(self) -> str:
399         """Render the line."""
400         if not self:
401             return "\n"
402
403         indent = "    " * self.depth
404         leaves = iter(self.leaves)
405         first = next(leaves)
406         res = f"{first.prefix}{indent}{first.value}"
407         for leaf in leaves:
408             res += str(leaf)
409         for comment in itertools.chain.from_iterable(self.comments.values()):
410             res += str(comment)
411
412         return res + "\n"
413
414     def __bool__(self) -> bool:
415         """Return True if the line has leaves or comments."""
416         return bool(self.leaves or self.comments)
417
418
419 @dataclass
420 class EmptyLineTracker:
421     """Provides a stateful method that returns the number of potential extra
422     empty lines needed before and after the currently processed line.
423
424     Note: this tracker works on lines that haven't been split yet.  It assumes
425     the prefix of the first leaf consists of optional newlines.  Those newlines
426     are consumed by `maybe_empty_lines()` and included in the computation.
427     """
428
429     is_pyi: bool = False
430     previous_line: Optional[Line] = None
431     previous_after: int = 0
432     previous_defs: List[int] = field(default_factory=list)
433
434     def maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
435         """Return the number of extra empty lines before and after the `current_line`.
436
437         This is for separating `def`, `async def` and `class` with extra empty
438         lines (two on module-level).
439         """
440         before, after = self._maybe_empty_lines(current_line)
441         before = (
442             # Black should not insert empty lines at the beginning
443             # of the file
444             0
445             if self.previous_line is None
446             else before - self.previous_after
447         )
448         self.previous_after = after
449         self.previous_line = current_line
450         return before, after
451
452     def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
453         max_allowed = 1
454         if current_line.depth == 0:
455             max_allowed = 1 if self.is_pyi else 2
456         if current_line.leaves:
457             # Consume the first leaf's extra newlines.
458             first_leaf = current_line.leaves[0]
459             before = first_leaf.prefix.count("\n")
460             before = min(before, max_allowed)
461             first_leaf.prefix = ""
462         else:
463             before = 0
464         depth = current_line.depth
465         while self.previous_defs and self.previous_defs[-1] >= depth:
466             if self.is_pyi:
467                 assert self.previous_line is not None
468                 if depth and not current_line.is_def and self.previous_line.is_def:
469                     # Empty lines between attributes and methods should be preserved.
470                     before = min(1, before)
471                 elif depth:
472                     before = 0
473                 else:
474                     before = 1
475             else:
476                 if depth:
477                     before = 1
478                 elif (
479                     not depth
480                     and self.previous_defs[-1]
481                     and current_line.leaves[-1].type == token.COLON
482                     and (
483                         current_line.leaves[0].value
484                         not in ("with", "try", "for", "while", "if", "match")
485                     )
486                 ):
487                     # We shouldn't add two newlines between an indented function and
488                     # a dependent non-indented clause. This is to avoid issues with
489                     # conditional function definitions that are technically top-level
490                     # and therefore get two trailing newlines, but look weird and
491                     # inconsistent when they're followed by elif, else, etc. This is
492                     # worse because these functions only get *one* preceding newline
493                     # already.
494                     before = 1
495                 else:
496                     before = 2
497             self.previous_defs.pop()
498         if current_line.is_decorator or current_line.is_def or current_line.is_class:
499             return self._maybe_empty_lines_for_class_or_def(current_line, before)
500
501         if (
502             self.previous_line
503             and self.previous_line.is_import
504             and not current_line.is_import
505             and depth == self.previous_line.depth
506         ):
507             return (before or 1), 0
508
509         if (
510             self.previous_line
511             and self.previous_line.is_class
512             and current_line.is_triple_quoted_string
513         ):
514             return before, 1
515
516         return before, 0
517
518     def _maybe_empty_lines_for_class_or_def(
519         self, current_line: Line, before: int
520     ) -> Tuple[int, int]:
521         if not current_line.is_decorator:
522             self.previous_defs.append(current_line.depth)
523         if self.previous_line is None:
524             # Don't insert empty lines before the first line in the file.
525             return 0, 0
526
527         if self.previous_line.is_decorator:
528             if self.is_pyi and current_line.is_stub_class:
529                 # Insert an empty line after a decorated stub class
530                 return 0, 1
531
532             return 0, 0
533
534         if self.previous_line.depth < current_line.depth and (
535             self.previous_line.is_class or self.previous_line.is_def
536         ):
537             return 0, 0
538
539         if (
540             self.previous_line.is_comment
541             and self.previous_line.depth == current_line.depth
542             and before == 0
543         ):
544             return 0, 0
545
546         if self.is_pyi:
547             if current_line.is_class or self.previous_line.is_class:
548                 if self.previous_line.depth < current_line.depth:
549                     newlines = 0
550                 elif self.previous_line.depth > current_line.depth:
551                     newlines = 1
552                 elif current_line.is_stub_class and self.previous_line.is_stub_class:
553                     # No blank line between classes with an empty body
554                     newlines = 0
555                 else:
556                     newlines = 1
557             elif (
558                 current_line.is_def or current_line.is_decorator
559             ) and not self.previous_line.is_def:
560                 if current_line.depth:
561                     # In classes empty lines between attributes and methods should
562                     # be preserved.
563                     newlines = min(1, before)
564                 else:
565                     # Blank line between a block of functions (maybe with preceding
566                     # decorators) and a block of non-functions
567                     newlines = 1
568             elif self.previous_line.depth > current_line.depth:
569                 newlines = 1
570             else:
571                 newlines = 0
572         else:
573             newlines = 1 if current_line.depth else 2
574         return newlines, 0
575
576
577 def enumerate_reversed(sequence: Sequence[T]) -> Iterator[Tuple[Index, T]]:
578     """Like `reversed(enumerate(sequence))` if that were possible."""
579     index = len(sequence) - 1
580     for element in reversed(sequence):
581         yield (index, element)
582         index -= 1
583
584
585 def append_leaves(
586     new_line: Line, old_line: Line, leaves: List[Leaf], preformatted: bool = False
587 ) -> None:
588     """
589     Append leaves (taken from @old_line) to @new_line, making sure to fix the
590     underlying Node structure where appropriate.
591
592     All of the leaves in @leaves are duplicated. The duplicates are then
593     appended to @new_line and used to replace their originals in the underlying
594     Node structure. Any comments attached to the old leaves are reattached to
595     the new leaves.
596
597     Pre-conditions:
598         set(@leaves) is a subset of set(@old_line.leaves).
599     """
600     for old_leaf in leaves:
601         new_leaf = Leaf(old_leaf.type, old_leaf.value)
602         replace_child(old_leaf, new_leaf)
603         new_line.append(new_leaf, preformatted=preformatted)
604
605         for comment_leaf in old_line.comments_after(old_leaf):
606             new_line.append(comment_leaf, preformatted=True)
607
608
609 def is_line_short_enough(line: Line, *, line_length: int, line_str: str = "") -> bool:
610     """Return True if `line` is no longer than `line_length`.
611
612     Uses the provided `line_str` rendering, if any, otherwise computes a new one.
613     """
614     if not line_str:
615         line_str = line_to_string(line)
616     return (
617         len(line_str) <= line_length
618         and "\n" not in line_str  # multiline strings
619         and not line.contains_standalone_comments()
620     )
621
622
623 def can_be_split(line: Line) -> bool:
624     """Return False if the line cannot be split *for sure*.
625
626     This is not an exhaustive search but a cheap heuristic that we can use to
627     avoid some unfortunate formattings (mostly around wrapping unsplittable code
628     in unnecessary parentheses).
629     """
630     leaves = line.leaves
631     if len(leaves) < 2:
632         return False
633
634     if leaves[0].type == token.STRING and leaves[1].type == token.DOT:
635         call_count = 0
636         dot_count = 0
637         next = leaves[-1]
638         for leaf in leaves[-2::-1]:
639             if leaf.type in OPENING_BRACKETS:
640                 if next.type not in CLOSING_BRACKETS:
641                     return False
642
643                 call_count += 1
644             elif leaf.type == token.DOT:
645                 dot_count += 1
646             elif leaf.type == token.NAME:
647                 if not (next.type == token.DOT or next.type in OPENING_BRACKETS):
648                     return False
649
650             elif leaf.type not in CLOSING_BRACKETS:
651                 return False
652
653             if dot_count > 1 and call_count > 1:
654                 return False
655
656     return True
657
658
659 def can_omit_invisible_parens(
660     line: Line,
661     line_length: int,
662 ) -> bool:
663     """Does `line` have a shape safe to reformat without optional parens around it?
664
665     Returns True for only a subset of potentially nice looking formattings but
666     the point is to not return false positives that end up producing lines that
667     are too long.
668     """
669     bt = line.bracket_tracker
670     if not bt.delimiters:
671         # Without delimiters the optional parentheses are useless.
672         return True
673
674     max_priority = bt.max_delimiter_priority()
675     if bt.delimiter_count_with_priority(max_priority) > 1:
676         # With more than one delimiter of a kind the optional parentheses read better.
677         return False
678
679     if max_priority == DOT_PRIORITY:
680         # A single stranded method call doesn't require optional parentheses.
681         return True
682
683     assert len(line.leaves) >= 2, "Stranded delimiter"
684
685     # With a single delimiter, omit if the expression starts or ends with
686     # a bracket.
687     first = line.leaves[0]
688     second = line.leaves[1]
689     if first.type in OPENING_BRACKETS and second.type not in CLOSING_BRACKETS:
690         if _can_omit_opening_paren(line, first=first, line_length=line_length):
691             return True
692
693         # Note: we are not returning False here because a line might have *both*
694         # a leading opening bracket and a trailing closing bracket.  If the
695         # opening bracket doesn't match our rule, maybe the closing will.
696
697     penultimate = line.leaves[-2]
698     last = line.leaves[-1]
699
700     if (
701         last.type == token.RPAR
702         or last.type == token.RBRACE
703         or (
704             # don't use indexing for omitting optional parentheses;
705             # it looks weird
706             last.type == token.RSQB
707             and last.parent
708             and last.parent.type != syms.trailer
709         )
710     ):
711         if penultimate.type in OPENING_BRACKETS:
712             # Empty brackets don't help.
713             return False
714
715         if is_multiline_string(first):
716             # Additional wrapping of a multiline string in this situation is
717             # unnecessary.
718             return True
719
720         if _can_omit_closing_paren(line, last=last, line_length=line_length):
721             return True
722
723     return False
724
725
726 def _can_omit_opening_paren(line: Line, *, first: Leaf, line_length: int) -> bool:
727     """See `can_omit_invisible_parens`."""
728     remainder = False
729     length = 4 * line.depth
730     _index = -1
731     for _index, leaf, leaf_length in line.enumerate_with_length():
732         if leaf.type in CLOSING_BRACKETS and leaf.opening_bracket is first:
733             remainder = True
734         if remainder:
735             length += leaf_length
736             if length > line_length:
737                 break
738
739             if leaf.type in OPENING_BRACKETS:
740                 # There are brackets we can further split on.
741                 remainder = False
742
743     else:
744         # checked the entire string and line length wasn't exceeded
745         if len(line.leaves) == _index + 1:
746             return True
747
748     return False
749
750
751 def _can_omit_closing_paren(line: Line, *, last: Leaf, line_length: int) -> bool:
752     """See `can_omit_invisible_parens`."""
753     length = 4 * line.depth
754     seen_other_brackets = False
755     for _index, leaf, leaf_length in line.enumerate_with_length():
756         length += leaf_length
757         if leaf is last.opening_bracket:
758             if seen_other_brackets or length <= line_length:
759                 return True
760
761         elif leaf.type in OPENING_BRACKETS:
762             # There are brackets we can further split on.
763             seen_other_brackets = True
764
765     return False
766
767
768 def line_to_string(line: Line) -> str:
769     """Returns the string representation of @line.
770
771     WARNING: This is known to be computationally expensive.
772     """
773     return str(line).strip("\n")