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 asyncio.base_events import BaseEventLoop
5 from concurrent.futures import Executor, ProcessPoolExecutor
6 from functools import partial
9 from pathlib import Path
13 Dict, Generic, Iterable, Iterator, List, Optional, Set, Tuple, Type, TypeVar, Union
16 from attr import dataclass, Factory
20 from blib2to3.pytree import Node, Leaf, type_repr
21 from blib2to3 import pygram, pytree
22 from blib2to3.pgen2 import driver, token
23 from blib2to3.pgen2.parse import ParseError
25 __version__ = "18.3a4"
26 DEFAULT_LINE_LENGTH = 88
28 syms = pygram.python_symbols
35 LN = Union[Leaf, Node]
36 out = partial(click.secho, bold=True, err=True)
37 err = partial(click.secho, fg='red', err=True)
40 class NothingChanged(UserWarning):
41 """Raised by :func:`format_file` when reformatted code is the same as source."""
44 class CannotSplit(Exception):
45 """A readable split that fits the allotted line length is impossible.
47 Raised by :func:`left_hand_split`, :func:`right_hand_split`, and
48 :func:`delimiter_split`.
52 class FormatError(Exception):
53 """Base exception for `# fmt: on` and `# fmt: off` handling.
55 It holds the number of bytes of the prefix consumed before the format
56 control comment appeared.
59 def __init__(self, consumed: int) -> None:
60 super().__init__(consumed)
61 self.consumed = consumed
63 def trim_prefix(self, leaf: Leaf) -> None:
64 leaf.prefix = leaf.prefix[self.consumed:]
66 def leaf_from_consumed(self, leaf: Leaf) -> Leaf:
67 """Returns a new Leaf from the consumed part of the prefix."""
68 unformatted_prefix = leaf.prefix[:self.consumed]
69 return Leaf(token.NEWLINE, unformatted_prefix)
72 class FormatOn(FormatError):
73 """Found a comment like `# fmt: on` in the file."""
76 class FormatOff(FormatError):
77 """Found a comment like `# fmt: off` in the file."""
85 default=DEFAULT_LINE_LENGTH,
86 help='How many character per line to allow.',
93 "Don't write back the files, just return the status. Return code 0 "
94 "means nothing would change. Return code 1 means some files would be "
95 "reformatted. Return code 123 means there was an internal error."
101 help='If --fast given, skip temporary sanity checks. [default: --safe]',
103 @click.version_option(version=__version__)
108 exists=True, file_okay=True, dir_okay=True, readable=True, allow_dash=True
113 ctx: click.Context, line_length: int, check: bool, fast: bool, src: List[str]
115 """The uncompromising code formatter."""
116 sources: List[Path] = []
120 sources.extend(gen_python_files_in_dir(p))
122 # if a file was explicitly given, we don't care about its extension
125 sources.append(Path('-'))
127 err(f'invalid path: {s}')
128 if len(sources) == 0:
130 elif len(sources) == 1:
132 report = Report(check=check)
134 if not p.is_file() and str(p) == '-':
135 changed = format_stdin_to_stdout(
136 line_length=line_length, fast=fast, write_back=not check
139 changed = format_file_in_place(
140 p, line_length=line_length, fast=fast, write_back=not check
142 report.done(p, changed)
143 except Exception as exc:
144 report.failed(p, str(exc))
145 ctx.exit(report.return_code)
147 loop = asyncio.get_event_loop()
148 executor = ProcessPoolExecutor(max_workers=os.cpu_count())
151 return_code = loop.run_until_complete(
153 sources, line_length, not check, fast, loop, executor
158 ctx.exit(return_code)
161 async def schedule_formatting(
169 """Run formatting of `sources` in parallel using the provided `executor`.
171 (Use ProcessPoolExecutors for actual parallelism.)
173 `line_length`, `write_back`, and `fast` options are passed to
174 :func:`format_file_in_place`.
177 src: loop.run_in_executor(
178 executor, format_file_in_place, src, line_length, fast, write_back
182 await asyncio.wait(tasks.values())
184 report = Report(check=not write_back)
185 for src, task in tasks.items():
187 report.failed(src, 'timed out, cancelling')
189 cancelled.append(task)
190 elif task.exception():
191 report.failed(src, str(task.exception()))
193 report.done(src, task.result())
195 await asyncio.wait(cancelled, timeout=2)
196 out('All done! ✨ 🍰 ✨')
197 click.echo(str(report))
198 return report.return_code
201 def format_file_in_place(
202 src: Path, line_length: int, fast: bool, write_back: bool = False
204 """Format file under `src` path. Return True if changed.
206 If `write_back` is True, write reformatted code back to stdout.
207 `line_length` and `fast` options are passed to :func:`format_file_contents`.
209 with tokenize.open(src) as src_buffer:
210 src_contents = src_buffer.read()
212 contents = format_file_contents(
213 src_contents, line_length=line_length, fast=fast
215 except NothingChanged:
219 with open(src, "w", encoding=src_buffer.encoding) as f:
224 def format_stdin_to_stdout(
225 line_length: int, fast: bool, write_back: bool = False
227 """Format file on stdin. Return True if changed.
229 If `write_back` is True, write reformatted code back to stdout.
230 `line_length` and `fast` arguments are passed to :func:`format_file_contents`.
232 contents = sys.stdin.read()
234 contents = format_file_contents(contents, line_length=line_length, fast=fast)
237 except NothingChanged:
242 sys.stdout.write(contents)
245 def format_file_contents(
246 src_contents: str, line_length: int, fast: bool
248 """Reformat contents a file and return new contents.
250 If `fast` is False, additionally confirm that the reformatted code is
251 valid by calling :func:`assert_equivalent` and :func:`assert_stable` on it.
252 `line_length` is passed to :func:`format_str`.
254 if src_contents.strip() == '':
257 dst_contents = format_str(src_contents, line_length=line_length)
258 if src_contents == dst_contents:
262 assert_equivalent(src_contents, dst_contents)
263 assert_stable(src_contents, dst_contents, line_length=line_length)
267 def format_str(src_contents: str, line_length: int) -> FileContent:
268 """Reformat a string and return new contents.
270 `line_length` determines how many characters per line are allowed.
272 src_node = lib2to3_parse(src_contents)
274 lines = LineGenerator()
275 elt = EmptyLineTracker()
276 py36 = is_python36(src_node)
279 for current_line in lines.visit(src_node):
280 for _ in range(after):
281 dst_contents += str(empty_line)
282 before, after = elt.maybe_empty_lines(current_line)
283 for _ in range(before):
284 dst_contents += str(empty_line)
285 for line in split_line(current_line, line_length=line_length, py36=py36):
286 dst_contents += str(line)
291 pygram.python_grammar_no_print_statement_no_exec_statement,
292 pygram.python_grammar_no_print_statement,
293 pygram.python_grammar_no_exec_statement,
294 pygram.python_grammar,
298 def lib2to3_parse(src_txt: str) -> Node:
299 """Given a string with source, return the lib2to3 Node."""
300 grammar = pygram.python_grammar_no_print_statement
301 if src_txt[-1] != '\n':
302 nl = '\r\n' if '\r\n' in src_txt[:1024] else '\n'
304 for grammar in GRAMMARS:
305 drv = driver.Driver(grammar, pytree.convert)
307 result = drv.parse_string(src_txt, True)
310 except ParseError as pe:
311 lineno, column = pe.context[1]
312 lines = src_txt.splitlines()
314 faulty_line = lines[lineno - 1]
316 faulty_line = "<line number missing in source>"
317 exc = ValueError(f"Cannot parse: {lineno}:{column}: {faulty_line}")
321 if isinstance(result, Leaf):
322 result = Node(syms.file_input, [result])
326 def lib2to3_unparse(node: Node) -> str:
327 """Given a lib2to3 node, return its string representation."""
335 class Visitor(Generic[T]):
336 """Basic lib2to3 visitor that yields things of type `T` on `visit()`."""
338 def visit(self, node: LN) -> Iterator[T]:
339 """Main method to visit `node` and its children.
341 It tries to find a `visit_*()` method for the given `node.type`, like
342 `visit_simple_stmt` for Node objects or `visit_INDENT` for Leaf objects.
343 If no dedicated `visit_*()` method is found, chooses `visit_default()`
346 Then yields objects of type `T` from the selected visitor.
349 name = token.tok_name[node.type]
351 name = type_repr(node.type)
352 yield from getattr(self, f'visit_{name}', self.visit_default)(node)
354 def visit_default(self, node: LN) -> Iterator[T]:
355 """Default `visit_*()` implementation. Recurses to children of `node`."""
356 if isinstance(node, Node):
357 for child in node.children:
358 yield from self.visit(child)
362 class DebugVisitor(Visitor[T]):
365 def visit_default(self, node: LN) -> Iterator[T]:
366 indent = ' ' * (2 * self.tree_depth)
367 if isinstance(node, Node):
368 _type = type_repr(node.type)
369 out(f'{indent}{_type}', fg='yellow')
371 for child in node.children:
372 yield from self.visit(child)
375 out(f'{indent}/{_type}', fg='yellow', bold=False)
377 _type = token.tok_name.get(node.type, str(node.type))
378 out(f'{indent}{_type}', fg='blue', nl=False)
380 # We don't have to handle prefixes for `Node` objects since
381 # that delegates to the first child anyway.
382 out(f' {node.prefix!r}', fg='green', bold=False, nl=False)
383 out(f' {node.value!r}', fg='blue', bold=False)
386 def show(cls, code: str) -> None:
387 """Pretty-print the lib2to3 AST of a given string of `code`.
389 Convenience method for debugging.
391 v: DebugVisitor[None] = DebugVisitor()
392 list(v.visit(lib2to3_parse(code)))
395 KEYWORDS = set(keyword.kwlist)
396 WHITESPACE = {token.DEDENT, token.INDENT, token.NEWLINE}
397 FLOW_CONTROL = {'return', 'raise', 'break', 'continue'}
408 STANDALONE_COMMENT = 153
409 LOGIC_OPERATORS = {'and', 'or'}
433 COMPREHENSION_PRIORITY = 20
437 COMPARATOR_PRIORITY = 3
442 class BracketTracker:
443 """Keeps track of brackets on a line."""
446 bracket_match: Dict[Tuple[Depth, NodeType], Leaf] = Factory(dict)
447 delimiters: Dict[LeafID, Priority] = Factory(dict)
448 previous: Optional[Leaf] = None
450 def mark(self, leaf: Leaf) -> None:
451 """Mark `leaf` with bracket-related metadata. Keep track of delimiters.
453 All leaves receive an int `bracket_depth` field that stores how deep
454 within brackets a given leaf is. 0 means there are no enclosing brackets
455 that started on this line.
457 If a leaf is itself a closing bracket, it receives an `opening_bracket`
458 field that it forms a pair with. This is a one-directional link to
459 avoid reference cycles.
461 If a leaf is a delimiter (a token on which Black can split the line if
462 needed) and it's on depth 0, its `id()` is stored in the tracker's
465 if leaf.type == token.COMMENT:
468 if leaf.type in CLOSING_BRACKETS:
470 opening_bracket = self.bracket_match.pop((self.depth, leaf.type))
471 leaf.opening_bracket = opening_bracket
472 leaf.bracket_depth = self.depth
474 delim = is_delimiter(leaf)
476 self.delimiters[id(leaf)] = delim
477 elif self.previous is not None:
478 if leaf.type == token.STRING and self.previous.type == token.STRING:
479 self.delimiters[id(self.previous)] = STRING_PRIORITY
481 leaf.type == token.NAME
482 and leaf.value == 'for'
484 and leaf.parent.type in {syms.comp_for, syms.old_comp_for}
486 self.delimiters[id(self.previous)] = COMPREHENSION_PRIORITY
488 leaf.type == token.NAME
489 and leaf.value == 'if'
491 and leaf.parent.type in {syms.comp_if, syms.old_comp_if}
493 self.delimiters[id(self.previous)] = COMPREHENSION_PRIORITY
495 leaf.type == token.NAME
496 and leaf.value in LOGIC_OPERATORS
499 self.delimiters[id(self.previous)] = LOGIC_PRIORITY
500 if leaf.type in OPENING_BRACKETS:
501 self.bracket_match[self.depth, BRACKET[leaf.type]] = leaf
505 def any_open_brackets(self) -> bool:
506 """Return True if there is an yet unmatched open bracket on the line."""
507 return bool(self.bracket_match)
509 def max_delimiter_priority(self, exclude: Iterable[LeafID] = ()) -> int:
510 """Return the highest priority of a delimiter found on the line.
512 Values are consistent with what `is_delimiter()` returns.
514 return max(v for k, v in self.delimiters.items() if k not in exclude)
519 """Holds leaves and comments. Can be printed with `str(line)`."""
522 leaves: List[Leaf] = Factory(list)
523 comments: Dict[LeafID, Leaf] = Factory(dict)
524 bracket_tracker: BracketTracker = Factory(BracketTracker)
525 inside_brackets: bool = False
526 has_for: bool = False
527 _for_loop_variable: bool = False
529 def append(self, leaf: Leaf, preformatted: bool = False) -> None:
530 """Add a new `leaf` to the end of the line.
532 Unless `preformatted` is True, the `leaf` will receive a new consistent
533 whitespace prefix and metadata applied by :class:`BracketTracker`.
534 Trailing commas are maybe removed, unpacked for loop variables are
535 demoted from being delimiters.
537 Inline comments are put aside.
539 has_value = leaf.value.strip()
543 if self.leaves and not preformatted:
544 # Note: at this point leaf.prefix should be empty except for
545 # imports, for which we only preserve newlines.
546 leaf.prefix += whitespace(leaf)
547 if self.inside_brackets or not preformatted:
548 self.maybe_decrement_after_for_loop_variable(leaf)
549 self.bracket_tracker.mark(leaf)
550 self.maybe_remove_trailing_comma(leaf)
551 self.maybe_increment_for_loop_variable(leaf)
552 if self.maybe_adapt_standalone_comment(leaf):
555 if not self.append_comment(leaf):
556 self.leaves.append(leaf)
559 def is_comment(self) -> bool:
560 """Is this line a standalone comment?"""
561 return bool(self) and self.leaves[0].type == STANDALONE_COMMENT
564 def is_decorator(self) -> bool:
565 """Is this line a decorator?"""
566 return bool(self) and self.leaves[0].type == token.AT
569 def is_import(self) -> bool:
570 """Is this an import line?"""
571 return bool(self) and is_import(self.leaves[0])
574 def is_class(self) -> bool:
575 """Is this line a class definition?"""
578 and self.leaves[0].type == token.NAME
579 and self.leaves[0].value == 'class'
583 def is_def(self) -> bool:
584 """Is this a function definition? (Also returns True for async defs.)"""
586 first_leaf = self.leaves[0]
591 second_leaf: Optional[Leaf] = self.leaves[1]
595 (first_leaf.type == token.NAME and first_leaf.value == 'def')
597 first_leaf.type == token.ASYNC
598 and second_leaf is not None
599 and second_leaf.type == token.NAME
600 and second_leaf.value == 'def'
605 def is_flow_control(self) -> bool:
606 """Is this line a flow control statement?
608 Those are `return`, `raise`, `break`, and `continue`.
612 and self.leaves[0].type == token.NAME
613 and self.leaves[0].value in FLOW_CONTROL
617 def is_yield(self) -> bool:
618 """Is this line a yield statement?"""
621 and self.leaves[0].type == token.NAME
622 and self.leaves[0].value == 'yield'
625 def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
626 """Remove trailing comma if there is one and it's safe."""
629 and self.leaves[-1].type == token.COMMA
630 and closing.type in CLOSING_BRACKETS
634 if closing.type == token.RBRACE:
638 if closing.type == token.RSQB:
639 comma = self.leaves[-1]
640 if comma.parent and comma.parent.type == syms.listmaker:
644 # For parens let's check if it's safe to remove the comma. If the
645 # trailing one is the only one, we might mistakenly change a tuple
646 # into a different type by removing the comma.
647 depth = closing.bracket_depth + 1
649 opening = closing.opening_bracket
650 for _opening_index, leaf in enumerate(self.leaves):
657 for leaf in self.leaves[_opening_index + 1:]:
661 bracket_depth = leaf.bracket_depth
662 if bracket_depth == depth and leaf.type == token.COMMA:
664 if leaf.parent and leaf.parent.type == syms.arglist:
674 def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
675 """In a for loop, or comprehension, the variables are often unpacks.
677 To avoid splitting on the comma in this situation, increase the depth of
678 tokens between `for` and `in`.
680 if leaf.type == token.NAME and leaf.value == 'for':
682 self.bracket_tracker.depth += 1
683 self._for_loop_variable = True
688 def maybe_decrement_after_for_loop_variable(self, leaf: Leaf) -> bool:
689 """See `maybe_increment_for_loop_variable` above for explanation."""
690 if self._for_loop_variable and leaf.type == token.NAME and leaf.value == 'in':
691 self.bracket_tracker.depth -= 1
692 self._for_loop_variable = False
697 def maybe_adapt_standalone_comment(self, comment: Leaf) -> bool:
698 """Hack a standalone comment to act as a trailing comment for line splitting.
700 If this line has brackets and a standalone `comment`, we need to adapt
701 it to be able to still reformat the line.
703 This is not perfect, the line to which the standalone comment gets
704 appended will appear "too long" when splitting.
707 comment.type == STANDALONE_COMMENT
708 and self.bracket_tracker.any_open_brackets()
712 comment.type = token.COMMENT
713 comment.prefix = '\n' + ' ' * (self.depth + 1)
714 return self.append_comment(comment)
716 def append_comment(self, comment: Leaf) -> bool:
717 """Add an inline comment to the line."""
718 if comment.type != token.COMMENT:
722 after = id(self.last_non_delimiter())
724 comment.type = STANDALONE_COMMENT
729 if after in self.comments:
730 self.comments[after].value += str(comment)
732 self.comments[after] = comment
735 def last_non_delimiter(self) -> Leaf:
736 """Return the last non-delimiter on the line. Raise LookupError otherwise."""
737 for i in range(len(self.leaves)):
738 last = self.leaves[-i - 1]
739 if not is_delimiter(last):
742 raise LookupError("No non-delimiters found")
744 def __str__(self) -> str:
745 """Render the line."""
749 indent = ' ' * self.depth
750 leaves = iter(self.leaves)
752 res = f'{first.prefix}{indent}{first.value}'
755 for comment in self.comments.values():
759 def __bool__(self) -> bool:
760 """Return True if the line has leaves or comments."""
761 return bool(self.leaves or self.comments)
764 class UnformattedLines(Line):
765 """Just like :class:`Line` but stores lines which aren't reformatted."""
767 def append(self, leaf: Leaf, preformatted: bool = True) -> None:
768 """Just add a new `leaf` to the end of the lines.
770 The `preformatted` argument is ignored.
772 Keeps track of indentation `depth`, which is useful when the user
773 says `# fmt: on`. Otherwise, doesn't do anything with the `leaf`.
776 list(generate_comments(leaf))
777 except FormatOn as f_on:
778 self.leaves.append(f_on.leaf_from_consumed(leaf))
781 self.leaves.append(leaf)
782 if leaf.type == token.INDENT:
784 elif leaf.type == token.DEDENT:
787 def __str__(self) -> str:
788 """Render unformatted lines from leaves which were added with `append()`.
790 `depth` is not used for indentation in this case.
796 for leaf in self.leaves:
800 def append_comment(self, comment: Leaf) -> bool:
801 """Not implemented in this class. Raises `NotImplementedError`."""
802 raise NotImplementedError("Unformatted lines don't store comments separately.")
804 def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
805 """Does nothing and returns False."""
808 def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
809 """Does nothing and returns False."""
812 def maybe_adapt_standalone_comment(self, comment: Leaf) -> bool:
813 """Does nothing and returns False."""
818 class EmptyLineTracker:
819 """Provides a stateful method that returns the number of potential extra
820 empty lines needed before and after the currently processed line.
822 Note: this tracker works on lines that haven't been split yet. It assumes
823 the prefix of the first leaf consists of optional newlines. Those newlines
824 are consumed by `maybe_empty_lines()` and included in the computation.
826 previous_line: Optional[Line] = None
827 previous_after: int = 0
828 previous_defs: List[int] = Factory(list)
830 def maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
831 """Return the number of extra empty lines before and after the `current_line`.
833 This is for separating `def`, `async def` and `class` with extra empty
834 lines (two on module-level), as well as providing an extra empty line
835 after flow control keywords to make them more prominent.
837 if isinstance(current_line, UnformattedLines):
840 before, after = self._maybe_empty_lines(current_line)
841 before -= self.previous_after
842 self.previous_after = after
843 self.previous_line = current_line
846 def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
848 if current_line.depth == 0:
850 if current_line.leaves:
851 # Consume the first leaf's extra newlines.
852 first_leaf = current_line.leaves[0]
853 before = first_leaf.prefix.count('\n')
854 before = min(before, max_allowed)
855 first_leaf.prefix = ''
858 depth = current_line.depth
859 while self.previous_defs and self.previous_defs[-1] >= depth:
860 self.previous_defs.pop()
861 before = 1 if depth else 2
862 is_decorator = current_line.is_decorator
863 if is_decorator or current_line.is_def or current_line.is_class:
865 self.previous_defs.append(depth)
866 if self.previous_line is None:
867 # Don't insert empty lines before the first line in the file.
870 if self.previous_line and self.previous_line.is_decorator:
871 # Don't insert empty lines between decorators.
875 if current_line.depth:
879 if current_line.is_flow_control:
884 and self.previous_line.is_import
885 and not current_line.is_import
886 and depth == self.previous_line.depth
888 return (before or 1), 0
892 and self.previous_line.is_yield
893 and (not current_line.is_yield or depth != self.previous_line.depth)
895 return (before or 1), 0
901 class LineGenerator(Visitor[Line]):
902 """Generates reformatted Line objects. Empty lines are not emitted.
904 Note: destroys the tree it's visiting by mutating prefixes of its leaves
905 in ways that will no longer stringify to valid Python code on the tree.
907 current_line: Line = Factory(Line)
909 def line(self, indent: int = 0, type: Type[Line] = Line) -> Iterator[Line]:
912 If the line is empty, only emit if it makes sense.
913 If the line is too long, split it first and then generate.
915 If any lines were generated, set up a new current_line.
917 if not self.current_line:
918 if self.current_line.__class__ == type:
919 self.current_line.depth += indent
921 self.current_line = type(depth=self.current_line.depth + indent)
922 return # Line is empty, don't emit. Creating a new one unnecessary.
924 complete_line = self.current_line
925 self.current_line = type(depth=complete_line.depth + indent)
928 def visit(self, node: LN) -> Iterator[Line]:
929 """Main method to visit `node` and its children.
931 Yields :class:`Line` objects.
933 if isinstance(self.current_line, UnformattedLines):
934 # File contained `# fmt: off`
935 yield from self.visit_unformatted(node)
938 yield from super().visit(node)
940 def visit_default(self, node: LN) -> Iterator[Line]:
941 """Default `visit_*()` implementation. Recurses to children of `node`."""
942 if isinstance(node, Leaf):
943 any_open_brackets = self.current_line.bracket_tracker.any_open_brackets()
945 for comment in generate_comments(node):
946 if any_open_brackets:
947 # any comment within brackets is subject to splitting
948 self.current_line.append(comment)
949 elif comment.type == token.COMMENT:
950 # regular trailing comment
951 self.current_line.append(comment)
952 yield from self.line()
955 # regular standalone comment
956 yield from self.line()
958 self.current_line.append(comment)
959 yield from self.line()
961 except FormatOff as f_off:
962 f_off.trim_prefix(node)
963 yield from self.line(type=UnformattedLines)
964 yield from self.visit(node)
966 except FormatOn as f_on:
967 # This only happens here if somebody says "fmt: on" multiple
969 f_on.trim_prefix(node)
970 yield from self.visit_default(node)
973 normalize_prefix(node, inside_brackets=any_open_brackets)
974 if node.type not in WHITESPACE:
975 self.current_line.append(node)
976 yield from super().visit_default(node)
978 def visit_INDENT(self, node: Node) -> Iterator[Line]:
979 """Increase indentation level, maybe yield a line."""
980 # In blib2to3 INDENT never holds comments.
981 yield from self.line(+1)
982 yield from self.visit_default(node)
984 def visit_DEDENT(self, node: Node) -> Iterator[Line]:
985 """Decrease indentation level, maybe yield a line."""
986 # DEDENT has no value. Additionally, in blib2to3 it never holds comments.
987 yield from self.line(-1)
989 def visit_stmt(self, node: Node, keywords: Set[str]) -> Iterator[Line]:
990 """Visit a statement.
992 This implementation is shared for `if`, `while`, `for`, `try`, `except`,
993 `def`, `with`, and `class`.
995 The relevant Python language `keywords` for a given statement will be NAME
996 leaves within it. This methods puts those on a separate line.
998 for child in node.children:
999 if child.type == token.NAME and child.value in keywords: # type: ignore
1000 yield from self.line()
1002 yield from self.visit(child)
1004 def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
1005 """Visit a statement without nested statements."""
1006 is_suite_like = node.parent and node.parent.type in STATEMENT
1008 yield from self.line(+1)
1009 yield from self.visit_default(node)
1010 yield from self.line(-1)
1013 yield from self.line()
1014 yield from self.visit_default(node)
1016 def visit_async_stmt(self, node: Node) -> Iterator[Line]:
1017 """Visit `async def`, `async for`, `async with`."""
1018 yield from self.line()
1020 children = iter(node.children)
1021 for child in children:
1022 yield from self.visit(child)
1024 if child.type == token.ASYNC:
1027 internal_stmt = next(children)
1028 for child in internal_stmt.children:
1029 yield from self.visit(child)
1031 def visit_decorators(self, node: Node) -> Iterator[Line]:
1032 """Visit decorators."""
1033 for child in node.children:
1034 yield from self.line()
1035 yield from self.visit(child)
1037 def visit_SEMI(self, leaf: Leaf) -> Iterator[Line]:
1038 """Remove a semicolon and put the other statement on a separate line."""
1039 yield from self.line()
1041 def visit_ENDMARKER(self, leaf: Leaf) -> Iterator[Line]:
1042 """End of file. Process outstanding comments and end with a newline."""
1043 yield from self.visit_default(leaf)
1044 yield from self.line()
1046 def visit_unformatted(self, node: LN) -> Iterator[Line]:
1047 """Used when file contained a `# fmt: off`."""
1048 if isinstance(node, Node):
1049 for child in node.children:
1050 yield from self.visit(child)
1054 self.current_line.append(node)
1055 except FormatOn as f_on:
1056 f_on.trim_prefix(node)
1057 yield from self.line()
1058 yield from self.visit(node)
1060 def __attrs_post_init__(self) -> None:
1061 """You are in a twisty little maze of passages."""
1063 self.visit_if_stmt = partial(v, keywords={'if', 'else', 'elif'})
1064 self.visit_while_stmt = partial(v, keywords={'while', 'else'})
1065 self.visit_for_stmt = partial(v, keywords={'for', 'else'})
1066 self.visit_try_stmt = partial(v, keywords={'try', 'except', 'else', 'finally'})
1067 self.visit_except_clause = partial(v, keywords={'except'})
1068 self.visit_funcdef = partial(v, keywords={'def'})
1069 self.visit_with_stmt = partial(v, keywords={'with'})
1070 self.visit_classdef = partial(v, keywords={'class'})
1071 self.visit_async_funcdef = self.visit_async_stmt
1072 self.visit_decorated = self.visit_decorators
1075 BRACKET = {token.LPAR: token.RPAR, token.LSQB: token.RSQB, token.LBRACE: token.RBRACE}
1076 OPENING_BRACKETS = set(BRACKET.keys())
1077 CLOSING_BRACKETS = set(BRACKET.values())
1078 BRACKETS = OPENING_BRACKETS | CLOSING_BRACKETS
1079 ALWAYS_NO_SPACE = CLOSING_BRACKETS | {token.COMMA, STANDALONE_COMMENT}
1082 def whitespace(leaf: Leaf) -> str: # noqa C901
1083 """Return whitespace prefix if needed for the given `leaf`."""
1090 if t in ALWAYS_NO_SPACE:
1093 if t == token.COMMENT:
1096 assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
1097 if t == token.COLON and p.type not in {syms.subscript, syms.subscriptlist}:
1100 prev = leaf.prev_sibling
1102 prevp = preceding_leaf(p)
1103 if not prevp or prevp.type in OPENING_BRACKETS:
1106 if t == token.COLON:
1107 return SPACE if prevp.type == token.COMMA else NO
1109 if prevp.type == token.EQUAL:
1111 if prevp.parent.type in {
1112 syms.arglist, syms.argument, syms.parameters, syms.varargslist
1116 elif prevp.parent.type == syms.typedargslist:
1117 # A bit hacky: if the equal sign has whitespace, it means we
1118 # previously found it's a typed argument. So, we're using
1122 elif prevp.type == token.DOUBLESTAR:
1123 if prevp.parent and prevp.parent.type in {
1133 elif prevp.type == token.COLON:
1134 if prevp.parent and prevp.parent.type in {syms.subscript, syms.sliceop}:
1139 and prevp.parent.type in {syms.factor, syms.star_expr}
1140 and prevp.type in MATH_OPERATORS
1145 prevp.type == token.RIGHTSHIFT
1147 and prevp.parent.type == syms.shift_expr
1148 and prevp.prev_sibling
1149 and prevp.prev_sibling.type == token.NAME
1150 and prevp.prev_sibling.value == 'print' # type: ignore
1152 # Python 2 print chevron
1155 elif prev.type in OPENING_BRACKETS:
1158 if p.type in {syms.parameters, syms.arglist}:
1159 # untyped function signatures or calls
1163 if not prev or prev.type != token.COMMA:
1166 elif p.type == syms.varargslist:
1171 if prev and prev.type != token.COMMA:
1174 elif p.type == syms.typedargslist:
1175 # typed function signatures
1179 if t == token.EQUAL:
1180 if prev.type != syms.tname:
1183 elif prev.type == token.EQUAL:
1184 # A bit hacky: if the equal sign has whitespace, it means we
1185 # previously found it's a typed argument. So, we're using that, too.
1188 elif prev.type != token.COMMA:
1191 elif p.type == syms.tname:
1194 prevp = preceding_leaf(p)
1195 if not prevp or prevp.type != token.COMMA:
1198 elif p.type == syms.trailer:
1199 # attributes and calls
1200 if t == token.LPAR or t == token.RPAR:
1205 prevp = preceding_leaf(p)
1206 if not prevp or prevp.type != token.NUMBER:
1209 elif t == token.LSQB:
1212 elif prev.type != token.COMMA:
1215 elif p.type == syms.argument:
1217 if t == token.EQUAL:
1221 prevp = preceding_leaf(p)
1222 if not prevp or prevp.type == token.LPAR:
1225 elif prev.type == token.EQUAL or prev.type == token.DOUBLESTAR:
1228 elif p.type == syms.decorator:
1232 elif p.type == syms.dotted_name:
1236 prevp = preceding_leaf(p)
1237 if not prevp or prevp.type == token.AT or prevp.type == token.DOT:
1240 elif p.type == syms.classdef:
1244 if prev and prev.type == token.LPAR:
1247 elif p.type == syms.subscript:
1250 assert p.parent is not None, "subscripts are always parented"
1251 if p.parent.type == syms.subscriptlist:
1259 elif p.type == syms.atom:
1260 if prev and t == token.DOT:
1261 # dots, but not the first one.
1265 p.type == syms.listmaker
1266 or p.type == syms.testlist_gexp
1267 or p.type == syms.subscriptlist
1269 # list interior, including unpacking
1273 elif p.type == syms.dictsetmaker:
1274 # dict and set interior, including unpacking
1278 if prev.type == token.DOUBLESTAR:
1281 elif p.type in {syms.factor, syms.star_expr}:
1284 prevp = preceding_leaf(p)
1285 if not prevp or prevp.type in OPENING_BRACKETS:
1288 prevp_parent = prevp.parent
1289 assert prevp_parent is not None
1290 if prevp.type == token.COLON and prevp_parent.type in {
1291 syms.subscript, syms.sliceop
1295 elif prevp.type == token.EQUAL and prevp_parent.type == syms.argument:
1298 elif t == token.NAME or t == token.NUMBER:
1301 elif p.type == syms.import_from:
1303 if prev and prev.type == token.DOT:
1306 elif t == token.NAME:
1310 if prev and prev.type == token.DOT:
1313 elif p.type == syms.sliceop:
1319 def preceding_leaf(node: Optional[LN]) -> Optional[Leaf]:
1320 """Return the first leaf that precedes `node`, if any."""
1322 res = node.prev_sibling
1324 if isinstance(res, Leaf):
1328 return list(res.leaves())[-1]
1337 def is_delimiter(leaf: Leaf) -> int:
1338 """Return the priority of the `leaf` delimiter. Return 0 if not delimiter.
1340 Higher numbers are higher priority.
1342 if leaf.type == token.COMMA:
1343 return COMMA_PRIORITY
1345 if leaf.type in COMPARATORS:
1346 return COMPARATOR_PRIORITY
1349 leaf.type in MATH_OPERATORS
1351 and leaf.parent.type not in {syms.factor, syms.star_expr}
1353 return MATH_PRIORITY
1358 def generate_comments(leaf: Leaf) -> Iterator[Leaf]:
1359 """Clean the prefix of the `leaf` and generate comments from it, if any.
1361 Comments in lib2to3 are shoved into the whitespace prefix. This happens
1362 in `pgen2/driver.py:Driver.parse_tokens()`. This was a brilliant implementation
1363 move because it does away with modifying the grammar to include all the
1364 possible places in which comments can be placed.
1366 The sad consequence for us though is that comments don't "belong" anywhere.
1367 This is why this function generates simple parentless Leaf objects for
1368 comments. We simply don't know what the correct parent should be.
1370 No matter though, we can live without this. We really only need to
1371 differentiate between inline and standalone comments. The latter don't
1372 share the line with any code.
1374 Inline comments are emitted as regular token.COMMENT leaves. Standalone
1375 are emitted with a fake STANDALONE_COMMENT token identifier.
1386 for index, line in enumerate(p.split('\n')):
1387 consumed += len(line) + 1 # adding the length of the split '\n'
1388 line = line.lstrip()
1391 if not line.startswith('#'):
1394 if index == 0 and leaf.type != token.ENDMARKER:
1395 comment_type = token.COMMENT # simple trailing comment
1397 comment_type = STANDALONE_COMMENT
1398 comment = make_comment(line)
1399 yield Leaf(comment_type, comment, prefix='\n' * nlines)
1401 if comment in {'# fmt: on', '# yapf: enable'}:
1402 raise FormatOn(consumed)
1404 if comment in {'# fmt: off', '# yapf: disable'}:
1405 raise FormatOff(consumed)
1410 def make_comment(content: str) -> str:
1411 """Return a consistently formatted comment from the given `content` string.
1413 All comments (except for "##", "#!", "#:") should have a single space between
1414 the hash sign and the content.
1416 If `content` didn't start with a hash sign, one is provided.
1418 content = content.rstrip()
1422 if content[0] == '#':
1423 content = content[1:]
1424 if content and content[0] not in ' !:#':
1425 content = ' ' + content
1426 return '#' + content
1430 line: Line, line_length: int, inner: bool = False, py36: bool = False
1431 ) -> Iterator[Line]:
1432 """Split a `line` into potentially many lines.
1434 They should fit in the allotted `line_length` but might not be able to.
1435 `inner` signifies that there were a pair of brackets somewhere around the
1436 current `line`, possibly transitively. This means we can fallback to splitting
1437 by delimiters if the LHS/RHS don't yield any results.
1439 If `py36` is True, splitting may generate syntax that is only compatible
1440 with Python 3.6 and later.
1442 if isinstance(line, UnformattedLines):
1446 line_str = str(line).strip('\n')
1447 if len(line_str) <= line_length and '\n' not in line_str:
1452 split_funcs = [left_hand_split]
1453 elif line.inside_brackets:
1454 split_funcs = [delimiter_split]
1455 if '\n' not in line_str:
1456 # Only attempt RHS if we don't have multiline strings or comments
1458 split_funcs.append(right_hand_split)
1460 split_funcs = [right_hand_split]
1461 for split_func in split_funcs:
1462 # We are accumulating lines in `result` because we might want to abort
1463 # mission and return the original line in the end, or attempt a different
1465 result: List[Line] = []
1467 for l in split_func(line, py36=py36):
1468 if str(l).strip('\n') == line_str:
1469 raise CannotSplit("Split function returned an unchanged result")
1472 split_line(l, line_length=line_length, inner=True, py36=py36)
1474 except CannotSplit as cs:
1485 def left_hand_split(line: Line, py36: bool = False) -> Iterator[Line]:
1486 """Split line into many lines, starting with the first matching bracket pair.
1488 Note: this usually looks weird, only use this for function definitions.
1489 Prefer RHS otherwise.
1491 head = Line(depth=line.depth)
1492 body = Line(depth=line.depth + 1, inside_brackets=True)
1493 tail = Line(depth=line.depth)
1494 tail_leaves: List[Leaf] = []
1495 body_leaves: List[Leaf] = []
1496 head_leaves: List[Leaf] = []
1497 current_leaves = head_leaves
1498 matching_bracket = None
1499 for leaf in line.leaves:
1501 current_leaves is body_leaves
1502 and leaf.type in CLOSING_BRACKETS
1503 and leaf.opening_bracket is matching_bracket
1505 current_leaves = tail_leaves if body_leaves else head_leaves
1506 current_leaves.append(leaf)
1507 if current_leaves is head_leaves:
1508 if leaf.type in OPENING_BRACKETS:
1509 matching_bracket = leaf
1510 current_leaves = body_leaves
1511 # Since body is a new indent level, remove spurious leading whitespace.
1513 normalize_prefix(body_leaves[0], inside_brackets=True)
1514 # Build the new lines.
1515 for result, leaves in (
1516 (head, head_leaves), (body, body_leaves), (tail, tail_leaves)
1519 result.append(leaf, preformatted=True)
1520 comment_after = line.comments.get(id(leaf))
1522 result.append(comment_after, preformatted=True)
1523 bracket_split_succeeded_or_raise(head, body, tail)
1524 for result in (head, body, tail):
1529 def right_hand_split(line: Line, py36: bool = False) -> Iterator[Line]:
1530 """Split line into many lines, starting with the last matching bracket pair."""
1531 head = Line(depth=line.depth)
1532 body = Line(depth=line.depth + 1, inside_brackets=True)
1533 tail = Line(depth=line.depth)
1534 tail_leaves: List[Leaf] = []
1535 body_leaves: List[Leaf] = []
1536 head_leaves: List[Leaf] = []
1537 current_leaves = tail_leaves
1538 opening_bracket = None
1539 for leaf in reversed(line.leaves):
1540 if current_leaves is body_leaves:
1541 if leaf is opening_bracket:
1542 current_leaves = head_leaves if body_leaves else tail_leaves
1543 current_leaves.append(leaf)
1544 if current_leaves is tail_leaves:
1545 if leaf.type in CLOSING_BRACKETS:
1546 opening_bracket = leaf.opening_bracket
1547 current_leaves = body_leaves
1548 tail_leaves.reverse()
1549 body_leaves.reverse()
1550 head_leaves.reverse()
1551 # Since body is a new indent level, remove spurious leading whitespace.
1553 normalize_prefix(body_leaves[0], inside_brackets=True)
1554 # Build the new lines.
1555 for result, leaves in (
1556 (head, head_leaves), (body, body_leaves), (tail, tail_leaves)
1559 result.append(leaf, preformatted=True)
1560 comment_after = line.comments.get(id(leaf))
1562 result.append(comment_after, preformatted=True)
1563 bracket_split_succeeded_or_raise(head, body, tail)
1564 for result in (head, body, tail):
1569 def bracket_split_succeeded_or_raise(head: Line, body: Line, tail: Line) -> None:
1570 """Raise :exc:`CannotSplit` if the last left- or right-hand split failed.
1572 Do nothing otherwise.
1574 A left- or right-hand split is based on a pair of brackets. Content before
1575 (and including) the opening bracket is left on one line, content inside the
1576 brackets is put on a separate line, and finally content starting with and
1577 following the closing bracket is put on a separate line.
1579 Those are called `head`, `body`, and `tail`, respectively. If the split
1580 produced the same line (all content in `head`) or ended up with an empty `body`
1581 and the `tail` is just the closing bracket, then it's considered failed.
1583 tail_len = len(str(tail).strip())
1586 raise CannotSplit("Splitting brackets produced the same line")
1590 f"Splitting brackets on an empty body to save "
1591 f"{tail_len} characters is not worth it"
1595 def delimiter_split(line: Line, py36: bool = False) -> Iterator[Line]:
1596 """Split according to delimiters of the highest priority.
1598 This kind of split doesn't increase indentation.
1599 If `py36` is True, the split will add trailing commas also in function
1600 signatures that contain `*` and `**`.
1603 last_leaf = line.leaves[-1]
1605 raise CannotSplit("Line empty")
1607 delimiters = line.bracket_tracker.delimiters
1609 delimiter_priority = line.bracket_tracker.max_delimiter_priority(
1610 exclude={id(last_leaf)}
1613 raise CannotSplit("No delimiters found")
1615 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
1616 lowest_depth = sys.maxsize
1617 trailing_comma_safe = True
1618 for leaf in line.leaves:
1619 current_line.append(leaf, preformatted=True)
1620 comment_after = line.comments.get(id(leaf))
1622 current_line.append(comment_after, preformatted=True)
1623 lowest_depth = min(lowest_depth, leaf.bracket_depth)
1625 leaf.bracket_depth == lowest_depth
1626 and leaf.type == token.STAR
1627 or leaf.type == token.DOUBLESTAR
1629 trailing_comma_safe = trailing_comma_safe and py36
1630 leaf_priority = delimiters.get(id(leaf))
1631 if leaf_priority == delimiter_priority:
1632 normalize_prefix(current_line.leaves[0], inside_brackets=True)
1635 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
1638 delimiter_priority == COMMA_PRIORITY
1639 and current_line.leaves[-1].type != token.COMMA
1640 and trailing_comma_safe
1642 current_line.append(Leaf(token.COMMA, ','))
1643 normalize_prefix(current_line.leaves[0], inside_brackets=True)
1647 def is_import(leaf: Leaf) -> bool:
1648 """Return True if the given leaf starts an import statement."""
1655 (v == 'import' and p and p.type == syms.import_name)
1656 or (v == 'from' and p and p.type == syms.import_from)
1661 def normalize_prefix(leaf: Leaf, *, inside_brackets: bool) -> None:
1662 """Leave existing extra newlines if not `inside_brackets`. Remove everything
1665 Note: don't use backslashes for formatting or you'll lose your voting rights.
1667 if not inside_brackets:
1668 spl = leaf.prefix.split('#')
1669 if '\\' not in spl[0]:
1670 nl_count = spl[-1].count('\n')
1673 leaf.prefix = '\n' * nl_count
1679 def is_python36(node: Node) -> bool:
1680 """Return True if the current file is using Python 3.6+ features.
1682 Currently looking for:
1684 - trailing commas after * or ** in function signatures.
1686 for n in node.pre_order():
1687 if n.type == token.STRING:
1688 value_head = n.value[:2] # type: ignore
1689 if value_head in {'f"', 'F"', "f'", "F'", 'rf', 'fr', 'RF', 'FR'}:
1693 n.type == syms.typedargslist
1695 and n.children[-1].type == token.COMMA
1697 for ch in n.children:
1698 if ch.type == token.STAR or ch.type == token.DOUBLESTAR:
1704 PYTHON_EXTENSIONS = {'.py'}
1705 BLACKLISTED_DIRECTORIES = {
1706 'build', 'buck-out', 'dist', '_build', '.git', '.hg', '.mypy_cache', '.tox', '.venv'
1710 def gen_python_files_in_dir(path: Path) -> Iterator[Path]:
1711 """Generate all files under `path` which aren't under BLACKLISTED_DIRECTORIES
1712 and have one of the PYTHON_EXTENSIONS.
1714 for child in path.iterdir():
1716 if child.name in BLACKLISTED_DIRECTORIES:
1719 yield from gen_python_files_in_dir(child)
1721 elif child.suffix in PYTHON_EXTENSIONS:
1727 """Provides a reformatting counter. Can be rendered with `str(report)`."""
1729 change_count: int = 0
1731 failure_count: int = 0
1733 def done(self, src: Path, changed: bool) -> None:
1734 """Increment the counter for successful reformatting. Write out a message."""
1736 reformatted = 'would reformat' if self.check else 'reformatted'
1737 out(f'{reformatted} {src}')
1738 self.change_count += 1
1740 out(f'{src} already well formatted, good job.', bold=False)
1741 self.same_count += 1
1743 def failed(self, src: Path, message: str) -> None:
1744 """Increment the counter for failed reformatting. Write out a message."""
1745 err(f'error: cannot format {src}: {message}')
1746 self.failure_count += 1
1749 def return_code(self) -> int:
1750 """Return the exit code that the app should use.
1752 This considers the current state of changed files and failures:
1753 - if there were any failures, return 123;
1754 - if any files were changed and --check is being used, return 1;
1755 - otherwise return 0.
1757 # According to http://tldp.org/LDP/abs/html/exitcodes.html starting with
1758 # 126 we have special returncodes reserved by the shell.
1759 if self.failure_count:
1762 elif self.change_count and self.check:
1767 def __str__(self) -> str:
1768 """Render a color report of the current state.
1770 Use `click.unstyle` to remove colors.
1773 reformatted = "would be reformatted"
1774 unchanged = "would be left unchanged"
1775 failed = "would fail to reformat"
1777 reformatted = "reformatted"
1778 unchanged = "left unchanged"
1779 failed = "failed to reformat"
1781 if self.change_count:
1782 s = 's' if self.change_count > 1 else ''
1784 click.style(f'{self.change_count} file{s} {reformatted}', bold=True)
1787 s = 's' if self.same_count > 1 else ''
1788 report.append(f'{self.same_count} file{s} {unchanged}')
1789 if self.failure_count:
1790 s = 's' if self.failure_count > 1 else ''
1792 click.style(f'{self.failure_count} file{s} {failed}', fg='red')
1794 return ', '.join(report) + '.'
1797 def assert_equivalent(src: str, dst: str) -> None:
1798 """Raise AssertionError if `src` and `dst` aren't equivalent."""
1803 def _v(node: ast.AST, depth: int = 0) -> Iterator[str]:
1804 """Simple visitor generating strings to compare ASTs by content."""
1805 yield f"{' ' * depth}{node.__class__.__name__}("
1807 for field in sorted(node._fields):
1809 value = getattr(node, field)
1810 except AttributeError:
1813 yield f"{' ' * (depth+1)}{field}="
1815 if isinstance(value, list):
1817 if isinstance(item, ast.AST):
1818 yield from _v(item, depth + 2)
1820 elif isinstance(value, ast.AST):
1821 yield from _v(value, depth + 2)
1824 yield f"{' ' * (depth+2)}{value!r}, # {value.__class__.__name__}"
1826 yield f"{' ' * depth}) # /{node.__class__.__name__}"
1829 src_ast = ast.parse(src)
1830 except Exception as exc:
1831 major, minor = sys.version_info[:2]
1832 raise AssertionError(
1833 f"cannot use --safe with this file; failed to parse source file "
1834 f"with Python {major}.{minor}'s builtin AST. Re-run with --fast "
1835 f"or stop using deprecated Python 2 syntax. AST error message: {exc}"
1839 dst_ast = ast.parse(dst)
1840 except Exception as exc:
1841 log = dump_to_file(''.join(traceback.format_tb(exc.__traceback__)), dst)
1842 raise AssertionError(
1843 f"INTERNAL ERROR: Black produced invalid code: {exc}. "
1844 f"Please report a bug on https://github.com/ambv/black/issues. "
1845 f"This invalid output might be helpful: {log}"
1848 src_ast_str = '\n'.join(_v(src_ast))
1849 dst_ast_str = '\n'.join(_v(dst_ast))
1850 if src_ast_str != dst_ast_str:
1851 log = dump_to_file(diff(src_ast_str, dst_ast_str, 'src', 'dst'))
1852 raise AssertionError(
1853 f"INTERNAL ERROR: Black produced code that is not equivalent to "
1855 f"Please report a bug on https://github.com/ambv/black/issues. "
1856 f"This diff might be helpful: {log}"
1860 def assert_stable(src: str, dst: str, line_length: int) -> None:
1861 """Raise AssertionError if `dst` reformats differently the second time."""
1862 newdst = format_str(dst, line_length=line_length)
1865 diff(src, dst, 'source', 'first pass'),
1866 diff(dst, newdst, 'first pass', 'second pass'),
1868 raise AssertionError(
1869 f"INTERNAL ERROR: Black produced different code on the second pass "
1870 f"of the formatter. "
1871 f"Please report a bug on https://github.com/ambv/black/issues. "
1872 f"This diff might be helpful: {log}"
1876 def dump_to_file(*output: str) -> str:
1877 """Dump `output` to a temporary file. Return path to the file."""
1880 with tempfile.NamedTemporaryFile(
1881 mode='w', prefix='blk_', suffix='.log', delete=False
1883 for lines in output:
1889 def diff(a: str, b: str, a_name: str, b_name: str) -> str:
1890 """Return a unified diff string between strings `a` and `b`."""
1893 a_lines = [line + '\n' for line in a.split('\n')]
1894 b_lines = [line + '\n' for line in b.split('\n')]
1896 difflib.unified_diff(a_lines, b_lines, fromfile=a_name, tofile=b_name, n=5)
1900 if __name__ == '__main__':