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.
4 from dataclasses import dataclass, field
18 from black.brackets import COMMA_PRIORITY, DOT_PRIORITY, BracketTracker
19 from black.mode import Mode, Preview
20 from black.nodes import (
29 is_one_sequence_between,
31 is_type_ignore_comment,
32 is_with_or_async_with_stmt,
37 from black.strings import str_width
38 from blib2to3.pgen2 import token
39 from blib2to3.pytree import Leaf, Node
45 LN = Union[Leaf, Node]
50 """Holds leaves and comments. Can be printed with `str(line)`."""
52 mode: Mode = field(repr=False)
54 leaves: List[Leaf] = field(default_factory=list)
55 # keys ordered like `leaves`
56 comments: Dict[LeafID, List[Leaf]] = field(default_factory=dict)
57 bracket_tracker: BracketTracker = field(default_factory=BracketTracker)
58 inside_brackets: bool = False
59 should_split_rhs: bool = False
60 magic_trailing_comma: Optional[Leaf] = None
63 self, leaf: Leaf, preformatted: bool = False, track_bracket: bool = False
65 """Add a new `leaf` to the end of the line.
67 Unless `preformatted` is True, the `leaf` will receive a new consistent
68 whitespace prefix and metadata applied by :class:`BracketTracker`.
69 Trailing commas are maybe removed, unpacked for loop variables are
70 demoted from being delimiters.
72 Inline comments are put aside.
74 has_value = leaf.type in BRACKETS or bool(leaf.value.strip())
78 if token.COLON == leaf.type and self.is_class_paren_empty:
80 if self.leaves and not preformatted:
81 # Note: at this point leaf.prefix should be empty except for
82 # imports, for which we only preserve newlines.
83 leaf.prefix += whitespace(
84 leaf, complex_subscript=self.is_complex_subscript(leaf)
86 if self.inside_brackets or not preformatted or track_bracket:
87 self.bracket_tracker.mark(leaf)
88 if self.mode.magic_trailing_comma:
89 if self.has_magic_trailing_comma(leaf):
90 self.magic_trailing_comma = leaf
91 elif self.has_magic_trailing_comma(leaf, ensure_removable=True):
92 self.remove_trailing_comma()
93 if not self.append_comment(leaf):
94 self.leaves.append(leaf)
96 def append_safe(self, leaf: Leaf, preformatted: bool = False) -> None:
97 """Like :func:`append()` but disallow invalid standalone comment structure.
99 Raises ValueError when any `leaf` is appended after a standalone comment
100 or when a standalone comment is not the first leaf on the line.
102 if self.bracket_tracker.depth == 0:
104 raise ValueError("cannot append to standalone comments")
106 if self.leaves and leaf.type == STANDALONE_COMMENT:
108 "cannot append standalone comments to a populated line"
111 self.append(leaf, preformatted=preformatted)
114 def is_comment(self) -> bool:
115 """Is this line a standalone comment?"""
116 return len(self.leaves) == 1 and self.leaves[0].type == STANDALONE_COMMENT
119 def is_decorator(self) -> bool:
120 """Is this line a decorator?"""
121 return bool(self) and self.leaves[0].type == token.AT
124 def is_import(self) -> bool:
125 """Is this an import line?"""
126 return bool(self) and is_import(self.leaves[0])
129 def is_with_or_async_with_stmt(self) -> bool:
130 """Is this a with_stmt line?"""
131 return bool(self) and is_with_or_async_with_stmt(self.leaves[0])
134 def is_class(self) -> bool:
135 """Is this line a class definition?"""
138 and self.leaves[0].type == token.NAME
139 and self.leaves[0].value == "class"
143 def is_stub_class(self) -> bool:
144 """Is this line a class definition with a body consisting only of "..."?"""
145 return self.is_class and self.leaves[-3:] == [
146 Leaf(token.DOT, ".") for _ in range(3)
150 def is_def(self) -> bool:
151 """Is this a function definition? (Also returns True for async defs.)"""
153 first_leaf = self.leaves[0]
158 second_leaf: Optional[Leaf] = self.leaves[1]
161 return (first_leaf.type == token.NAME and first_leaf.value == "def") or (
162 first_leaf.type == token.ASYNC
163 and second_leaf is not None
164 and second_leaf.type == token.NAME
165 and second_leaf.value == "def"
169 def is_class_paren_empty(self) -> bool:
170 """Is this a class with no base classes but using parentheses?
172 Those are unnecessary and should be removed.
176 and len(self.leaves) == 4
178 and self.leaves[2].type == token.LPAR
179 and self.leaves[2].value == "("
180 and self.leaves[3].type == token.RPAR
181 and self.leaves[3].value == ")"
185 def is_triple_quoted_string(self) -> bool:
186 """Is the line a triple quoted string?"""
189 and self.leaves[0].type == token.STRING
190 and self.leaves[0].value.startswith(('"""', "'''"))
194 def opens_block(self) -> bool:
195 """Does this line open a new level of indentation."""
196 if len(self.leaves) == 0:
198 return self.leaves[-1].type == token.COLON
200 def is_fmt_pass_converted(
201 self, *, first_leaf_matches: Optional[Callable[[Leaf], bool]] = None
203 """Is this line converted from fmt off/skip code?
205 If first_leaf_matches is not None, it only returns True if the first
206 leaf of converted code matches.
208 if len(self.leaves) != 1:
210 leaf = self.leaves[0]
212 leaf.type != STANDALONE_COMMENT
213 or leaf.fmt_pass_converted_first_leaf is None
216 return first_leaf_matches is None or first_leaf_matches(
217 leaf.fmt_pass_converted_first_leaf
220 def contains_standalone_comments(self, depth_limit: int = sys.maxsize) -> bool:
221 """If so, needs to be split before emitting."""
222 for leaf in self.leaves:
223 if leaf.type == STANDALONE_COMMENT and leaf.bracket_depth <= depth_limit:
228 def contains_uncollapsable_type_comments(self) -> bool:
231 last_leaf = self.leaves[-1]
232 ignored_ids.add(id(last_leaf))
233 if last_leaf.type == token.COMMA or (
234 last_leaf.type == token.RPAR and not last_leaf.value
236 # When trailing commas or optional parens are inserted by Black for
237 # consistency, comments after the previous last element are not moved
238 # (they don't have to, rendering will still be correct). So we ignore
239 # trailing commas and invisible.
240 last_leaf = self.leaves[-2]
241 ignored_ids.add(id(last_leaf))
245 # A type comment is uncollapsable if it is attached to a leaf
246 # that isn't at the end of the line (since that could cause it
247 # to get associated to a different argument) or if there are
248 # comments before it (since that could cause it to get hidden
251 for leaf_id, comments in self.comments.items():
252 for comment in comments:
253 if is_type_comment(comment):
255 not is_type_ignore_comment(comment)
256 and leaf_id not in ignored_ids
264 def contains_unsplittable_type_ignore(self) -> bool:
268 # If a 'type: ignore' is attached to the end of a line, we
269 # can't split the line, because we can't know which of the
270 # subexpressions the ignore was meant to apply to.
272 # We only want this to apply to actual physical lines from the
273 # original source, though: we don't want the presence of a
274 # 'type: ignore' at the end of a multiline expression to
275 # justify pushing it all onto one line. Thus we
276 # (unfortunately) need to check the actual source lines and
277 # only report an unsplittable 'type: ignore' if this line was
278 # one line in the original code.
280 # Grab the first and last line numbers, skipping generated leaves
281 first_line = next((leaf.lineno for leaf in self.leaves if leaf.lineno != 0), 0)
283 (leaf.lineno for leaf in reversed(self.leaves) if leaf.lineno != 0), 0
286 if first_line == last_line:
287 # We look at the last two leaves since a comma or an
288 # invisible paren could have been added at the end of the
290 for node in self.leaves[-2:]:
291 for comment in self.comments.get(id(node), []):
292 if is_type_ignore_comment(comment):
297 def contains_multiline_strings(self) -> bool:
298 return any(is_multiline_string(leaf) for leaf in self.leaves)
300 def has_magic_trailing_comma(
301 self, closing: Leaf, ensure_removable: bool = False
303 """Return True if we have a magic trailing comma, that is when:
304 - there's a trailing comma here
305 - it's not a one-tuple
306 - it's not a single-element subscript
307 Additionally, if ensure_removable:
308 - it's not from square bracket indexing
309 (specifically, single-element square bracket indexing)
312 closing.type in CLOSING_BRACKETS
314 and self.leaves[-1].type == token.COMMA
318 if closing.type == token.RBRACE:
321 if closing.type == token.RSQB:
324 and closing.parent.type == syms.trailer
325 and closing.opening_bracket
326 and is_one_sequence_between(
327 closing.opening_bracket,
330 brackets=(token.LSQB, token.RSQB),
335 if not ensure_removable:
338 comma = self.leaves[-1]
339 if comma.parent is None:
342 comma.parent.type != syms.subscriptlist
343 or closing.opening_bracket is None
344 or not is_one_sequence_between(
345 closing.opening_bracket,
348 brackets=(token.LSQB, token.RSQB),
355 if closing.opening_bracket is not None and not is_one_sequence_between(
356 closing.opening_bracket, closing, self.leaves
362 def append_comment(self, comment: Leaf) -> bool:
363 """Add an inline or standalone comment to the line."""
365 comment.type == STANDALONE_COMMENT
366 and self.bracket_tracker.any_open_brackets()
371 if comment.type != token.COMMENT:
375 comment.type = STANDALONE_COMMENT
379 last_leaf = self.leaves[-1]
381 last_leaf.type == token.RPAR
382 and not last_leaf.value
384 and len(list(last_leaf.parent.leaves())) <= 3
385 and not is_type_comment(comment)
387 # Comments on an optional parens wrapping a single leaf should belong to
388 # the wrapped node except if it's a type comment. Pinning the comment like
389 # this avoids unstable formatting caused by comment migration.
390 if len(self.leaves) < 2:
391 comment.type = STANDALONE_COMMENT
395 last_leaf = self.leaves[-2]
396 self.comments.setdefault(id(last_leaf), []).append(comment)
399 def comments_after(self, leaf: Leaf) -> List[Leaf]:
400 """Generate comments that should appear directly after `leaf`."""
401 return self.comments.get(id(leaf), [])
403 def remove_trailing_comma(self) -> None:
404 """Remove the trailing comma and moves the comments attached to it."""
405 trailing_comma = self.leaves.pop()
406 trailing_comma_comments = self.comments.pop(id(trailing_comma), [])
407 self.comments.setdefault(id(self.leaves[-1]), []).extend(
408 trailing_comma_comments
411 def is_complex_subscript(self, leaf: Leaf) -> bool:
412 """Return True iff `leaf` is part of a slice with non-trivial exprs."""
413 open_lsqb = self.bracket_tracker.get_open_lsqb()
414 if open_lsqb is None:
417 subscript_start = open_lsqb.next_sibling
419 if isinstance(subscript_start, Node):
420 if subscript_start.type == syms.listmaker:
423 if subscript_start.type == syms.subscriptlist:
424 subscript_start = child_towards(subscript_start, leaf)
425 return subscript_start is not None and any(
426 n.type in TEST_DESCENDANTS for n in subscript_start.pre_order()
429 def enumerate_with_length(
430 self, reversed: bool = False
431 ) -> Iterator[Tuple[Index, Leaf, int]]:
432 """Return an enumeration of leaves with their length.
434 Stops prematurely on multiline strings and standalone comments.
437 Callable[[Sequence[Leaf]], Iterator[Tuple[Index, Leaf]]],
438 enumerate_reversed if reversed else enumerate,
440 for index, leaf in op(self.leaves):
441 length = len(leaf.prefix) + len(leaf.value)
442 if "\n" in leaf.value:
443 return # Multiline strings, we can't continue.
445 for comment in self.comments_after(leaf):
446 length += len(comment.value)
448 yield index, leaf, length
450 def clone(self) -> "Line":
454 inside_brackets=self.inside_brackets,
455 should_split_rhs=self.should_split_rhs,
456 magic_trailing_comma=self.magic_trailing_comma,
459 def __str__(self) -> str:
460 """Render the line."""
464 indent = " " * self.depth
465 leaves = iter(self.leaves)
467 res = f"{first.prefix}{indent}{first.value}"
470 for comment in itertools.chain.from_iterable(self.comments.values()):
475 def __bool__(self) -> bool:
476 """Return True if the line has leaves or comments."""
477 return bool(self.leaves or self.comments)
482 """Intermediate split result from a right hand split."""
487 opening_bracket: Leaf
488 closing_bracket: Leaf
493 """Class that holds information about a block of formatted lines.
495 This is introduced so that the EmptyLineTracker can look behind the standalone
496 comments and adjust their empty lines for class or def lines.
500 previous_block: Optional["LinesBlock"]
503 content_lines: List[str] = field(default_factory=list)
506 def all_lines(self) -> List[str]:
507 empty_line = str(Line(mode=self.mode))
509 [empty_line * self.before] + self.content_lines + [empty_line * self.after]
514 class EmptyLineTracker:
515 """Provides a stateful method that returns the number of potential extra
516 empty lines needed before and after the currently processed line.
518 Note: this tracker works on lines that haven't been split yet. It assumes
519 the prefix of the first leaf consists of optional newlines. Those newlines
520 are consumed by `maybe_empty_lines()` and included in the computation.
524 previous_line: Optional[Line] = None
525 previous_block: Optional[LinesBlock] = None
526 previous_defs: List[Line] = field(default_factory=list)
527 semantic_leading_comment: Optional[LinesBlock] = None
529 def maybe_empty_lines(self, current_line: Line) -> LinesBlock:
530 """Return the number of extra empty lines before and after the `current_line`.
532 This is for separating `def`, `async def` and `class` with extra empty
533 lines (two on module-level).
535 before, after = self._maybe_empty_lines(current_line)
536 previous_after = self.previous_block.after if self.previous_block else 0
538 # Black should not insert empty lines at the beginning
541 if self.previous_line is None
542 else before - previous_after
546 previous_block=self.previous_block,
547 original_line=current_line,
552 # Maintain the semantic_leading_comment state.
553 if current_line.is_comment:
554 if self.previous_line is None or (
555 not self.previous_line.is_decorator
556 # `or before` means this comment already has an empty line before
557 and (not self.previous_line.is_comment or before)
558 and (self.semantic_leading_comment is None or before)
560 self.semantic_leading_comment = block
561 # `or before` means this decorator already has an empty line before
562 elif not current_line.is_decorator or before:
563 self.semantic_leading_comment = None
565 self.previous_line = current_line
566 self.previous_block = block
569 def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
571 if current_line.depth == 0:
572 max_allowed = 1 if self.mode.is_pyi else 2
573 if current_line.leaves:
574 # Consume the first leaf's extra newlines.
575 first_leaf = current_line.leaves[0]
576 before = first_leaf.prefix.count("\n")
577 before = min(before, max_allowed)
578 first_leaf.prefix = ""
581 depth = current_line.depth
584 while self.previous_defs and self.previous_defs[-1].depth >= depth:
585 previous_def = self.previous_defs.pop()
587 if previous_def is not None:
588 assert self.previous_line is not None
590 if depth and not current_line.is_def and self.previous_line.is_def:
591 # Empty lines between attributes and methods should be preserved.
592 before = min(1, before)
594 Preview.blank_line_after_nested_stub_class in self.mode
595 and previous_def.is_class
596 and not previous_def.is_stub_class
608 and previous_def.depth
609 and current_line.leaves[-1].type == token.COLON
611 current_line.leaves[0].value
612 not in ("with", "try", "for", "while", "if", "match")
615 # We shouldn't add two newlines between an indented function and
616 # a dependent non-indented clause. This is to avoid issues with
617 # conditional function definitions that are technically top-level
618 # and therefore get two trailing newlines, but look weird and
619 # inconsistent when they're followed by elif, else, etc. This is
620 # worse because these functions only get *one* preceding newline
626 if current_line.is_decorator or current_line.is_def or current_line.is_class:
627 return self._maybe_empty_lines_for_class_or_def(current_line, before)
631 and self.previous_line.is_import
632 and not current_line.is_import
633 and not current_line.is_fmt_pass_converted(first_leaf_matches=is_import)
634 and depth == self.previous_line.depth
636 return (before or 1), 0
640 and self.previous_line.is_class
641 and current_line.is_triple_quoted_string
643 if Preview.no_blank_line_before_class_docstring in current_line.mode:
647 if self.previous_line and self.previous_line.opens_block:
651 def _maybe_empty_lines_for_class_or_def(
652 self, current_line: Line, before: int
653 ) -> Tuple[int, int]:
654 if not current_line.is_decorator:
655 self.previous_defs.append(current_line)
656 if self.previous_line is None:
657 # Don't insert empty lines before the first line in the file.
660 if self.previous_line.is_decorator:
661 if self.mode.is_pyi and current_line.is_stub_class:
662 # Insert an empty line after a decorated stub class
667 if self.previous_line.depth < current_line.depth and (
668 self.previous_line.is_class or self.previous_line.is_def
672 comment_to_add_newlines: Optional[LinesBlock] = None
674 self.previous_line.is_comment
675 and self.previous_line.depth == current_line.depth
678 slc = self.semantic_leading_comment
681 and slc.previous_block is not None
682 and not slc.previous_block.original_line.is_class
683 and not slc.previous_block.original_line.opens_block
686 comment_to_add_newlines = slc
691 if current_line.is_class or self.previous_line.is_class:
692 if self.previous_line.depth < current_line.depth:
694 elif self.previous_line.depth > current_line.depth:
696 elif current_line.is_stub_class and self.previous_line.is_stub_class:
697 # No blank line between classes with an empty body
702 current_line.is_def or current_line.is_decorator
703 ) and not self.previous_line.is_def:
704 if current_line.depth:
705 # In classes empty lines between attributes and methods should
707 newlines = min(1, before)
709 # Blank line between a block of functions (maybe with preceding
710 # decorators) and a block of non-functions
712 elif self.previous_line.depth > current_line.depth:
717 newlines = 1 if current_line.depth else 2
718 if comment_to_add_newlines is not None:
719 previous_block = comment_to_add_newlines.previous_block
720 if previous_block is not None:
721 comment_to_add_newlines.before = (
722 max(comment_to_add_newlines.before, newlines) - previous_block.after
728 def enumerate_reversed(sequence: Sequence[T]) -> Iterator[Tuple[Index, T]]:
729 """Like `reversed(enumerate(sequence))` if that were possible."""
730 index = len(sequence) - 1
731 for element in reversed(sequence):
732 yield (index, element)
737 new_line: Line, old_line: Line, leaves: List[Leaf], preformatted: bool = False
740 Append leaves (taken from @old_line) to @new_line, making sure to fix the
741 underlying Node structure where appropriate.
743 All of the leaves in @leaves are duplicated. The duplicates are then
744 appended to @new_line and used to replace their originals in the underlying
745 Node structure. Any comments attached to the old leaves are reattached to
749 set(@leaves) is a subset of set(@old_line.leaves).
751 for old_leaf in leaves:
752 new_leaf = Leaf(old_leaf.type, old_leaf.value)
753 replace_child(old_leaf, new_leaf)
754 new_line.append(new_leaf, preformatted=preformatted)
756 for comment_leaf in old_line.comments_after(old_leaf):
757 new_line.append(comment_leaf, preformatted=True)
760 def is_line_short_enough( # noqa: C901
761 line: Line, *, mode: Mode, line_str: str = ""
763 """For non-multiline strings, return True if `line` is no longer than `line_length`.
764 For multiline strings, looks at the context around `line` to determine
765 if it should be inlined or split up.
766 Uses the provided `line_str` rendering, if any, otherwise computes a new one.
769 line_str = line_to_string(line)
771 width = str_width if mode.preview else len
773 if Preview.multiline_string_handling not in mode:
775 width(line_str) <= mode.line_length
776 and "\n" not in line_str # multiline strings
777 and not line.contains_standalone_comments()
780 if line.contains_standalone_comments():
782 if "\n" not in line_str:
783 # No multiline strings (MLS) present
784 return width(line_str) <= mode.line_length
786 first, *_, last = line_str.split("\n")
787 if width(first) > mode.line_length or width(last) > mode.line_length:
790 # Traverse the AST to examine the context of the multiline string (MLS),
791 # tracking aspects such as depth and comma existence,
792 # to determine whether to split the MLS or keep it together.
793 # Depth (which is based on the existing bracket_depth concept)
794 # is needed to determine nesting level of the MLS.
795 # Includes special case for trailing commas.
796 commas: List[int] = [] # tracks number of commas per depth level
797 multiline_string: Optional[Leaf] = None
798 # store the leaves that contain parts of the MLS
799 multiline_string_contexts: List[LN] = []
801 max_level_to_update: Union[int, float] = math.inf # track the depth of the MLS
802 for i, leaf in enumerate(line.leaves):
803 if max_level_to_update == math.inf:
804 had_comma: Optional[int] = None
805 if leaf.bracket_depth + 1 > len(commas):
807 elif leaf.bracket_depth + 1 < len(commas):
808 had_comma = commas.pop()
810 had_comma is not None
811 and multiline_string is not None
812 and multiline_string.bracket_depth == leaf.bracket_depth + 1
814 # Have left the level with the MLS, stop tracking commas
815 max_level_to_update = leaf.bracket_depth
817 # MLS was in parens with at least one comma - force split
820 if leaf.bracket_depth <= max_level_to_update and leaf.type == token.COMMA:
821 # Ignore non-nested trailing comma
822 # directly after MLS/MLS-containing expression
823 ignore_ctxs: List[Optional[LN]] = [None]
824 ignore_ctxs += multiline_string_contexts
825 if not (leaf.prev_sibling in ignore_ctxs and i == len(line.leaves) - 1):
826 commas[leaf.bracket_depth] += 1
827 if max_level_to_update != math.inf:
828 max_level_to_update = min(max_level_to_update, leaf.bracket_depth)
830 if is_multiline_string(leaf):
831 if len(multiline_string_contexts) > 0:
832 # >1 multiline string cannot fit on a single line - force split
834 multiline_string = leaf
836 # fetch the leaf components of the MLS in the AST
837 while str(ctx) in line_str:
838 multiline_string_contexts.append(ctx)
839 if ctx.parent is None:
843 # May not have a triple-quoted multiline string at all,
844 # in case of a regular string with embedded newlines and line continuations
845 if len(multiline_string_contexts) == 0:
848 return all(val == 0 for val in commas)
851 def can_be_split(line: Line) -> bool:
852 """Return False if the line cannot be split *for sure*.
854 This is not an exhaustive search but a cheap heuristic that we can use to
855 avoid some unfortunate formattings (mostly around wrapping unsplittable code
856 in unnecessary parentheses).
862 if leaves[0].type == token.STRING and leaves[1].type == token.DOT:
866 for leaf in leaves[-2::-1]:
867 if leaf.type in OPENING_BRACKETS:
868 if next.type not in CLOSING_BRACKETS:
872 elif leaf.type == token.DOT:
874 elif leaf.type == token.NAME:
875 if not (next.type == token.DOT or next.type in OPENING_BRACKETS):
878 elif leaf.type not in CLOSING_BRACKETS:
881 if dot_count > 1 and call_count > 1:
887 def can_omit_invisible_parens(
891 """Does `rhs.body` have a shape safe to reformat without optional parens around it?
893 Returns True for only a subset of potentially nice looking formattings but
894 the point is to not return false positives that end up producing lines that
898 bt = line.bracket_tracker
899 if not bt.delimiters:
900 # Without delimiters the optional parentheses are useless.
903 max_priority = bt.max_delimiter_priority()
904 delimiter_count = bt.delimiter_count_with_priority(max_priority)
905 if delimiter_count > 1:
906 # With more than one delimiter of a kind the optional parentheses read better.
909 if delimiter_count == 1:
911 Preview.wrap_multiple_context_managers_in_parens in line.mode
912 and max_priority == COMMA_PRIORITY
913 and rhs.head.is_with_or_async_with_stmt
915 # For two context manager with statements, the optional parentheses read
916 # better. In this case, `rhs.body` is the context managers part of
917 # the with statement. `rhs.head` is the `with (` part on the previous
920 # Otherwise it may also read better, but we don't do it today and requires
921 # careful considerations for all possible cases. See
922 # https://github.com/psf/black/issues/2156.
924 if max_priority == DOT_PRIORITY:
925 # A single stranded method call doesn't require optional parentheses.
928 assert len(line.leaves) >= 2, "Stranded delimiter"
930 # With a single delimiter, omit if the expression starts or ends with
932 first = line.leaves[0]
933 second = line.leaves[1]
934 if first.type in OPENING_BRACKETS and second.type not in CLOSING_BRACKETS:
935 if _can_omit_opening_paren(line, first=first, line_length=line_length):
938 # Note: we are not returning False here because a line might have *both*
939 # a leading opening bracket and a trailing closing bracket. If the
940 # opening bracket doesn't match our rule, maybe the closing will.
942 penultimate = line.leaves[-2]
943 last = line.leaves[-1]
946 last.type == token.RPAR
947 or last.type == token.RBRACE
949 # don't use indexing for omitting optional parentheses;
951 last.type == token.RSQB
953 and last.parent.type != syms.trailer
956 if penultimate.type in OPENING_BRACKETS:
957 # Empty brackets don't help.
960 if is_multiline_string(first):
961 # Additional wrapping of a multiline string in this situation is
965 if _can_omit_closing_paren(line, last=last, line_length=line_length):
971 def _can_omit_opening_paren(line: Line, *, first: Leaf, line_length: int) -> bool:
972 """See `can_omit_invisible_parens`."""
974 length = 4 * line.depth
976 for _index, leaf, leaf_length in line.enumerate_with_length():
977 if leaf.type in CLOSING_BRACKETS and leaf.opening_bracket is first:
980 length += leaf_length
981 if length > line_length:
984 if leaf.type in OPENING_BRACKETS:
985 # There are brackets we can further split on.
989 # checked the entire string and line length wasn't exceeded
990 if len(line.leaves) == _index + 1:
996 def _can_omit_closing_paren(line: Line, *, last: Leaf, line_length: int) -> bool:
997 """See `can_omit_invisible_parens`."""
998 length = 4 * line.depth
999 seen_other_brackets = False
1000 for _index, leaf, leaf_length in line.enumerate_with_length():
1001 length += leaf_length
1002 if leaf is last.opening_bracket:
1003 if seen_other_brackets or length <= line_length:
1006 elif leaf.type in OPENING_BRACKETS:
1007 # There are brackets we can further split on.
1008 seen_other_brackets = True
1013 def line_to_string(line: Line) -> str:
1014 """Returns the string representation of @line.
1016 WARNING: This is known to be computationally expensive.
1018 return str(line).strip("\n")