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.
3 from dataclasses import dataclass, field
16 from black.brackets import DOT_PRIORITY, BracketTracker
17 from black.mode import Mode
18 from black.nodes import (
27 is_one_sequence_between,
33 from blib2to3.pgen2 import token
34 from blib2to3.pytree import Leaf, Node
44 """Holds leaves and comments. Can be printed with `str(line)`."""
48 leaves: List[Leaf] = field(default_factory=list)
49 # keys ordered like `leaves`
50 comments: Dict[LeafID, List[Leaf]] = field(default_factory=dict)
51 bracket_tracker: BracketTracker = field(default_factory=BracketTracker)
52 inside_brackets: bool = False
53 should_split_rhs: bool = False
54 magic_trailing_comma: Optional[Leaf] = None
57 self, leaf: Leaf, preformatted: bool = False, track_bracket: bool = False
59 """Add a new `leaf` to the end of the line.
61 Unless `preformatted` is True, the `leaf` will receive a new consistent
62 whitespace prefix and metadata applied by :class:`BracketTracker`.
63 Trailing commas are maybe removed, unpacked for loop variables are
64 demoted from being delimiters.
66 Inline comments are put aside.
68 has_value = leaf.type in BRACKETS or bool(leaf.value.strip())
72 if token.COLON == leaf.type and self.is_class_paren_empty:
74 if self.leaves and not preformatted:
75 # Note: at this point leaf.prefix should be empty except for
76 # imports, for which we only preserve newlines.
77 leaf.prefix += whitespace(
78 leaf, complex_subscript=self.is_complex_subscript(leaf)
80 if self.inside_brackets or not preformatted or track_bracket:
81 self.bracket_tracker.mark(leaf)
82 if self.mode.magic_trailing_comma:
83 if self.has_magic_trailing_comma(leaf):
84 self.magic_trailing_comma = leaf
85 elif self.has_magic_trailing_comma(leaf, ensure_removable=True):
86 self.remove_trailing_comma()
87 if not self.append_comment(leaf):
88 self.leaves.append(leaf)
90 def append_safe(self, leaf: Leaf, preformatted: bool = False) -> None:
91 """Like :func:`append()` but disallow invalid standalone comment structure.
93 Raises ValueError when any `leaf` is appended after a standalone comment
94 or when a standalone comment is not the first leaf on the line.
96 if self.bracket_tracker.depth == 0:
98 raise ValueError("cannot append to standalone comments")
100 if self.leaves and leaf.type == STANDALONE_COMMENT:
102 "cannot append standalone comments to a populated line"
105 self.append(leaf, preformatted=preformatted)
108 def is_comment(self) -> bool:
109 """Is this line a standalone comment?"""
110 return len(self.leaves) == 1 and self.leaves[0].type == STANDALONE_COMMENT
113 def is_decorator(self) -> bool:
114 """Is this line a decorator?"""
115 return bool(self) and self.leaves[0].type == token.AT
118 def is_import(self) -> bool:
119 """Is this an import line?"""
120 return bool(self) and is_import(self.leaves[0])
123 def is_class(self) -> bool:
124 """Is this line a class definition?"""
127 and self.leaves[0].type == token.NAME
128 and self.leaves[0].value == "class"
132 def is_stub_class(self) -> bool:
133 """Is this line a class definition with a body consisting only of "..."?"""
134 return self.is_class and self.leaves[-3:] == [
135 Leaf(token.DOT, ".") for _ in range(3)
139 def is_def(self) -> bool:
140 """Is this a function definition? (Also returns True for async defs.)"""
142 first_leaf = self.leaves[0]
147 second_leaf: Optional[Leaf] = self.leaves[1]
150 return (first_leaf.type == token.NAME and first_leaf.value == "def") or (
151 first_leaf.type == token.ASYNC
152 and second_leaf is not None
153 and second_leaf.type == token.NAME
154 and second_leaf.value == "def"
158 def is_class_paren_empty(self) -> bool:
159 """Is this a class with no base classes but using parentheses?
161 Those are unnecessary and should be removed.
165 and len(self.leaves) == 4
167 and self.leaves[2].type == token.LPAR
168 and self.leaves[2].value == "("
169 and self.leaves[3].type == token.RPAR
170 and self.leaves[3].value == ")"
174 def is_triple_quoted_string(self) -> bool:
175 """Is the line a triple quoted string?"""
178 and self.leaves[0].type == token.STRING
179 and self.leaves[0].value.startswith(('"""', "'''"))
183 def opens_block(self) -> bool:
184 """Does this line open a new level of indentation."""
185 if len(self.leaves) == 0:
187 return self.leaves[-1].type == token.COLON
189 def contains_standalone_comments(self, depth_limit: int = sys.maxsize) -> bool:
190 """If so, needs to be split before emitting."""
191 for leaf in self.leaves:
192 if leaf.type == STANDALONE_COMMENT and leaf.bracket_depth <= depth_limit:
197 def contains_uncollapsable_type_comments(self) -> bool:
200 last_leaf = self.leaves[-1]
201 ignored_ids.add(id(last_leaf))
202 if last_leaf.type == token.COMMA or (
203 last_leaf.type == token.RPAR and not last_leaf.value
205 # When trailing commas or optional parens are inserted by Black for
206 # consistency, comments after the previous last element are not moved
207 # (they don't have to, rendering will still be correct). So we ignore
208 # trailing commas and invisible.
209 last_leaf = self.leaves[-2]
210 ignored_ids.add(id(last_leaf))
214 # A type comment is uncollapsable if it is attached to a leaf
215 # that isn't at the end of the line (since that could cause it
216 # to get associated to a different argument) or if there are
217 # comments before it (since that could cause it to get hidden
220 for leaf_id, comments in self.comments.items():
221 for comment in comments:
222 if is_type_comment(comment):
224 not is_type_comment(comment, " ignore")
225 and leaf_id not in ignored_ids
233 def contains_unsplittable_type_ignore(self) -> bool:
237 # If a 'type: ignore' is attached to the end of a line, we
238 # can't split the line, because we can't know which of the
239 # subexpressions the ignore was meant to apply to.
241 # We only want this to apply to actual physical lines from the
242 # original source, though: we don't want the presence of a
243 # 'type: ignore' at the end of a multiline expression to
244 # justify pushing it all onto one line. Thus we
245 # (unfortunately) need to check the actual source lines and
246 # only report an unsplittable 'type: ignore' if this line was
247 # one line in the original code.
249 # Grab the first and last line numbers, skipping generated leaves
250 first_line = next((leaf.lineno for leaf in self.leaves if leaf.lineno != 0), 0)
252 (leaf.lineno for leaf in reversed(self.leaves) if leaf.lineno != 0), 0
255 if first_line == last_line:
256 # We look at the last two leaves since a comma or an
257 # invisible paren could have been added at the end of the
259 for node in self.leaves[-2:]:
260 for comment in self.comments.get(id(node), []):
261 if is_type_comment(comment, " ignore"):
266 def contains_multiline_strings(self) -> bool:
267 return any(is_multiline_string(leaf) for leaf in self.leaves)
269 def has_magic_trailing_comma(
270 self, closing: Leaf, ensure_removable: bool = False
272 """Return True if we have a magic trailing comma, that is when:
273 - there's a trailing comma here
274 - it's not a one-tuple
275 - it's not a single-element subscript
276 Additionally, if ensure_removable:
277 - it's not from square bracket indexing
278 (specifically, single-element square bracket indexing)
281 closing.type in CLOSING_BRACKETS
283 and self.leaves[-1].type == token.COMMA
287 if closing.type == token.RBRACE:
290 if closing.type == token.RSQB:
293 and closing.parent.type == syms.trailer
294 and closing.opening_bracket
295 and is_one_sequence_between(
296 closing.opening_bracket,
299 brackets=(token.LSQB, token.RSQB),
304 if not ensure_removable:
307 comma = self.leaves[-1]
308 if comma.parent is None:
311 comma.parent.type != syms.subscriptlist
312 or closing.opening_bracket is None
313 or not is_one_sequence_between(
314 closing.opening_bracket,
317 brackets=(token.LSQB, token.RSQB),
324 if closing.opening_bracket is not None and not is_one_sequence_between(
325 closing.opening_bracket, closing, self.leaves
331 def append_comment(self, comment: Leaf) -> bool:
332 """Add an inline or standalone comment to the line."""
334 comment.type == STANDALONE_COMMENT
335 and self.bracket_tracker.any_open_brackets()
340 if comment.type != token.COMMENT:
344 comment.type = STANDALONE_COMMENT
348 last_leaf = self.leaves[-1]
350 last_leaf.type == token.RPAR
351 and not last_leaf.value
353 and len(list(last_leaf.parent.leaves())) <= 3
354 and not is_type_comment(comment)
356 # Comments on an optional parens wrapping a single leaf should belong to
357 # the wrapped node except if it's a type comment. Pinning the comment like
358 # this avoids unstable formatting caused by comment migration.
359 if len(self.leaves) < 2:
360 comment.type = STANDALONE_COMMENT
364 last_leaf = self.leaves[-2]
365 self.comments.setdefault(id(last_leaf), []).append(comment)
368 def comments_after(self, leaf: Leaf) -> List[Leaf]:
369 """Generate comments that should appear directly after `leaf`."""
370 return self.comments.get(id(leaf), [])
372 def remove_trailing_comma(self) -> None:
373 """Remove the trailing comma and moves the comments attached to it."""
374 trailing_comma = self.leaves.pop()
375 trailing_comma_comments = self.comments.pop(id(trailing_comma), [])
376 self.comments.setdefault(id(self.leaves[-1]), []).extend(
377 trailing_comma_comments
380 def is_complex_subscript(self, leaf: Leaf) -> bool:
381 """Return True iff `leaf` is part of a slice with non-trivial exprs."""
382 open_lsqb = self.bracket_tracker.get_open_lsqb()
383 if open_lsqb is None:
386 subscript_start = open_lsqb.next_sibling
388 if isinstance(subscript_start, Node):
389 if subscript_start.type == syms.listmaker:
392 if subscript_start.type == syms.subscriptlist:
393 subscript_start = child_towards(subscript_start, leaf)
394 return subscript_start is not None and any(
395 n.type in TEST_DESCENDANTS for n in subscript_start.pre_order()
398 def enumerate_with_length(
399 self, reversed: bool = False
400 ) -> Iterator[Tuple[Index, Leaf, int]]:
401 """Return an enumeration of leaves with their length.
403 Stops prematurely on multiline strings and standalone comments.
406 Callable[[Sequence[Leaf]], Iterator[Tuple[Index, Leaf]]],
407 enumerate_reversed if reversed else enumerate,
409 for index, leaf in op(self.leaves):
410 length = len(leaf.prefix) + len(leaf.value)
411 if "\n" in leaf.value:
412 return # Multiline strings, we can't continue.
414 for comment in self.comments_after(leaf):
415 length += len(comment.value)
417 yield index, leaf, length
419 def clone(self) -> "Line":
423 inside_brackets=self.inside_brackets,
424 should_split_rhs=self.should_split_rhs,
425 magic_trailing_comma=self.magic_trailing_comma,
428 def __str__(self) -> str:
429 """Render the line."""
433 indent = " " * self.depth
434 leaves = iter(self.leaves)
436 res = f"{first.prefix}{indent}{first.value}"
439 for comment in itertools.chain.from_iterable(self.comments.values()):
444 def __bool__(self) -> bool:
445 """Return True if the line has leaves or comments."""
446 return bool(self.leaves or self.comments)
451 """Class that holds information about a block of formatted lines.
453 This is introduced so that the EmptyLineTracker can look behind the standalone
454 comments and adjust their empty lines for class or def lines.
458 previous_block: Optional["LinesBlock"]
461 content_lines: List[str] = field(default_factory=list)
464 def all_lines(self) -> List[str]:
465 empty_line = str(Line(mode=self.mode))
467 [empty_line * self.before] + self.content_lines + [empty_line * self.after]
472 class EmptyLineTracker:
473 """Provides a stateful method that returns the number of potential extra
474 empty lines needed before and after the currently processed line.
476 Note: this tracker works on lines that haven't been split yet. It assumes
477 the prefix of the first leaf consists of optional newlines. Those newlines
478 are consumed by `maybe_empty_lines()` and included in the computation.
482 previous_line: Optional[Line] = None
483 previous_block: Optional[LinesBlock] = None
484 previous_defs: List[int] = field(default_factory=list)
485 semantic_leading_comment: Optional[LinesBlock] = None
487 def maybe_empty_lines(self, current_line: Line) -> LinesBlock:
488 """Return the number of extra empty lines before and after the `current_line`.
490 This is for separating `def`, `async def` and `class` with extra empty
491 lines (two on module-level).
493 before, after = self._maybe_empty_lines(current_line)
494 previous_after = self.previous_block.after if self.previous_block else 0
496 # Black should not insert empty lines at the beginning
499 if self.previous_line is None
500 else before - previous_after
504 previous_block=self.previous_block,
505 original_line=current_line,
510 # Maintain the semantic_leading_comment state.
511 if current_line.is_comment:
512 if self.previous_line is None or (
513 not self.previous_line.is_decorator
514 # `or before` means this comment already has an empty line before
515 and (not self.previous_line.is_comment or before)
516 and (self.semantic_leading_comment is None or before)
518 self.semantic_leading_comment = block
519 # `or before` means this decorator already has an empty line before
520 elif not current_line.is_decorator or before:
521 self.semantic_leading_comment = None
523 self.previous_line = current_line
524 self.previous_block = block
527 def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
529 if current_line.depth == 0:
530 max_allowed = 1 if self.mode.is_pyi else 2
531 if current_line.leaves:
532 # Consume the first leaf's extra newlines.
533 first_leaf = current_line.leaves[0]
534 before = first_leaf.prefix.count("\n")
535 before = min(before, max_allowed)
536 first_leaf.prefix = ""
539 depth = current_line.depth
540 while self.previous_defs and self.previous_defs[-1] >= depth:
542 assert self.previous_line is not None
543 if depth and not current_line.is_def and self.previous_line.is_def:
544 # Empty lines between attributes and methods should be preserved.
545 before = min(1, before)
555 and self.previous_defs[-1]
556 and current_line.leaves[-1].type == token.COLON
558 current_line.leaves[0].value
559 not in ("with", "try", "for", "while", "if", "match")
562 # We shouldn't add two newlines between an indented function and
563 # a dependent non-indented clause. This is to avoid issues with
564 # conditional function definitions that are technically top-level
565 # and therefore get two trailing newlines, but look weird and
566 # inconsistent when they're followed by elif, else, etc. This is
567 # worse because these functions only get *one* preceding newline
572 self.previous_defs.pop()
573 if current_line.is_decorator or current_line.is_def or current_line.is_class:
574 return self._maybe_empty_lines_for_class_or_def(current_line, before)
578 and self.previous_line.is_import
579 and not current_line.is_import
580 and depth == self.previous_line.depth
582 return (before or 1), 0
586 and self.previous_line.is_class
587 and current_line.is_triple_quoted_string
591 if self.previous_line and self.previous_line.opens_block:
595 def _maybe_empty_lines_for_class_or_def(
596 self, current_line: Line, before: int
597 ) -> Tuple[int, int]:
598 if not current_line.is_decorator:
599 self.previous_defs.append(current_line.depth)
600 if self.previous_line is None:
601 # Don't insert empty lines before the first line in the file.
604 if self.previous_line.is_decorator:
605 if self.mode.is_pyi and current_line.is_stub_class:
606 # Insert an empty line after a decorated stub class
611 if self.previous_line.depth < current_line.depth and (
612 self.previous_line.is_class or self.previous_line.is_def
616 comment_to_add_newlines: Optional[LinesBlock] = None
618 self.previous_line.is_comment
619 and self.previous_line.depth == current_line.depth
622 slc = self.semantic_leading_comment
625 and slc.previous_block is not None
626 and not slc.previous_block.original_line.is_class
627 and not slc.previous_block.original_line.opens_block
630 comment_to_add_newlines = slc
635 if current_line.is_class or self.previous_line.is_class:
636 if self.previous_line.depth < current_line.depth:
638 elif self.previous_line.depth > current_line.depth:
640 elif current_line.is_stub_class and self.previous_line.is_stub_class:
641 # No blank line between classes with an empty body
646 current_line.is_def or current_line.is_decorator
647 ) and not self.previous_line.is_def:
648 if current_line.depth:
649 # In classes empty lines between attributes and methods should
651 newlines = min(1, before)
653 # Blank line between a block of functions (maybe with preceding
654 # decorators) and a block of non-functions
656 elif self.previous_line.depth > current_line.depth:
661 newlines = 1 if current_line.depth else 2
662 if comment_to_add_newlines is not None:
663 previous_block = comment_to_add_newlines.previous_block
664 if previous_block is not None:
665 comment_to_add_newlines.before = (
666 max(comment_to_add_newlines.before, newlines) - previous_block.after
672 def enumerate_reversed(sequence: Sequence[T]) -> Iterator[Tuple[Index, T]]:
673 """Like `reversed(enumerate(sequence))` if that were possible."""
674 index = len(sequence) - 1
675 for element in reversed(sequence):
676 yield (index, element)
681 new_line: Line, old_line: Line, leaves: List[Leaf], preformatted: bool = False
684 Append leaves (taken from @old_line) to @new_line, making sure to fix the
685 underlying Node structure where appropriate.
687 All of the leaves in @leaves are duplicated. The duplicates are then
688 appended to @new_line and used to replace their originals in the underlying
689 Node structure. Any comments attached to the old leaves are reattached to
693 set(@leaves) is a subset of set(@old_line.leaves).
695 for old_leaf in leaves:
696 new_leaf = Leaf(old_leaf.type, old_leaf.value)
697 replace_child(old_leaf, new_leaf)
698 new_line.append(new_leaf, preformatted=preformatted)
700 for comment_leaf in old_line.comments_after(old_leaf):
701 new_line.append(comment_leaf, preformatted=True)
704 def is_line_short_enough(line: Line, *, line_length: int, line_str: str = "") -> bool:
705 """Return True if `line` is no longer than `line_length`.
707 Uses the provided `line_str` rendering, if any, otherwise computes a new one.
710 line_str = line_to_string(line)
712 len(line_str) <= line_length
713 and "\n" not in line_str # multiline strings
714 and not line.contains_standalone_comments()
718 def can_be_split(line: Line) -> bool:
719 """Return False if the line cannot be split *for sure*.
721 This is not an exhaustive search but a cheap heuristic that we can use to
722 avoid some unfortunate formattings (mostly around wrapping unsplittable code
723 in unnecessary parentheses).
729 if leaves[0].type == token.STRING and leaves[1].type == token.DOT:
733 for leaf in leaves[-2::-1]:
734 if leaf.type in OPENING_BRACKETS:
735 if next.type not in CLOSING_BRACKETS:
739 elif leaf.type == token.DOT:
741 elif leaf.type == token.NAME:
742 if not (next.type == token.DOT or next.type in OPENING_BRACKETS):
745 elif leaf.type not in CLOSING_BRACKETS:
748 if dot_count > 1 and call_count > 1:
754 def can_omit_invisible_parens(
758 """Does `line` have a shape safe to reformat without optional parens around it?
760 Returns True for only a subset of potentially nice looking formattings but
761 the point is to not return false positives that end up producing lines that
764 bt = line.bracket_tracker
765 if not bt.delimiters:
766 # Without delimiters the optional parentheses are useless.
769 max_priority = bt.max_delimiter_priority()
770 if bt.delimiter_count_with_priority(max_priority) > 1:
771 # With more than one delimiter of a kind the optional parentheses read better.
774 if max_priority == DOT_PRIORITY:
775 # A single stranded method call doesn't require optional parentheses.
778 assert len(line.leaves) >= 2, "Stranded delimiter"
780 # With a single delimiter, omit if the expression starts or ends with
782 first = line.leaves[0]
783 second = line.leaves[1]
784 if first.type in OPENING_BRACKETS and second.type not in CLOSING_BRACKETS:
785 if _can_omit_opening_paren(line, first=first, line_length=line_length):
788 # Note: we are not returning False here because a line might have *both*
789 # a leading opening bracket and a trailing closing bracket. If the
790 # opening bracket doesn't match our rule, maybe the closing will.
792 penultimate = line.leaves[-2]
793 last = line.leaves[-1]
796 last.type == token.RPAR
797 or last.type == token.RBRACE
799 # don't use indexing for omitting optional parentheses;
801 last.type == token.RSQB
803 and last.parent.type != syms.trailer
806 if penultimate.type in OPENING_BRACKETS:
807 # Empty brackets don't help.
810 if is_multiline_string(first):
811 # Additional wrapping of a multiline string in this situation is
815 if _can_omit_closing_paren(line, last=last, line_length=line_length):
821 def _can_omit_opening_paren(line: Line, *, first: Leaf, line_length: int) -> bool:
822 """See `can_omit_invisible_parens`."""
824 length = 4 * line.depth
826 for _index, leaf, leaf_length in line.enumerate_with_length():
827 if leaf.type in CLOSING_BRACKETS and leaf.opening_bracket is first:
830 length += leaf_length
831 if length > line_length:
834 if leaf.type in OPENING_BRACKETS:
835 # There are brackets we can further split on.
839 # checked the entire string and line length wasn't exceeded
840 if len(line.leaves) == _index + 1:
846 def _can_omit_closing_paren(line: Line, *, last: Leaf, line_length: int) -> bool:
847 """See `can_omit_invisible_parens`."""
848 length = 4 * line.depth
849 seen_other_brackets = False
850 for _index, leaf, leaf_length in line.enumerate_with_length():
851 length += leaf_length
852 if leaf is last.opening_bracket:
853 if seen_other_brackets or length <= line_length:
856 elif leaf.type in OPENING_BRACKETS:
857 # There are brackets we can further split on.
858 seen_other_brackets = True
863 def line_to_string(line: Line) -> str:
864 """Returns the string representation of @line.
866 WARNING: This is known to be computationally expensive.
868 return str(line).strip("\n")