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 `format_file` when the 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 `left_hand_split()`, `right_hand_split()`, and `delimiter_split()`.
51 class FormatError(Exception):
52 """Base exception for `# fmt: on` and `# fmt: off` handling.
54 It holds the number of bytes of the prefix consumed before the format
55 control comment appeared.
58 def __init__(self, consumed: int) -> None:
59 super().__init__(consumed)
60 self.consumed = consumed
62 def trim_prefix(self, leaf: Leaf) -> None:
63 leaf.prefix = leaf.prefix[self.consumed:]
65 def leaf_from_consumed(self, leaf: Leaf) -> Leaf:
66 """Returns a new Leaf from the consumed part of the prefix."""
67 unformatted_prefix = leaf.prefix[:self.consumed]
68 return Leaf(token.NEWLINE, unformatted_prefix)
71 class FormatOn(FormatError):
72 """Found a comment like `# fmt: on` in the file."""
75 class FormatOff(FormatError):
76 """Found a comment like `# fmt: off` in the file."""
84 default=DEFAULT_LINE_LENGTH,
85 help='How many character per line to allow.',
92 "Don't write back the files, just return the status. Return code 0 "
93 "means nothing would change. Return code 1 means some files would be "
94 "reformatted. Return code 123 means there was an internal error."
100 help='If --fast given, skip temporary sanity checks. [default: --safe]',
102 @click.version_option(version=__version__)
107 exists=True, file_okay=True, dir_okay=True, readable=True, allow_dash=True
112 ctx: click.Context, line_length: int, check: bool, fast: bool, src: List[str]
114 """The uncompromising code formatter."""
115 sources: List[Path] = []
119 sources.extend(gen_python_files_in_dir(p))
121 # if a file was explicitly given, we don't care about its extension
124 sources.append(Path('-'))
126 err(f'invalid path: {s}')
127 if len(sources) == 0:
129 elif len(sources) == 1:
131 report = Report(check=check)
133 if not p.is_file() and str(p) == '-':
134 changed = format_stdin_to_stdout(
135 line_length=line_length, fast=fast, write_back=not check
138 changed = format_file_in_place(
139 p, line_length=line_length, fast=fast, write_back=not check
141 report.done(p, changed)
142 except Exception as exc:
143 report.failed(p, str(exc))
144 ctx.exit(report.return_code)
146 loop = asyncio.get_event_loop()
147 executor = ProcessPoolExecutor(max_workers=os.cpu_count())
150 return_code = loop.run_until_complete(
152 sources, line_length, not check, fast, loop, executor
157 ctx.exit(return_code)
160 async def schedule_formatting(
169 src: loop.run_in_executor(
170 executor, format_file_in_place, src, line_length, fast, write_back
174 await asyncio.wait(tasks.values())
177 for src, task in tasks.items():
179 report.failed(src, 'timed out, cancelling')
181 cancelled.append(task)
182 elif task.exception():
183 report.failed(src, str(task.exception()))
185 report.done(src, task.result())
187 await asyncio.wait(cancelled, timeout=2)
188 out('All done! ✨ 🍰 ✨')
189 click.echo(str(report))
190 return report.return_code
193 def format_file_in_place(
194 src: Path, line_length: int, fast: bool, write_back: bool = False
196 """Format the file and rewrite if changed. Return True if changed."""
197 with tokenize.open(src) as src_buffer:
198 src_contents = src_buffer.read()
200 contents = format_file_contents(
201 src_contents, line_length=line_length, fast=fast
203 except NothingChanged:
207 with open(src, "w", encoding=src_buffer.encoding) as f:
212 def format_stdin_to_stdout(
213 line_length: int, fast: bool, write_back: bool = False
215 """Format file on stdin and pipe output to stdout. Return True if changed."""
216 contents = sys.stdin.read()
218 contents = format_file_contents(contents, line_length=line_length, fast=fast)
221 except NothingChanged:
226 sys.stdout.write(contents)
229 def format_file_contents(
230 src_contents: str, line_length: int, fast: bool
232 """Reformats a file and returns its contents and encoding."""
233 if src_contents.strip() == '':
236 dst_contents = format_str(src_contents, line_length=line_length)
237 if src_contents == dst_contents:
241 assert_equivalent(src_contents, dst_contents)
242 assert_stable(src_contents, dst_contents, line_length=line_length)
246 def format_str(src_contents: str, line_length: int) -> FileContent:
247 """Reformats a string and returns new contents."""
248 src_node = lib2to3_parse(src_contents)
250 lines = LineGenerator()
251 elt = EmptyLineTracker()
252 py36 = is_python36(src_node)
255 for current_line in lines.visit(src_node):
256 for _ in range(after):
257 dst_contents += str(empty_line)
258 before, after = elt.maybe_empty_lines(current_line)
259 for _ in range(before):
260 dst_contents += str(empty_line)
261 for line in split_line(current_line, line_length=line_length, py36=py36):
262 dst_contents += str(line)
267 pygram.python_grammar_no_print_statement_no_exec_statement,
268 pygram.python_grammar_no_print_statement,
269 pygram.python_grammar_no_exec_statement,
270 pygram.python_grammar,
274 def lib2to3_parse(src_txt: str) -> Node:
275 """Given a string with source, return the lib2to3 Node."""
276 grammar = pygram.python_grammar_no_print_statement
277 if src_txt[-1] != '\n':
278 nl = '\r\n' if '\r\n' in src_txt[:1024] else '\n'
280 for grammar in GRAMMARS:
281 drv = driver.Driver(grammar, pytree.convert)
283 result = drv.parse_string(src_txt, True)
286 except ParseError as pe:
287 lineno, column = pe.context[1]
288 lines = src_txt.splitlines()
290 faulty_line = lines[lineno - 1]
292 faulty_line = "<line number missing in source>"
293 exc = ValueError(f"Cannot parse: {lineno}:{column}: {faulty_line}")
297 if isinstance(result, Leaf):
298 result = Node(syms.file_input, [result])
302 def lib2to3_unparse(node: Node) -> str:
303 """Given a lib2to3 node, return its string representation."""
311 class Visitor(Generic[T]):
312 """Basic lib2to3 visitor that yields things of type `T` on `visit()`."""
314 def visit(self, node: LN) -> Iterator[T]:
315 """Main method to start the visit process. Yields objects of type `T`.
317 It tries to find a `visit_*()` method for the given `node.type`, like
318 `visit_simple_stmt` for Node objects or `visit_INDENT` for Leaf objects.
319 If no dedicated `visit_*()` method is found, chooses `visit_default()`
322 Then yields objects of type `T` from the selected visitor.
325 name = token.tok_name[node.type]
327 name = type_repr(node.type)
328 yield from getattr(self, f'visit_{name}', self.visit_default)(node)
330 def visit_default(self, node: LN) -> Iterator[T]:
331 """Default `visit_*()` implementation. Recurses to children of `node`."""
332 if isinstance(node, Node):
333 for child in node.children:
334 yield from self.visit(child)
338 class DebugVisitor(Visitor[T]):
341 def visit_default(self, node: LN) -> Iterator[T]:
342 indent = ' ' * (2 * self.tree_depth)
343 if isinstance(node, Node):
344 _type = type_repr(node.type)
345 out(f'{indent}{_type}', fg='yellow')
347 for child in node.children:
348 yield from self.visit(child)
351 out(f'{indent}/{_type}', fg='yellow', bold=False)
353 _type = token.tok_name.get(node.type, str(node.type))
354 out(f'{indent}{_type}', fg='blue', nl=False)
356 # We don't have to handle prefixes for `Node` objects since
357 # that delegates to the first child anyway.
358 out(f' {node.prefix!r}', fg='green', bold=False, nl=False)
359 out(f' {node.value!r}', fg='blue', bold=False)
362 def show(cls, code: str) -> None:
363 """Pretty-prints a given string of `code`.
365 Convenience method for debugging.
367 v: DebugVisitor[None] = DebugVisitor()
368 list(v.visit(lib2to3_parse(code)))
371 KEYWORDS = set(keyword.kwlist)
372 WHITESPACE = {token.DEDENT, token.INDENT, token.NEWLINE}
373 FLOW_CONTROL = {'return', 'raise', 'break', 'continue'}
384 STANDALONE_COMMENT = 153
385 LOGIC_OPERATORS = {'and', 'or'}
409 COMPREHENSION_PRIORITY = 20
413 COMPARATOR_PRIORITY = 3
418 class BracketTracker:
419 """Keeps track of brackets on a line."""
421 #: Current bracket depth.
423 #: All currently unclosed brackets.
424 bracket_match: Dict[Tuple[Depth, NodeType], Leaf] = Factory(dict)
425 #: All current delimiters with their assigned priority.
426 delimiters: Dict[LeafID, Priority] = Factory(dict)
427 #: Last processed leaf, if any.
428 previous: Optional[Leaf] = None
430 def mark(self, leaf: Leaf) -> None:
431 """Marks `leaf` with bracket-related metadata. Keeps track of delimiters.
433 All leaves receive an int `bracket_depth` field that stores how deep
434 within brackets a given leaf is. 0 means there are no enclosing brackets
435 that started on this line.
437 If a leaf is itself a closing bracket, it receives an `opening_bracket`
438 field that it forms a pair with. This is a one-directional link to
439 avoid reference cycles.
441 If a leaf is a delimiter (a token on which Black can split the line if
442 needed) and it's on depth 0, its `id()` is stored in the tracker's
445 if leaf.type == token.COMMENT:
448 if leaf.type in CLOSING_BRACKETS:
450 opening_bracket = self.bracket_match.pop((self.depth, leaf.type))
451 leaf.opening_bracket = opening_bracket
452 leaf.bracket_depth = self.depth
454 delim = is_delimiter(leaf)
456 self.delimiters[id(leaf)] = delim
457 elif self.previous is not None:
458 if leaf.type == token.STRING and self.previous.type == token.STRING:
459 self.delimiters[id(self.previous)] = STRING_PRIORITY
461 leaf.type == token.NAME
462 and leaf.value == 'for'
464 and leaf.parent.type in {syms.comp_for, syms.old_comp_for}
466 self.delimiters[id(self.previous)] = COMPREHENSION_PRIORITY
468 leaf.type == token.NAME
469 and leaf.value == 'if'
471 and leaf.parent.type in {syms.comp_if, syms.old_comp_if}
473 self.delimiters[id(self.previous)] = COMPREHENSION_PRIORITY
475 leaf.type == token.NAME
476 and leaf.value in LOGIC_OPERATORS
479 self.delimiters[id(self.previous)] = LOGIC_PRIORITY
480 if leaf.type in OPENING_BRACKETS:
481 self.bracket_match[self.depth, BRACKET[leaf.type]] = leaf
485 def any_open_brackets(self) -> bool:
486 """Returns True if there is an yet unmatched open bracket on the line."""
487 return bool(self.bracket_match)
489 def max_delimiter_priority(self, exclude: Iterable[LeafID] = ()) -> int:
490 """Returns the highest priority of a delimiter found on the line.
492 Values are consistent with what `is_delimiter()` returns.
494 return max(v for k, v in self.delimiters.items() if k not in exclude)
499 """Holds leaves and comments. Can be printed with `str(line)`."""
504 leaves: List[Leaf] = Factory(list)
505 #: inline comments that belong on this line
506 comments: Dict[LeafID, Leaf] = Factory(dict)
507 bracket_tracker: BracketTracker = Factory(BracketTracker)
508 inside_brackets: bool = False
509 has_for: bool = False
510 _for_loop_variable: bool = False
512 def append(self, leaf: Leaf, preformatted: bool = False) -> None:
513 """Add a new `leaf` to the end of the line.
515 Unless `preformatted` is True, the `leaf` will receive a new consistent
516 whitespace prefix and metadata applied by :class:`BracketTracker`.
517 Trailing commas are maybe removed, unpacked for loop variables are
518 demoted from being delimiters.
520 Inline comments are put aside.
522 has_value = leaf.value.strip()
526 if self.leaves and not preformatted:
527 # Note: at this point leaf.prefix should be empty except for
528 # imports, for which we only preserve newlines.
529 leaf.prefix += whitespace(leaf)
530 if self.inside_brackets or not preformatted:
531 self.maybe_decrement_after_for_loop_variable(leaf)
532 self.bracket_tracker.mark(leaf)
533 self.maybe_remove_trailing_comma(leaf)
534 self.maybe_increment_for_loop_variable(leaf)
535 if self.maybe_adapt_standalone_comment(leaf):
538 if not self.append_comment(leaf):
539 self.leaves.append(leaf)
542 def is_comment(self) -> bool:
543 """Is this line a standalone comment?"""
544 return bool(self) and self.leaves[0].type == STANDALONE_COMMENT
547 def is_decorator(self) -> bool:
548 """Is this line a decorator?"""
549 return bool(self) and self.leaves[0].type == token.AT
552 def is_import(self) -> bool:
553 """Is this an import line?"""
554 return bool(self) and is_import(self.leaves[0])
557 def is_class(self) -> bool:
558 """Is this a class definition?"""
561 and self.leaves[0].type == token.NAME
562 and self.leaves[0].value == 'class'
566 def is_def(self) -> bool:
567 """Is this a function definition? (Also returns True for async defs.)"""
569 first_leaf = self.leaves[0]
574 second_leaf: Optional[Leaf] = self.leaves[1]
578 (first_leaf.type == token.NAME and first_leaf.value == 'def')
580 first_leaf.type == token.ASYNC
581 and second_leaf is not None
582 and second_leaf.type == token.NAME
583 and second_leaf.value == 'def'
588 def is_flow_control(self) -> bool:
589 """Is this a flow control statement?
591 Those are `return`, `raise`, `break`, and `continue`.
595 and self.leaves[0].type == token.NAME
596 and self.leaves[0].value in FLOW_CONTROL
600 def is_yield(self) -> bool:
601 """Is this a yield statement?"""
604 and self.leaves[0].type == token.NAME
605 and self.leaves[0].value == 'yield'
608 def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
609 """Remove trailing comma if there is one and it's safe."""
612 and self.leaves[-1].type == token.COMMA
613 and closing.type in CLOSING_BRACKETS
617 if closing.type == token.RBRACE:
621 if closing.type == token.RSQB:
622 comma = self.leaves[-1]
623 if comma.parent and comma.parent.type == syms.listmaker:
627 # For parens let's check if it's safe to remove the comma. If the
628 # trailing one is the only one, we might mistakenly change a tuple
629 # into a different type by removing the comma.
630 depth = closing.bracket_depth + 1
632 opening = closing.opening_bracket
633 for _opening_index, leaf in enumerate(self.leaves):
640 for leaf in self.leaves[_opening_index + 1:]:
644 bracket_depth = leaf.bracket_depth
645 if bracket_depth == depth and leaf.type == token.COMMA:
647 if leaf.parent and leaf.parent.type == syms.arglist:
657 def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
658 """In a for loop, or comprehension, the variables are often unpacks.
660 To avoid splitting on the comma in this situation, we will increase
661 the depth of tokens between `for` and `in`.
663 if leaf.type == token.NAME and leaf.value == 'for':
665 self.bracket_tracker.depth += 1
666 self._for_loop_variable = True
671 def maybe_decrement_after_for_loop_variable(self, leaf: Leaf) -> bool:
672 """See `maybe_increment_for_loop_variable` above for explanation."""
673 if self._for_loop_variable and leaf.type == token.NAME and leaf.value == 'in':
674 self.bracket_tracker.depth -= 1
675 self._for_loop_variable = False
680 def maybe_adapt_standalone_comment(self, comment: Leaf) -> bool:
681 """Hack a standalone comment to act as a trailing comment for line splitting.
683 If this line has brackets and a standalone `comment`, we need to adapt
684 it to be able to still reformat the line.
686 This is not perfect, the line to which the standalone comment gets
687 appended will appear "too long" when splitting.
690 comment.type == STANDALONE_COMMENT
691 and self.bracket_tracker.any_open_brackets()
695 comment.type = token.COMMENT
696 comment.prefix = '\n' + ' ' * (self.depth + 1)
697 return self.append_comment(comment)
699 def append_comment(self, comment: Leaf) -> bool:
700 """Add an inline comment to the line."""
701 if comment.type != token.COMMENT:
705 after = id(self.last_non_delimiter())
707 comment.type = STANDALONE_COMMENT
712 if after in self.comments:
713 self.comments[after].value += str(comment)
715 self.comments[after] = comment
718 def last_non_delimiter(self) -> Leaf:
719 """Returns the last non-delimiter on the line. Raises LookupError otherwise."""
720 for i in range(len(self.leaves)):
721 last = self.leaves[-i - 1]
722 if not is_delimiter(last):
725 raise LookupError("No non-delimiters found")
727 def __str__(self) -> str:
728 """Render the line."""
732 indent = ' ' * self.depth
733 leaves = iter(self.leaves)
735 res = f'{first.prefix}{indent}{first.value}'
738 for comment in self.comments.values():
742 def __bool__(self) -> bool:
743 """Returns True if the line has leaves or comments."""
744 return bool(self.leaves or self.comments)
747 class UnformattedLines(Line):
748 """Just like :class:`Line` but stores lines which aren't reformatted."""
750 def append(self, leaf: Leaf, preformatted: bool = True) -> None:
751 """Just add a new `leaf` to the end of the lines.
753 The `preformatted` argument is ignored.
755 Keeps track of indentation `depth`, which is useful when the user
756 says `# fmt: on`. Otherwise, doesn't do anything with the `leaf`.
759 list(generate_comments(leaf))
760 except FormatOn as f_on:
761 self.leaves.append(f_on.leaf_from_consumed(leaf))
764 self.leaves.append(leaf)
765 if leaf.type == token.INDENT:
767 elif leaf.type == token.DEDENT:
770 def append_comment(self, comment: Leaf) -> bool:
771 """Not implemented in this class."""
772 raise NotImplementedError("Unformatted lines don't store comments separately.")
774 def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
775 """Does nothing and returns False."""
778 def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
779 """Does nothing and returns False."""
782 def maybe_adapt_standalone_comment(self, comment: Leaf) -> bool:
783 """Does nothing and returns False."""
786 def __str__(self) -> str:
787 """Renders unformatted lines from leaves which were added with `append()`.
789 `depth` is not used for indentation in this case.
795 for leaf in self.leaves:
801 class EmptyLineTracker:
802 """Provides a stateful method that returns the number of potential extra
803 empty lines needed before and after the currently processed line.
805 Note: this tracker works on lines that haven't been split yet. It assumes
806 the prefix of the first leaf consists of optional newlines. Those newlines
807 are consumed by `maybe_empty_lines()` and included in the computation.
809 previous_line: Optional[Line] = None
810 previous_after: int = 0
811 previous_defs: List[int] = Factory(list)
813 def maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
814 """Returns the number of extra empty lines before and after the `current_line`.
816 This is for separating `def`, `async def` and `class` with extra empty lines
817 (two on module-level), as well as providing an extra empty line after flow
818 control keywords to make them more prominent.
820 if isinstance(current_line, UnformattedLines):
823 before, after = self._maybe_empty_lines(current_line)
824 before -= self.previous_after
825 self.previous_after = after
826 self.previous_line = current_line
829 def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
831 if current_line.depth == 0:
833 if current_line.leaves:
834 # Consume the first leaf's extra newlines.
835 first_leaf = current_line.leaves[0]
836 before = first_leaf.prefix.count('\n')
837 before = min(before, max_allowed)
838 first_leaf.prefix = ''
841 depth = current_line.depth
842 while self.previous_defs and self.previous_defs[-1] >= depth:
843 self.previous_defs.pop()
844 before = 1 if depth else 2
845 is_decorator = current_line.is_decorator
846 if is_decorator or current_line.is_def or current_line.is_class:
848 self.previous_defs.append(depth)
849 if self.previous_line is None:
850 # Don't insert empty lines before the first line in the file.
853 if self.previous_line and self.previous_line.is_decorator:
854 # Don't insert empty lines between decorators.
858 if current_line.depth:
862 if current_line.is_flow_control:
867 and self.previous_line.is_import
868 and not current_line.is_import
869 and depth == self.previous_line.depth
871 return (before or 1), 0
875 and self.previous_line.is_yield
876 and (not current_line.is_yield or depth != self.previous_line.depth)
878 return (before or 1), 0
884 class LineGenerator(Visitor[Line]):
885 """Generates reformatted Line objects. Empty lines are not emitted.
887 Note: destroys the tree it's visiting by mutating prefixes of its leaves
888 in ways that will no longer stringify to valid Python code on the tree.
890 current_line: Line = Factory(Line)
892 def line(self, indent: int = 0, type: Type[Line] = Line) -> Iterator[Line]:
895 If the line is empty, only emit if it makes sense.
896 If the line is too long, split it first and then generate.
898 If any lines were generated, set up a new current_line.
900 if not self.current_line:
901 if self.current_line.__class__ == type:
902 self.current_line.depth += indent
904 self.current_line = type(depth=self.current_line.depth + indent)
905 return # Line is empty, don't emit. Creating a new one unnecessary.
907 complete_line = self.current_line
908 self.current_line = type(depth=complete_line.depth + indent)
911 def visit(self, node: LN) -> Iterator[Line]:
912 """Main method to start the visit process. Yields :class:`Line` objects."""
913 if isinstance(self.current_line, UnformattedLines):
914 # File contained `# fmt: off`
915 yield from self.visit_unformatted(node)
918 yield from super().visit(node)
920 def visit_default(self, node: LN) -> Iterator[Line]:
921 """Default `visit_*()` implementation. Recurses to children of `node`."""
922 if isinstance(node, Leaf):
923 any_open_brackets = self.current_line.bracket_tracker.any_open_brackets()
925 for comment in generate_comments(node):
926 if any_open_brackets:
927 # any comment within brackets is subject to splitting
928 self.current_line.append(comment)
929 elif comment.type == token.COMMENT:
930 # regular trailing comment
931 self.current_line.append(comment)
932 yield from self.line()
935 # regular standalone comment
936 yield from self.line()
938 self.current_line.append(comment)
939 yield from self.line()
941 except FormatOff as f_off:
942 f_off.trim_prefix(node)
943 yield from self.line(type=UnformattedLines)
944 yield from self.visit(node)
946 except FormatOn as f_on:
947 # This only happens here if somebody says "fmt: on" multiple
949 f_on.trim_prefix(node)
950 yield from self.visit_default(node)
953 normalize_prefix(node, inside_brackets=any_open_brackets)
954 if node.type not in WHITESPACE:
955 self.current_line.append(node)
956 yield from super().visit_default(node)
958 def visit_INDENT(self, node: Node) -> Iterator[Line]:
959 """Increases indentation level, maybe yields a line."""
960 # In blib2to3 INDENT never holds comments.
961 yield from self.line(+1)
962 yield from self.visit_default(node)
964 def visit_DEDENT(self, node: Node) -> Iterator[Line]:
965 """Decreases indentation level, maybe yields a line."""
966 # DEDENT has no value. Additionally, in blib2to3 it never holds comments.
967 yield from self.line(-1)
969 def visit_stmt(self, node: Node, keywords: Set[str]) -> Iterator[Line]:
970 """Visits a statement.
972 This implementation is shared for `if`, `while`, `for`, `try`, `except`,
973 `def`, `with`, and `class`.
975 The relevant Python language `keywords` for a given statement will be NAME
976 leaves within it. This methods puts those on a separate line.
978 for child in node.children:
979 if child.type == token.NAME and child.value in keywords: # type: ignore
980 yield from self.line()
982 yield from self.visit(child)
984 def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
985 """Visits a statement without nested statements."""
986 is_suite_like = node.parent and node.parent.type in STATEMENT
988 yield from self.line(+1)
989 yield from self.visit_default(node)
990 yield from self.line(-1)
993 yield from self.line()
994 yield from self.visit_default(node)
996 def visit_async_stmt(self, node: Node) -> Iterator[Line]:
997 """Visits `async def`, `async for`, `async with`."""
998 yield from self.line()
1000 children = iter(node.children)
1001 for child in children:
1002 yield from self.visit(child)
1004 if child.type == token.ASYNC:
1007 internal_stmt = next(children)
1008 for child in internal_stmt.children:
1009 yield from self.visit(child)
1011 def visit_decorators(self, node: Node) -> Iterator[Line]:
1012 """Visits decorators."""
1013 for child in node.children:
1014 yield from self.line()
1015 yield from self.visit(child)
1017 def visit_SEMI(self, leaf: Leaf) -> Iterator[Line]:
1018 """Semicolons are always removed.
1020 Statements between them are put on separate lines.
1022 yield from self.line()
1024 def visit_ENDMARKER(self, leaf: Leaf) -> Iterator[Line]:
1027 Process outstanding comments and end with a newline.
1029 yield from self.visit_default(leaf)
1030 yield from self.line()
1032 def visit_unformatted(self, node: LN) -> Iterator[Line]:
1033 """Used when file contained a `# fmt: off`."""
1034 if isinstance(node, Node):
1035 for child in node.children:
1036 yield from self.visit(child)
1040 self.current_line.append(node)
1041 except FormatOn as f_on:
1042 f_on.trim_prefix(node)
1043 yield from self.line()
1044 yield from self.visit(node)
1046 def __attrs_post_init__(self) -> None:
1047 """You are in a twisty little maze of passages."""
1049 self.visit_if_stmt = partial(v, keywords={'if', 'else', 'elif'})
1050 self.visit_while_stmt = partial(v, keywords={'while', 'else'})
1051 self.visit_for_stmt = partial(v, keywords={'for', 'else'})
1052 self.visit_try_stmt = partial(v, keywords={'try', 'except', 'else', 'finally'})
1053 self.visit_except_clause = partial(v, keywords={'except'})
1054 self.visit_funcdef = partial(v, keywords={'def'})
1055 self.visit_with_stmt = partial(v, keywords={'with'})
1056 self.visit_classdef = partial(v, keywords={'class'})
1057 self.visit_async_funcdef = self.visit_async_stmt
1058 self.visit_decorated = self.visit_decorators
1061 BRACKET = {token.LPAR: token.RPAR, token.LSQB: token.RSQB, token.LBRACE: token.RBRACE}
1062 OPENING_BRACKETS = set(BRACKET.keys())
1063 CLOSING_BRACKETS = set(BRACKET.values())
1064 BRACKETS = OPENING_BRACKETS | CLOSING_BRACKETS
1065 ALWAYS_NO_SPACE = CLOSING_BRACKETS | {token.COMMA, STANDALONE_COMMENT}
1068 def whitespace(leaf: Leaf) -> str: # noqa C901
1069 """Return whitespace prefix if needed for the given `leaf`."""
1076 if t in ALWAYS_NO_SPACE:
1079 if t == token.COMMENT:
1082 assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
1083 if t == token.COLON and p.type not in {syms.subscript, syms.subscriptlist}:
1086 prev = leaf.prev_sibling
1088 prevp = preceding_leaf(p)
1089 if not prevp or prevp.type in OPENING_BRACKETS:
1092 if t == token.COLON:
1093 return SPACE if prevp.type == token.COMMA else NO
1095 if prevp.type == token.EQUAL:
1097 if prevp.parent.type in {
1098 syms.arglist, syms.argument, syms.parameters, syms.varargslist
1102 elif prevp.parent.type == syms.typedargslist:
1103 # A bit hacky: if the equal sign has whitespace, it means we
1104 # previously found it's a typed argument. So, we're using
1108 elif prevp.type == token.DOUBLESTAR:
1109 if prevp.parent and prevp.parent.type in {
1119 elif prevp.type == token.COLON:
1120 if prevp.parent and prevp.parent.type in {syms.subscript, syms.sliceop}:
1125 and prevp.parent.type in {syms.factor, syms.star_expr}
1126 and prevp.type in MATH_OPERATORS
1131 prevp.type == token.RIGHTSHIFT
1133 and prevp.parent.type == syms.shift_expr
1134 and prevp.prev_sibling
1135 and prevp.prev_sibling.type == token.NAME
1136 and prevp.prev_sibling.value == 'print' # type: ignore
1138 # Python 2 print chevron
1141 elif prev.type in OPENING_BRACKETS:
1144 if p.type in {syms.parameters, syms.arglist}:
1145 # untyped function signatures or calls
1149 if not prev or prev.type != token.COMMA:
1152 elif p.type == syms.varargslist:
1157 if prev and prev.type != token.COMMA:
1160 elif p.type == syms.typedargslist:
1161 # typed function signatures
1165 if t == token.EQUAL:
1166 if prev.type != syms.tname:
1169 elif prev.type == token.EQUAL:
1170 # A bit hacky: if the equal sign has whitespace, it means we
1171 # previously found it's a typed argument. So, we're using that, too.
1174 elif prev.type != token.COMMA:
1177 elif p.type == syms.tname:
1180 prevp = preceding_leaf(p)
1181 if not prevp or prevp.type != token.COMMA:
1184 elif p.type == syms.trailer:
1185 # attributes and calls
1186 if t == token.LPAR or t == token.RPAR:
1191 prevp = preceding_leaf(p)
1192 if not prevp or prevp.type != token.NUMBER:
1195 elif t == token.LSQB:
1198 elif prev.type != token.COMMA:
1201 elif p.type == syms.argument:
1203 if t == token.EQUAL:
1207 prevp = preceding_leaf(p)
1208 if not prevp or prevp.type == token.LPAR:
1211 elif prev.type == token.EQUAL or prev.type == token.DOUBLESTAR:
1214 elif p.type == syms.decorator:
1218 elif p.type == syms.dotted_name:
1222 prevp = preceding_leaf(p)
1223 if not prevp or prevp.type == token.AT or prevp.type == token.DOT:
1226 elif p.type == syms.classdef:
1230 if prev and prev.type == token.LPAR:
1233 elif p.type == syms.subscript:
1236 assert p.parent is not None, "subscripts are always parented"
1237 if p.parent.type == syms.subscriptlist:
1245 elif p.type == syms.atom:
1246 if prev and t == token.DOT:
1247 # dots, but not the first one.
1251 p.type == syms.listmaker
1252 or p.type == syms.testlist_gexp
1253 or p.type == syms.subscriptlist
1255 # list interior, including unpacking
1259 elif p.type == syms.dictsetmaker:
1260 # dict and set interior, including unpacking
1264 if prev.type == token.DOUBLESTAR:
1267 elif p.type in {syms.factor, syms.star_expr}:
1270 prevp = preceding_leaf(p)
1271 if not prevp or prevp.type in OPENING_BRACKETS:
1274 prevp_parent = prevp.parent
1275 assert prevp_parent is not None
1276 if prevp.type == token.COLON and prevp_parent.type in {
1277 syms.subscript, syms.sliceop
1281 elif prevp.type == token.EQUAL and prevp_parent.type == syms.argument:
1284 elif t == token.NAME or t == token.NUMBER:
1287 elif p.type == syms.import_from:
1289 if prev and prev.type == token.DOT:
1292 elif t == token.NAME:
1296 if prev and prev.type == token.DOT:
1299 elif p.type == syms.sliceop:
1305 def preceding_leaf(node: Optional[LN]) -> Optional[Leaf]:
1306 """Returns the first leaf that precedes `node`, if any."""
1308 res = node.prev_sibling
1310 if isinstance(res, Leaf):
1314 return list(res.leaves())[-1]
1323 def is_delimiter(leaf: Leaf) -> int:
1324 """Returns the priority of the `leaf` delimiter. Returns 0 if not delimiter.
1326 Higher numbers are higher priority.
1328 if leaf.type == token.COMMA:
1329 return COMMA_PRIORITY
1331 if leaf.type in COMPARATORS:
1332 return COMPARATOR_PRIORITY
1335 leaf.type in MATH_OPERATORS
1337 and leaf.parent.type not in {syms.factor, syms.star_expr}
1339 return MATH_PRIORITY
1344 def generate_comments(leaf: Leaf) -> Iterator[Leaf]:
1345 """Cleans the prefix of the `leaf` and generates comments from it, if any.
1347 Comments in lib2to3 are shoved into the whitespace prefix. This happens
1348 in `pgen2/driver.py:Driver.parse_tokens()`. This was a brilliant implementation
1349 move because it does away with modifying the grammar to include all the
1350 possible places in which comments can be placed.
1352 The sad consequence for us though is that comments don't "belong" anywhere.
1353 This is why this function generates simple parentless Leaf objects for
1354 comments. We simply don't know what the correct parent should be.
1356 No matter though, we can live without this. We really only need to
1357 differentiate between inline and standalone comments. The latter don't
1358 share the line with any code.
1360 Inline comments are emitted as regular token.COMMENT leaves. Standalone
1361 are emitted with a fake STANDALONE_COMMENT token identifier.
1372 for index, line in enumerate(p.split('\n')):
1373 consumed += len(line) + 1 # adding the length of the split '\n'
1374 line = line.lstrip()
1377 if not line.startswith('#'):
1380 if index == 0 and leaf.type != token.ENDMARKER:
1381 comment_type = token.COMMENT # simple trailing comment
1383 comment_type = STANDALONE_COMMENT
1384 comment = make_comment(line)
1385 yield Leaf(comment_type, comment, prefix='\n' * nlines)
1387 if comment in {'# fmt: on', '# yapf: enable'}:
1388 raise FormatOn(consumed)
1390 if comment in {'# fmt: off', '# yapf: disable'}:
1391 raise FormatOff(consumed)
1396 def make_comment(content: str) -> str:
1397 """Returns a consistently formatted comment from the given `content` string.
1399 All comments (except for "##", "#!", "#:") should have a single space between
1400 the hash sign and the content.
1402 If `content` didn't start with a hash sign, one is provided.
1404 content = content.rstrip()
1408 if content[0] == '#':
1409 content = content[1:]
1410 if content and content[0] not in ' !:#':
1411 content = ' ' + content
1412 return '#' + content
1416 line: Line, line_length: int, inner: bool = False, py36: bool = False
1417 ) -> Iterator[Line]:
1418 """Splits a `line` into potentially many lines.
1420 They should fit in the allotted `line_length` but might not be able to.
1421 `inner` signifies that there were a pair of brackets somewhere around the
1422 current `line`, possibly transitively. This means we can fallback to splitting
1423 by delimiters if the LHS/RHS don't yield any results.
1425 If `py36` is True, splitting may generate syntax that is only compatible
1426 with Python 3.6 and later.
1428 if isinstance(line, UnformattedLines):
1432 line_str = str(line).strip('\n')
1433 if len(line_str) <= line_length and '\n' not in line_str:
1438 split_funcs = [left_hand_split]
1439 elif line.inside_brackets:
1440 split_funcs = [delimiter_split]
1441 if '\n' not in line_str:
1442 # Only attempt RHS if we don't have multiline strings or comments
1444 split_funcs.append(right_hand_split)
1446 split_funcs = [right_hand_split]
1447 for split_func in split_funcs:
1448 # We are accumulating lines in `result` because we might want to abort
1449 # mission and return the original line in the end, or attempt a different
1451 result: List[Line] = []
1453 for l in split_func(line, py36=py36):
1454 if str(l).strip('\n') == line_str:
1455 raise CannotSplit("Split function returned an unchanged result")
1458 split_line(l, line_length=line_length, inner=True, py36=py36)
1460 except CannotSplit as cs:
1471 def left_hand_split(line: Line, py36: bool = False) -> Iterator[Line]:
1472 """Splits line into many lines, starting with the first matching bracket pair.
1474 Note: this usually looks weird, only use this for function definitions.
1475 Prefer RHS otherwise.
1477 head = Line(depth=line.depth)
1478 body = Line(depth=line.depth + 1, inside_brackets=True)
1479 tail = Line(depth=line.depth)
1480 tail_leaves: List[Leaf] = []
1481 body_leaves: List[Leaf] = []
1482 head_leaves: List[Leaf] = []
1483 current_leaves = head_leaves
1484 matching_bracket = None
1485 for leaf in line.leaves:
1487 current_leaves is body_leaves
1488 and leaf.type in CLOSING_BRACKETS
1489 and leaf.opening_bracket is matching_bracket
1491 current_leaves = tail_leaves if body_leaves else head_leaves
1492 current_leaves.append(leaf)
1493 if current_leaves is head_leaves:
1494 if leaf.type in OPENING_BRACKETS:
1495 matching_bracket = leaf
1496 current_leaves = body_leaves
1497 # Since body is a new indent level, remove spurious leading whitespace.
1499 normalize_prefix(body_leaves[0], inside_brackets=True)
1500 # Build the new lines.
1501 for result, leaves in (
1502 (head, head_leaves), (body, body_leaves), (tail, tail_leaves)
1505 result.append(leaf, preformatted=True)
1506 comment_after = line.comments.get(id(leaf))
1508 result.append(comment_after, preformatted=True)
1509 bracket_split_succeeded_or_raise(head, body, tail)
1510 for result in (head, body, tail):
1515 def right_hand_split(line: Line, py36: bool = False) -> Iterator[Line]:
1516 """Splits line into many lines, starting with the last matching bracket pair."""
1517 head = Line(depth=line.depth)
1518 body = Line(depth=line.depth + 1, inside_brackets=True)
1519 tail = Line(depth=line.depth)
1520 tail_leaves: List[Leaf] = []
1521 body_leaves: List[Leaf] = []
1522 head_leaves: List[Leaf] = []
1523 current_leaves = tail_leaves
1524 opening_bracket = None
1525 for leaf in reversed(line.leaves):
1526 if current_leaves is body_leaves:
1527 if leaf is opening_bracket:
1528 current_leaves = head_leaves if body_leaves else tail_leaves
1529 current_leaves.append(leaf)
1530 if current_leaves is tail_leaves:
1531 if leaf.type in CLOSING_BRACKETS:
1532 opening_bracket = leaf.opening_bracket
1533 current_leaves = body_leaves
1534 tail_leaves.reverse()
1535 body_leaves.reverse()
1536 head_leaves.reverse()
1537 # Since body is a new indent level, remove spurious leading whitespace.
1539 normalize_prefix(body_leaves[0], inside_brackets=True)
1540 # Build the new lines.
1541 for result, leaves in (
1542 (head, head_leaves), (body, body_leaves), (tail, tail_leaves)
1545 result.append(leaf, preformatted=True)
1546 comment_after = line.comments.get(id(leaf))
1548 result.append(comment_after, preformatted=True)
1549 bracket_split_succeeded_or_raise(head, body, tail)
1550 for result in (head, body, tail):
1555 def bracket_split_succeeded_or_raise(head: Line, body: Line, tail: Line) -> None:
1556 """Raise :exc:`CannotSplit` if the last left- or right-hand split failed.
1558 Do nothing otherwise.
1560 A left- or right-hand split is based on a pair of brackets. Content before
1561 (and including) the opening bracket is left on one line, content inside the
1562 brackets is put on a separate line, and finally content starting with and
1563 following the closing bracket is put on a separate line.
1565 Those are called `head`, `body`, and `tail`, respectively. If the split
1566 produced the same line (all content in `head`) or ended up with an empty `body`
1567 and the `tail` is just the closing bracket, then it's considered failed.
1569 tail_len = len(str(tail).strip())
1572 raise CannotSplit("Splitting brackets produced the same line")
1576 f"Splitting brackets on an empty body to save "
1577 f"{tail_len} characters is not worth it"
1581 def delimiter_split(line: Line, py36: bool = False) -> Iterator[Line]:
1582 """Splits according to delimiters of the highest priority.
1584 This kind of split doesn't increase indentation.
1585 If `py36` is True, the split will add trailing commas also in function
1586 signatures that contain `*` and `**`.
1589 last_leaf = line.leaves[-1]
1591 raise CannotSplit("Line empty")
1593 delimiters = line.bracket_tracker.delimiters
1595 delimiter_priority = line.bracket_tracker.max_delimiter_priority(
1596 exclude={id(last_leaf)}
1599 raise CannotSplit("No delimiters found")
1601 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
1602 lowest_depth = sys.maxsize
1603 trailing_comma_safe = True
1604 for leaf in line.leaves:
1605 current_line.append(leaf, preformatted=True)
1606 comment_after = line.comments.get(id(leaf))
1608 current_line.append(comment_after, preformatted=True)
1609 lowest_depth = min(lowest_depth, leaf.bracket_depth)
1611 leaf.bracket_depth == lowest_depth
1612 and leaf.type == token.STAR
1613 or leaf.type == token.DOUBLESTAR
1615 trailing_comma_safe = trailing_comma_safe and py36
1616 leaf_priority = delimiters.get(id(leaf))
1617 if leaf_priority == delimiter_priority:
1618 normalize_prefix(current_line.leaves[0], inside_brackets=True)
1621 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
1624 delimiter_priority == COMMA_PRIORITY
1625 and current_line.leaves[-1].type != token.COMMA
1626 and trailing_comma_safe
1628 current_line.append(Leaf(token.COMMA, ','))
1629 normalize_prefix(current_line.leaves[0], inside_brackets=True)
1633 def is_import(leaf: Leaf) -> bool:
1634 """Returns True if the given leaf starts an import statement."""
1641 (v == 'import' and p and p.type == syms.import_name)
1642 or (v == 'from' and p and p.type == syms.import_from)
1647 def normalize_prefix(leaf: Leaf, *, inside_brackets: bool) -> None:
1648 """Leaves existing extra newlines if not `inside_brackets`.
1650 Removes everything else. Note: don't use backslashes for formatting or
1651 you'll lose your voting rights.
1653 if not inside_brackets:
1654 spl = leaf.prefix.split('#')
1655 if '\\' not in spl[0]:
1656 nl_count = spl[-1].count('\n')
1659 leaf.prefix = '\n' * nl_count
1665 def is_python36(node: Node) -> bool:
1666 """Returns True if the current file is using Python 3.6+ features.
1668 Currently looking for:
1670 - trailing commas after * or ** in function signatures.
1672 for n in node.pre_order():
1673 if n.type == token.STRING:
1674 value_head = n.value[:2] # type: ignore
1675 if value_head in {'f"', 'F"', "f'", "F'", 'rf', 'fr', 'RF', 'FR'}:
1679 n.type == syms.typedargslist
1681 and n.children[-1].type == token.COMMA
1683 for ch in n.children:
1684 if ch.type == token.STAR or ch.type == token.DOUBLESTAR:
1690 PYTHON_EXTENSIONS = {'.py'}
1691 BLACKLISTED_DIRECTORIES = {
1692 'build', 'buck-out', 'dist', '_build', '.git', '.hg', '.mypy_cache', '.tox', '.venv'
1696 def gen_python_files_in_dir(path: Path) -> Iterator[Path]:
1697 """Generates all files under `path` which aren't under BLACKLISTED_DIRECTORIES
1698 and have one of the PYTHON_EXTENSIONS.
1700 for child in path.iterdir():
1702 if child.name in BLACKLISTED_DIRECTORIES:
1705 yield from gen_python_files_in_dir(child)
1707 elif child.suffix in PYTHON_EXTENSIONS:
1713 """Provides a reformatting counter. Can be rendered with `str(report)`."""
1715 change_count: int = 0
1717 failure_count: int = 0
1719 def done(self, src: Path, changed: bool) -> None:
1720 """Increment the counter for successful reformatting. Write out a message."""
1722 reformatted = 'would reformat' if self.check else 'reformatted'
1723 out(f'{reformatted} {src}')
1724 self.change_count += 1
1726 out(f'{src} already well formatted, good job.', bold=False)
1727 self.same_count += 1
1729 def failed(self, src: Path, message: str) -> None:
1730 """Increment the counter for failed reformatting. Write out a message."""
1731 err(f'error: cannot format {src}: {message}')
1732 self.failure_count += 1
1735 def return_code(self) -> int:
1736 """Which return code should the app use considering the current state."""
1737 # According to http://tldp.org/LDP/abs/html/exitcodes.html starting with
1738 # 126 we have special returncodes reserved by the shell.
1739 if self.failure_count:
1742 elif self.change_count and self.check:
1747 def __str__(self) -> str:
1748 """A color report of the current state.
1750 Use `click.unstyle` to remove colors.
1753 reformatted = "would be reformatted"
1754 unchanged = "would be left unchanged"
1755 failed = "would fail to reformat"
1757 reformatted = "reformatted"
1758 unchanged = "left unchanged"
1759 failed = "failed to reformat"
1761 if self.change_count:
1762 s = 's' if self.change_count > 1 else ''
1764 click.style(f'{self.change_count} file{s} {reformatted}', bold=True)
1767 s = 's' if self.same_count > 1 else ''
1768 report.append(f'{self.same_count} file{s} {unchanged}')
1769 if self.failure_count:
1770 s = 's' if self.failure_count > 1 else ''
1772 click.style(f'{self.failure_count} file{s} {failed}', fg='red')
1774 return ', '.join(report) + '.'
1777 def assert_equivalent(src: str, dst: str) -> None:
1778 """Raises AssertionError if `src` and `dst` aren't equivalent.
1780 This is a temporary sanity check until Black becomes stable.
1786 def _v(node: ast.AST, depth: int = 0) -> Iterator[str]:
1787 """Simple visitor generating strings to compare ASTs by content."""
1788 yield f"{' ' * depth}{node.__class__.__name__}("
1790 for field in sorted(node._fields):
1792 value = getattr(node, field)
1793 except AttributeError:
1796 yield f"{' ' * (depth+1)}{field}="
1798 if isinstance(value, list):
1800 if isinstance(item, ast.AST):
1801 yield from _v(item, depth + 2)
1803 elif isinstance(value, ast.AST):
1804 yield from _v(value, depth + 2)
1807 yield f"{' ' * (depth+2)}{value!r}, # {value.__class__.__name__}"
1809 yield f"{' ' * depth}) # /{node.__class__.__name__}"
1812 src_ast = ast.parse(src)
1813 except Exception as exc:
1814 major, minor = sys.version_info[:2]
1815 raise AssertionError(
1816 f"cannot use --safe with this file; failed to parse source file "
1817 f"with Python {major}.{minor}'s builtin AST. Re-run with --fast "
1818 f"or stop using deprecated Python 2 syntax. AST error message: {exc}"
1822 dst_ast = ast.parse(dst)
1823 except Exception as exc:
1824 log = dump_to_file(''.join(traceback.format_tb(exc.__traceback__)), dst)
1825 raise AssertionError(
1826 f"INTERNAL ERROR: Black produced invalid code: {exc}. "
1827 f"Please report a bug on https://github.com/ambv/black/issues. "
1828 f"This invalid output might be helpful: {log}"
1831 src_ast_str = '\n'.join(_v(src_ast))
1832 dst_ast_str = '\n'.join(_v(dst_ast))
1833 if src_ast_str != dst_ast_str:
1834 log = dump_to_file(diff(src_ast_str, dst_ast_str, 'src', 'dst'))
1835 raise AssertionError(
1836 f"INTERNAL ERROR: Black produced code that is not equivalent to "
1838 f"Please report a bug on https://github.com/ambv/black/issues. "
1839 f"This diff might be helpful: {log}"
1843 def assert_stable(src: str, dst: str, line_length: int) -> None:
1844 """Raises AssertionError if `dst` reformats differently the second time.
1846 This is a temporary sanity check until Black becomes stable.
1848 newdst = format_str(dst, line_length=line_length)
1851 diff(src, dst, 'source', 'first pass'),
1852 diff(dst, newdst, 'first pass', 'second pass'),
1854 raise AssertionError(
1855 f"INTERNAL ERROR: Black produced different code on the second pass "
1856 f"of the formatter. "
1857 f"Please report a bug on https://github.com/ambv/black/issues. "
1858 f"This diff might be helpful: {log}"
1862 def dump_to_file(*output: str) -> str:
1863 """Dumps `output` to a temporary file. Returns path to the file."""
1866 with tempfile.NamedTemporaryFile(
1867 mode='w', prefix='blk_', suffix='.log', delete=False
1869 for lines in output:
1875 def diff(a: str, b: str, a_name: str, b_name: str) -> str:
1876 """Returns a udiff string between strings `a` and `b`."""
1879 a_lines = [line + '\n' for line in a.split('\n')]
1880 b_lines = [line + '\n' for line in b.split('\n')]
1882 difflib.unified_diff(a_lines, b_lines, fromfile=a_name, tofile=b_name, n=5)
1886 if __name__ == '__main__':