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.
1 from dataclasses import dataclass, field
16 from blib2to3.pytree import Node, Leaf
17 from blib2to3.pgen2 import token
19 from black.brackets import BracketTracker, DOT_PRIORITY
20 from black.mode import Mode
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_tuple_between
35 """Holds leaves and comments. Can be printed with `str(line)`."""
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
47 def append(self, leaf: Leaf, preformatted: bool = False) -> None:
48 """Add a new `leaf` to the end of the line.
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.
55 Inline comments are put aside.
57 has_value = leaf.type in BRACKETS or bool(leaf.value.strip())
61 if token.COLON == leaf.type and self.is_class_paren_empty:
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)
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)
79 def append_safe(self, leaf: Leaf, preformatted: bool = False) -> None:
80 """Like :func:`append()` but disallow invalid standalone comment structure.
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.
85 if self.bracket_tracker.depth == 0:
87 raise ValueError("cannot append to standalone comments")
89 if self.leaves and leaf.type == STANDALONE_COMMENT:
91 "cannot append standalone comments to a populated line"
94 self.append(leaf, preformatted=preformatted)
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
102 def is_decorator(self) -> bool:
103 """Is this line a decorator?"""
104 return bool(self) and self.leaves[0].type == token.AT
107 def is_import(self) -> bool:
108 """Is this an import line?"""
109 return bool(self) and is_import(self.leaves[0])
112 def is_class(self) -> bool:
113 """Is this line a class definition?"""
116 and self.leaves[0].type == token.NAME
117 and self.leaves[0].value == "class"
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)
128 def is_def(self) -> bool:
129 """Is this a function definition? (Also returns True for async defs.)"""
131 first_leaf = self.leaves[0]
136 second_leaf: Optional[Leaf] = self.leaves[1]
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"
147 def is_class_paren_empty(self) -> bool:
148 """Is this a class with no base classes but using parentheses?
150 Those are unnecessary and should be removed.
154 and len(self.leaves) == 4
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 == ")"
163 def is_triple_quoted_string(self) -> bool:
164 """Is the line a triple quoted string?"""
167 and self.leaves[0].type == token.STRING
168 and self.leaves[0].value.startswith(('"""', "'''"))
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:
179 def contains_uncollapsable_type_comments(self) -> bool:
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
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))
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
202 for leaf_id, comments in self.comments.items():
203 for comment in comments:
204 if is_type_comment(comment):
206 not is_type_comment(comment, " ignore")
207 and leaf_id not in ignored_ids
215 def contains_unsplittable_type_ignore(self) -> bool:
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.
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.
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)
234 (leaf.lineno for leaf in reversed(self.leaves) if leaf.lineno != 0), 0
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
241 for node in self.leaves[-2:]:
242 for comment in self.comments.get(id(node), []):
243 if is_type_comment(comment, " ignore"):
248 def contains_multiline_strings(self) -> bool:
249 return any(is_multiline_string(leaf) for leaf in self.leaves)
251 def has_magic_trailing_comma(
252 self, closing: Leaf, ensure_removable: bool = False
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 Additionally, if ensure_removable:
258 - it's not from square bracket indexing
261 closing.type in CLOSING_BRACKETS
263 and self.leaves[-1].type == token.COMMA
267 if closing.type == token.RBRACE:
270 if closing.type == token.RSQB:
271 if not ensure_removable:
273 comma = self.leaves[-1]
274 return bool(comma.parent and comma.parent.type == syms.listmaker)
279 if closing.opening_bracket is not None and not is_one_tuple_between(
280 closing.opening_bracket, closing, self.leaves
286 def append_comment(self, comment: Leaf) -> bool:
287 """Add an inline or standalone comment to the line."""
289 comment.type == STANDALONE_COMMENT
290 and self.bracket_tracker.any_open_brackets()
295 if comment.type != token.COMMENT:
299 comment.type = STANDALONE_COMMENT
303 last_leaf = self.leaves[-1]
305 last_leaf.type == token.RPAR
306 and not last_leaf.value
308 and len(list(last_leaf.parent.leaves())) <= 3
309 and not is_type_comment(comment)
311 # Comments on an optional parens wrapping a single leaf should belong to
312 # the wrapped node except if it's a type comment. Pinning the comment like
313 # this avoids unstable formatting caused by comment migration.
314 if len(self.leaves) < 2:
315 comment.type = STANDALONE_COMMENT
319 last_leaf = self.leaves[-2]
320 self.comments.setdefault(id(last_leaf), []).append(comment)
323 def comments_after(self, leaf: Leaf) -> List[Leaf]:
324 """Generate comments that should appear directly after `leaf`."""
325 return self.comments.get(id(leaf), [])
327 def remove_trailing_comma(self) -> None:
328 """Remove the trailing comma and moves the comments attached to it."""
329 trailing_comma = self.leaves.pop()
330 trailing_comma_comments = self.comments.pop(id(trailing_comma), [])
331 self.comments.setdefault(id(self.leaves[-1]), []).extend(
332 trailing_comma_comments
335 def is_complex_subscript(self, leaf: Leaf) -> bool:
336 """Return True iff `leaf` is part of a slice with non-trivial exprs."""
337 open_lsqb = self.bracket_tracker.get_open_lsqb()
338 if open_lsqb is None:
341 subscript_start = open_lsqb.next_sibling
343 if isinstance(subscript_start, Node):
344 if subscript_start.type == syms.listmaker:
347 if subscript_start.type == syms.subscriptlist:
348 subscript_start = child_towards(subscript_start, leaf)
349 return subscript_start is not None and any(
350 n.type in TEST_DESCENDANTS for n in subscript_start.pre_order()
353 def enumerate_with_length(
354 self, reversed: bool = False
355 ) -> Iterator[Tuple[Index, Leaf, int]]:
356 """Return an enumeration of leaves with their length.
358 Stops prematurely on multiline strings and standalone comments.
361 Callable[[Sequence[Leaf]], Iterator[Tuple[Index, Leaf]]],
362 enumerate_reversed if reversed else enumerate,
364 for index, leaf in op(self.leaves):
365 length = len(leaf.prefix) + len(leaf.value)
366 if "\n" in leaf.value:
367 return # Multiline strings, we can't continue.
369 for comment in self.comments_after(leaf):
370 length += len(comment.value)
372 yield index, leaf, length
374 def clone(self) -> "Line":
378 inside_brackets=self.inside_brackets,
379 should_split_rhs=self.should_split_rhs,
380 magic_trailing_comma=self.magic_trailing_comma,
383 def __str__(self) -> str:
384 """Render the line."""
388 indent = " " * self.depth
389 leaves = iter(self.leaves)
391 res = f"{first.prefix}{indent}{first.value}"
394 for comment in itertools.chain.from_iterable(self.comments.values()):
399 def __bool__(self) -> bool:
400 """Return True if the line has leaves or comments."""
401 return bool(self.leaves or self.comments)
405 class EmptyLineTracker:
406 """Provides a stateful method that returns the number of potential extra
407 empty lines needed before and after the currently processed line.
409 Note: this tracker works on lines that haven't been split yet. It assumes
410 the prefix of the first leaf consists of optional newlines. Those newlines
411 are consumed by `maybe_empty_lines()` and included in the computation.
415 previous_line: Optional[Line] = None
416 previous_after: int = 0
417 previous_defs: List[int] = field(default_factory=list)
419 def maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
420 """Return the number of extra empty lines before and after the `current_line`.
422 This is for separating `def`, `async def` and `class` with extra empty
423 lines (two on module-level).
425 before, after = self._maybe_empty_lines(current_line)
427 # Black should not insert empty lines at the beginning
430 if self.previous_line is None
431 else before - self.previous_after
433 self.previous_after = after
434 self.previous_line = current_line
437 def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
439 if current_line.depth == 0:
440 max_allowed = 1 if self.is_pyi else 2
441 if current_line.leaves:
442 # Consume the first leaf's extra newlines.
443 first_leaf = current_line.leaves[0]
444 before = first_leaf.prefix.count("\n")
445 before = min(before, max_allowed)
446 first_leaf.prefix = ""
449 depth = current_line.depth
450 while self.previous_defs and self.previous_defs[-1] >= depth:
452 assert self.previous_line is not None
453 if depth and not current_line.is_def and self.previous_line.is_def:
454 # Empty lines between attributes and methods should be preserved.
455 before = min(1, before)
465 and self.previous_defs[-1]
466 and current_line.leaves[-1].type == token.COLON
468 current_line.leaves[0].value
469 not in ("with", "try", "for", "while", "if", "match")
472 # We shouldn't add two newlines between an indented function and
473 # a dependent non-indented clause. This is to avoid issues with
474 # conditional function definitions that are technically top-level
475 # and therefore get two trailing newlines, but look weird and
476 # inconsistent when they're followed by elif, else, etc. This is
477 # worse because these functions only get *one* preceding newline
482 self.previous_defs.pop()
483 if current_line.is_decorator or current_line.is_def or current_line.is_class:
484 return self._maybe_empty_lines_for_class_or_def(current_line, before)
488 and self.previous_line.is_import
489 and not current_line.is_import
490 and depth == self.previous_line.depth
492 return (before or 1), 0
496 and self.previous_line.is_class
497 and current_line.is_triple_quoted_string
503 def _maybe_empty_lines_for_class_or_def(
504 self, current_line: Line, before: int
505 ) -> Tuple[int, int]:
506 if not current_line.is_decorator:
507 self.previous_defs.append(current_line.depth)
508 if self.previous_line is None:
509 # Don't insert empty lines before the first line in the file.
512 if self.previous_line.is_decorator:
513 if self.is_pyi and current_line.is_stub_class:
514 # Insert an empty line after a decorated stub class
519 if self.previous_line.depth < current_line.depth and (
520 self.previous_line.is_class or self.previous_line.is_def
525 self.previous_line.is_comment
526 and self.previous_line.depth == current_line.depth
532 if current_line.is_class or self.previous_line.is_class:
533 if self.previous_line.depth < current_line.depth:
535 elif self.previous_line.depth > current_line.depth:
537 elif current_line.is_stub_class and self.previous_line.is_stub_class:
538 # No blank line between classes with an empty body
543 current_line.is_def or current_line.is_decorator
544 ) and not self.previous_line.is_def:
545 if current_line.depth:
546 # In classes empty lines between attributes and methods should
548 newlines = min(1, before)
550 # Blank line between a block of functions (maybe with preceding
551 # decorators) and a block of non-functions
553 elif self.previous_line.depth > current_line.depth:
558 newlines = 1 if current_line.depth else 2
562 def enumerate_reversed(sequence: Sequence[T]) -> Iterator[Tuple[Index, T]]:
563 """Like `reversed(enumerate(sequence))` if that were possible."""
564 index = len(sequence) - 1
565 for element in reversed(sequence):
566 yield (index, element)
571 new_line: Line, old_line: Line, leaves: List[Leaf], preformatted: bool = False
574 Append leaves (taken from @old_line) to @new_line, making sure to fix the
575 underlying Node structure where appropriate.
577 All of the leaves in @leaves are duplicated. The duplicates are then
578 appended to @new_line and used to replace their originals in the underlying
579 Node structure. Any comments attached to the old leaves are reattached to
583 set(@leaves) is a subset of set(@old_line.leaves).
585 for old_leaf in leaves:
586 new_leaf = Leaf(old_leaf.type, old_leaf.value)
587 replace_child(old_leaf, new_leaf)
588 new_line.append(new_leaf, preformatted=preformatted)
590 for comment_leaf in old_line.comments_after(old_leaf):
591 new_line.append(comment_leaf, preformatted=True)
594 def is_line_short_enough(line: Line, *, line_length: int, line_str: str = "") -> bool:
595 """Return True if `line` is no longer than `line_length`.
597 Uses the provided `line_str` rendering, if any, otherwise computes a new one.
600 line_str = line_to_string(line)
602 len(line_str) <= line_length
603 and "\n" not in line_str # multiline strings
604 and not line.contains_standalone_comments()
608 def can_be_split(line: Line) -> bool:
609 """Return False if the line cannot be split *for sure*.
611 This is not an exhaustive search but a cheap heuristic that we can use to
612 avoid some unfortunate formattings (mostly around wrapping unsplittable code
613 in unnecessary parentheses).
619 if leaves[0].type == token.STRING and leaves[1].type == token.DOT:
623 for leaf in leaves[-2::-1]:
624 if leaf.type in OPENING_BRACKETS:
625 if next.type not in CLOSING_BRACKETS:
629 elif leaf.type == token.DOT:
631 elif leaf.type == token.NAME:
632 if not (next.type == token.DOT or next.type in OPENING_BRACKETS):
635 elif leaf.type not in CLOSING_BRACKETS:
638 if dot_count > 1 and call_count > 1:
644 def can_omit_invisible_parens(
648 """Does `line` have a shape safe to reformat without optional parens around it?
650 Returns True for only a subset of potentially nice looking formattings but
651 the point is to not return false positives that end up producing lines that
654 bt = line.bracket_tracker
655 if not bt.delimiters:
656 # Without delimiters the optional parentheses are useless.
659 max_priority = bt.max_delimiter_priority()
660 if bt.delimiter_count_with_priority(max_priority) > 1:
661 # With more than one delimiter of a kind the optional parentheses read better.
664 if max_priority == DOT_PRIORITY:
665 # A single stranded method call doesn't require optional parentheses.
668 assert len(line.leaves) >= 2, "Stranded delimiter"
670 # With a single delimiter, omit if the expression starts or ends with
672 first = line.leaves[0]
673 second = line.leaves[1]
674 if first.type in OPENING_BRACKETS and second.type not in CLOSING_BRACKETS:
675 if _can_omit_opening_paren(line, first=first, line_length=line_length):
678 # Note: we are not returning False here because a line might have *both*
679 # a leading opening bracket and a trailing closing bracket. If the
680 # opening bracket doesn't match our rule, maybe the closing will.
682 penultimate = line.leaves[-2]
683 last = line.leaves[-1]
686 last.type == token.RPAR
687 or last.type == token.RBRACE
689 # don't use indexing for omitting optional parentheses;
691 last.type == token.RSQB
693 and last.parent.type != syms.trailer
696 if penultimate.type in OPENING_BRACKETS:
697 # Empty brackets don't help.
700 if is_multiline_string(first):
701 # Additional wrapping of a multiline string in this situation is
705 if _can_omit_closing_paren(line, last=last, line_length=line_length):
711 def _can_omit_opening_paren(line: Line, *, first: Leaf, line_length: int) -> bool:
712 """See `can_omit_invisible_parens`."""
714 length = 4 * line.depth
716 for _index, leaf, leaf_length in line.enumerate_with_length():
717 if leaf.type in CLOSING_BRACKETS and leaf.opening_bracket is first:
720 length += leaf_length
721 if length > line_length:
724 if leaf.type in OPENING_BRACKETS:
725 # There are brackets we can further split on.
729 # checked the entire string and line length wasn't exceeded
730 if len(line.leaves) == _index + 1:
736 def _can_omit_closing_paren(line: Line, *, last: Leaf, line_length: int) -> bool:
737 """See `can_omit_invisible_parens`."""
738 length = 4 * line.depth
739 seen_other_brackets = False
740 for _index, leaf, leaf_length in line.enumerate_with_length():
741 length += leaf_length
742 if leaf is last.opening_bracket:
743 if seen_other_brackets or length <= line_length:
746 elif leaf.type in OPENING_BRACKETS:
747 # There are brackets we can further split on.
748 seen_other_brackets = True
753 def line_to_string(line: Line) -> str:
754 """Returns the string representation of @line.
756 WARNING: This is known to be computationally expensive.
758 return str(line).strip("\n")