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
7 from functools import partial, wraps
10 from multiprocessing import Manager
12 from pathlib import Path
32 from attr import dataclass, Factory
36 from blib2to3.pytree import Node, Leaf, type_repr
37 from blib2to3 import pygram, pytree
38 from blib2to3.pgen2 import driver, token
39 from blib2to3.pgen2.parse import ParseError
41 __version__ = "18.4a0"
42 DEFAULT_LINE_LENGTH = 88
44 syms = pygram.python_symbols
52 LN = Union[Leaf, Node]
53 SplitFunc = Callable[["Line", bool], Iterator["Line"]]
54 out = partial(click.secho, bold=True, err=True)
55 err = partial(click.secho, fg="red", err=True)
58 class NothingChanged(UserWarning):
59 """Raised by :func:`format_file` when reformatted code is the same as source."""
62 class CannotSplit(Exception):
63 """A readable split that fits the allotted line length is impossible.
65 Raised by :func:`left_hand_split`, :func:`right_hand_split`, and
66 :func:`delimiter_split`.
70 class FormatError(Exception):
71 """Base exception for `# fmt: on` and `# fmt: off` handling.
73 It holds the number of bytes of the prefix consumed before the format
74 control comment appeared.
77 def __init__(self, consumed: int) -> None:
78 super().__init__(consumed)
79 self.consumed = consumed
81 def trim_prefix(self, leaf: Leaf) -> None:
82 leaf.prefix = leaf.prefix[self.consumed:]
84 def leaf_from_consumed(self, leaf: Leaf) -> Leaf:
85 """Returns a new Leaf from the consumed part of the prefix."""
86 unformatted_prefix = leaf.prefix[:self.consumed]
87 return Leaf(token.NEWLINE, unformatted_prefix)
90 class FormatOn(FormatError):
91 """Found a comment like `# fmt: on` in the file."""
94 class FormatOff(FormatError):
95 """Found a comment like `# fmt: off` in the file."""
98 class WriteBack(Enum):
109 default=DEFAULT_LINE_LENGTH,
110 help="How many character per line to allow.",
117 "Don't write the files back, just return the status. Return code 0 "
118 "means nothing would change. Return code 1 means some files would be "
119 "reformatted. Return code 123 means there was an internal error."
125 help="Don't write the files back, just output a diff for each file on stdout.",
130 help="If --fast given, skip temporary sanity checks. [default: --safe]",
137 "Don't emit non-error messages to stderr. Errors are still emitted, "
138 "silence those with 2>/dev/null."
141 @click.version_option(version=__version__)
146 exists=True, file_okay=True, dir_okay=True, readable=True, allow_dash=True
159 """The uncompromising code formatter."""
160 sources: List[Path] = []
164 sources.extend(gen_python_files_in_dir(p))
166 # if a file was explicitly given, we don't care about its extension
169 sources.append(Path("-"))
171 err(f"invalid path: {s}")
173 exc = click.ClickException("Options --check and --diff are mutually exclusive")
178 write_back = WriteBack.NO
180 write_back = WriteBack.DIFF
182 write_back = WriteBack.YES
183 if len(sources) == 0:
185 elif len(sources) == 1:
187 report = Report(check=check, quiet=quiet)
189 if not p.is_file() and str(p) == "-":
190 changed = format_stdin_to_stdout(
191 line_length=line_length, fast=fast, write_back=write_back
194 changed = format_file_in_place(
195 p, line_length=line_length, fast=fast, write_back=write_back
197 report.done(p, changed)
198 except Exception as exc:
199 report.failed(p, str(exc))
200 ctx.exit(report.return_code)
202 loop = asyncio.get_event_loop()
203 executor = ProcessPoolExecutor(max_workers=os.cpu_count())
206 return_code = loop.run_until_complete(
208 sources, line_length, write_back, fast, quiet, loop, executor
213 ctx.exit(return_code)
216 async def schedule_formatting(
219 write_back: WriteBack,
225 """Run formatting of `sources` in parallel using the provided `executor`.
227 (Use ProcessPoolExecutors for actual parallelism.)
229 `line_length`, `write_back`, and `fast` options are passed to
230 :func:`format_file_in_place`.
233 if write_back == WriteBack.DIFF:
234 # For diff output, we need locks to ensure we don't interleave output
235 # from different processes.
237 lock = manager.Lock()
239 src: loop.run_in_executor(
240 executor, format_file_in_place, src, line_length, fast, write_back, lock
244 _task_values = list(tasks.values())
245 loop.add_signal_handler(signal.SIGINT, cancel, _task_values)
246 loop.add_signal_handler(signal.SIGTERM, cancel, _task_values)
247 await asyncio.wait(tasks.values())
249 report = Report(check=write_back is WriteBack.NO, quiet=quiet)
250 for src, task in tasks.items():
252 report.failed(src, "timed out, cancelling")
254 cancelled.append(task)
255 elif task.cancelled():
256 cancelled.append(task)
257 elif task.exception():
258 report.failed(src, str(task.exception()))
260 report.done(src, task.result())
262 await asyncio.gather(*cancelled, loop=loop, return_exceptions=True)
264 out("All done! ✨ 🍰 ✨")
266 click.echo(str(report))
267 return report.return_code
270 def format_file_in_place(
274 write_back: WriteBack = WriteBack.NO,
275 lock: Any = None, # multiprocessing.Manager().Lock() is some crazy proxy
277 """Format file under `src` path. Return True if changed.
279 If `write_back` is True, write reformatted code back to stdout.
280 `line_length` and `fast` options are passed to :func:`format_file_contents`.
282 with tokenize.open(src) as src_buffer:
283 src_contents = src_buffer.read()
285 dst_contents = format_file_contents(
286 src_contents, line_length=line_length, fast=fast
288 except NothingChanged:
291 if write_back == write_back.YES:
292 with open(src, "w", encoding=src_buffer.encoding) as f:
293 f.write(dst_contents)
294 elif write_back == write_back.DIFF:
295 src_name = f"{src.name} (original)"
296 dst_name = f"{src.name} (formatted)"
297 diff_contents = diff(src_contents, dst_contents, src_name, dst_name)
301 sys.stdout.write(diff_contents)
308 def format_stdin_to_stdout(
309 line_length: int, fast: bool, write_back: WriteBack = WriteBack.NO
311 """Format file on stdin. Return True if changed.
313 If `write_back` is True, write reformatted code back to stdout.
314 `line_length` and `fast` arguments are passed to :func:`format_file_contents`.
316 src = sys.stdin.read()
318 dst = format_file_contents(src, line_length=line_length, fast=fast)
321 except NothingChanged:
326 if write_back == WriteBack.YES:
327 sys.stdout.write(dst)
328 elif write_back == WriteBack.DIFF:
329 src_name = "<stdin> (original)"
330 dst_name = "<stdin> (formatted)"
331 sys.stdout.write(diff(src, dst, src_name, dst_name))
334 def format_file_contents(
335 src_contents: str, line_length: int, fast: bool
337 """Reformat contents a file and return new contents.
339 If `fast` is False, additionally confirm that the reformatted code is
340 valid by calling :func:`assert_equivalent` and :func:`assert_stable` on it.
341 `line_length` is passed to :func:`format_str`.
343 if src_contents.strip() == "":
346 dst_contents = format_str(src_contents, line_length=line_length)
347 if src_contents == dst_contents:
351 assert_equivalent(src_contents, dst_contents)
352 assert_stable(src_contents, dst_contents, line_length=line_length)
356 def format_str(src_contents: str, line_length: int) -> FileContent:
357 """Reformat a string and return new contents.
359 `line_length` determines how many characters per line are allowed.
361 src_node = lib2to3_parse(src_contents)
363 lines = LineGenerator()
364 elt = EmptyLineTracker()
365 py36 = is_python36(src_node)
368 for current_line in lines.visit(src_node):
369 for _ in range(after):
370 dst_contents += str(empty_line)
371 before, after = elt.maybe_empty_lines(current_line)
372 for _ in range(before):
373 dst_contents += str(empty_line)
374 for line in split_line(current_line, line_length=line_length, py36=py36):
375 dst_contents += str(line)
380 pygram.python_grammar_no_print_statement_no_exec_statement,
381 pygram.python_grammar_no_print_statement,
382 pygram.python_grammar_no_exec_statement,
383 pygram.python_grammar,
387 def lib2to3_parse(src_txt: str) -> Node:
388 """Given a string with source, return the lib2to3 Node."""
389 grammar = pygram.python_grammar_no_print_statement
390 if src_txt[-1] != "\n":
391 nl = "\r\n" if "\r\n" in src_txt[:1024] else "\n"
393 for grammar in GRAMMARS:
394 drv = driver.Driver(grammar, pytree.convert)
396 result = drv.parse_string(src_txt, True)
399 except ParseError as pe:
400 lineno, column = pe.context[1]
401 lines = src_txt.splitlines()
403 faulty_line = lines[lineno - 1]
405 faulty_line = "<line number missing in source>"
406 exc = ValueError(f"Cannot parse: {lineno}:{column}: {faulty_line}")
410 if isinstance(result, Leaf):
411 result = Node(syms.file_input, [result])
415 def lib2to3_unparse(node: Node) -> str:
416 """Given a lib2to3 node, return its string representation."""
424 class Visitor(Generic[T]):
425 """Basic lib2to3 visitor that yields things of type `T` on `visit()`."""
427 def visit(self, node: LN) -> Iterator[T]:
428 """Main method to visit `node` and its children.
430 It tries to find a `visit_*()` method for the given `node.type`, like
431 `visit_simple_stmt` for Node objects or `visit_INDENT` for Leaf objects.
432 If no dedicated `visit_*()` method is found, chooses `visit_default()`
435 Then yields objects of type `T` from the selected visitor.
438 name = token.tok_name[node.type]
440 name = type_repr(node.type)
441 yield from getattr(self, f"visit_{name}", self.visit_default)(node)
443 def visit_default(self, node: LN) -> Iterator[T]:
444 """Default `visit_*()` implementation. Recurses to children of `node`."""
445 if isinstance(node, Node):
446 for child in node.children:
447 yield from self.visit(child)
451 class DebugVisitor(Visitor[T]):
454 def visit_default(self, node: LN) -> Iterator[T]:
455 indent = " " * (2 * self.tree_depth)
456 if isinstance(node, Node):
457 _type = type_repr(node.type)
458 out(f"{indent}{_type}", fg="yellow")
460 for child in node.children:
461 yield from self.visit(child)
464 out(f"{indent}/{_type}", fg="yellow", bold=False)
466 _type = token.tok_name.get(node.type, str(node.type))
467 out(f"{indent}{_type}", fg="blue", nl=False)
469 # We don't have to handle prefixes for `Node` objects since
470 # that delegates to the first child anyway.
471 out(f" {node.prefix!r}", fg="green", bold=False, nl=False)
472 out(f" {node.value!r}", fg="blue", bold=False)
475 def show(cls, code: str) -> None:
476 """Pretty-print the lib2to3 AST of a given string of `code`.
478 Convenience method for debugging.
480 v: DebugVisitor[None] = DebugVisitor()
481 list(v.visit(lib2to3_parse(code)))
484 KEYWORDS = set(keyword.kwlist)
485 WHITESPACE = {token.DEDENT, token.INDENT, token.NEWLINE}
486 FLOW_CONTROL = {"return", "raise", "break", "continue"}
497 STANDALONE_COMMENT = 153
498 LOGIC_OPERATORS = {"and", "or"}
522 VARARGS = {token.STAR, token.DOUBLESTAR}
523 COMPREHENSION_PRIORITY = 20
527 COMPARATOR_PRIORITY = 3
532 class BracketTracker:
533 """Keeps track of brackets on a line."""
536 bracket_match: Dict[Tuple[Depth, NodeType], Leaf] = Factory(dict)
537 delimiters: Dict[LeafID, Priority] = Factory(dict)
538 previous: Optional[Leaf] = None
540 def mark(self, leaf: Leaf) -> None:
541 """Mark `leaf` with bracket-related metadata. Keep track of delimiters.
543 All leaves receive an int `bracket_depth` field that stores how deep
544 within brackets a given leaf is. 0 means there are no enclosing brackets
545 that started on this line.
547 If a leaf is itself a closing bracket, it receives an `opening_bracket`
548 field that it forms a pair with. This is a one-directional link to
549 avoid reference cycles.
551 If a leaf is a delimiter (a token on which Black can split the line if
552 needed) and it's on depth 0, its `id()` is stored in the tracker's
555 if leaf.type == token.COMMENT:
558 if leaf.type in CLOSING_BRACKETS:
560 opening_bracket = self.bracket_match.pop((self.depth, leaf.type))
561 leaf.opening_bracket = opening_bracket
562 leaf.bracket_depth = self.depth
564 delim = is_split_before_delimiter(leaf, self.previous)
565 if delim and self.previous is not None:
566 self.delimiters[id(self.previous)] = delim
568 delim = is_split_after_delimiter(leaf, self.previous)
570 self.delimiters[id(leaf)] = delim
571 if leaf.type in OPENING_BRACKETS:
572 self.bracket_match[self.depth, BRACKET[leaf.type]] = leaf
576 def any_open_brackets(self) -> bool:
577 """Return True if there is an yet unmatched open bracket on the line."""
578 return bool(self.bracket_match)
580 def max_delimiter_priority(self, exclude: Iterable[LeafID] = ()) -> int:
581 """Return the highest priority of a delimiter found on the line.
583 Values are consistent with what `is_delimiter()` returns.
585 return max(v for k, v in self.delimiters.items() if k not in exclude)
590 """Holds leaves and comments. Can be printed with `str(line)`."""
593 leaves: List[Leaf] = Factory(list)
594 comments: List[Tuple[Index, Leaf]] = Factory(list)
595 bracket_tracker: BracketTracker = Factory(BracketTracker)
596 inside_brackets: bool = False
597 has_for: bool = False
598 _for_loop_variable: bool = False
600 def append(self, leaf: Leaf, preformatted: bool = False) -> None:
601 """Add a new `leaf` to the end of the line.
603 Unless `preformatted` is True, the `leaf` will receive a new consistent
604 whitespace prefix and metadata applied by :class:`BracketTracker`.
605 Trailing commas are maybe removed, unpacked for loop variables are
606 demoted from being delimiters.
608 Inline comments are put aside.
610 has_value = leaf.value.strip()
614 if self.leaves and not preformatted:
615 # Note: at this point leaf.prefix should be empty except for
616 # imports, for which we only preserve newlines.
617 leaf.prefix += whitespace(leaf)
618 if self.inside_brackets or not preformatted:
619 self.maybe_decrement_after_for_loop_variable(leaf)
620 self.bracket_tracker.mark(leaf)
621 self.maybe_remove_trailing_comma(leaf)
622 self.maybe_increment_for_loop_variable(leaf)
624 if not self.append_comment(leaf):
625 self.leaves.append(leaf)
627 def append_safe(self, leaf: Leaf, preformatted: bool = False) -> None:
628 """Like :func:`append()` but disallow invalid standalone comment structure.
630 Raises ValueError when any `leaf` is appended after a standalone comment
631 or when a standalone comment is not the first leaf on the line.
633 if self.bracket_tracker.depth == 0:
635 raise ValueError("cannot append to standalone comments")
637 if self.leaves and leaf.type == STANDALONE_COMMENT:
639 "cannot append standalone comments to a populated line"
642 self.append(leaf, preformatted=preformatted)
645 def is_comment(self) -> bool:
646 """Is this line a standalone comment?"""
647 return len(self.leaves) == 1 and self.leaves[0].type == STANDALONE_COMMENT
650 def is_decorator(self) -> bool:
651 """Is this line a decorator?"""
652 return bool(self) and self.leaves[0].type == token.AT
655 def is_import(self) -> bool:
656 """Is this an import line?"""
657 return bool(self) and is_import(self.leaves[0])
660 def is_class(self) -> bool:
661 """Is this line a class definition?"""
664 and self.leaves[0].type == token.NAME
665 and self.leaves[0].value == "class"
669 def is_def(self) -> bool:
670 """Is this a function definition? (Also returns True for async defs.)"""
672 first_leaf = self.leaves[0]
677 second_leaf: Optional[Leaf] = self.leaves[1]
681 (first_leaf.type == token.NAME and first_leaf.value == "def")
683 first_leaf.type == token.ASYNC
684 and second_leaf is not None
685 and second_leaf.type == token.NAME
686 and second_leaf.value == "def"
691 def is_flow_control(self) -> bool:
692 """Is this line a flow control statement?
694 Those are `return`, `raise`, `break`, and `continue`.
698 and self.leaves[0].type == token.NAME
699 and self.leaves[0].value in FLOW_CONTROL
703 def is_yield(self) -> bool:
704 """Is this line a yield statement?"""
707 and self.leaves[0].type == token.NAME
708 and self.leaves[0].value == "yield"
712 def contains_standalone_comments(self) -> bool:
713 """If so, needs to be split before emitting."""
714 for leaf in self.leaves:
715 if leaf.type == STANDALONE_COMMENT:
720 def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
721 """Remove trailing comma if there is one and it's safe."""
724 and self.leaves[-1].type == token.COMMA
725 and closing.type in CLOSING_BRACKETS
729 if closing.type == token.RBRACE:
730 self.remove_trailing_comma()
733 if closing.type == token.RSQB:
734 comma = self.leaves[-1]
735 if comma.parent and comma.parent.type == syms.listmaker:
736 self.remove_trailing_comma()
739 # For parens let's check if it's safe to remove the comma. If the
740 # trailing one is the only one, we might mistakenly change a tuple
741 # into a different type by removing the comma.
742 depth = closing.bracket_depth + 1
744 opening = closing.opening_bracket
745 for _opening_index, leaf in enumerate(self.leaves):
752 for leaf in self.leaves[_opening_index + 1:]:
756 bracket_depth = leaf.bracket_depth
757 if bracket_depth == depth and leaf.type == token.COMMA:
759 if leaf.parent and leaf.parent.type == syms.arglist:
764 self.remove_trailing_comma()
769 def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
770 """In a for loop, or comprehension, the variables are often unpacks.
772 To avoid splitting on the comma in this situation, increase the depth of
773 tokens between `for` and `in`.
775 if leaf.type == token.NAME and leaf.value == "for":
777 self.bracket_tracker.depth += 1
778 self._for_loop_variable = True
783 def maybe_decrement_after_for_loop_variable(self, leaf: Leaf) -> bool:
784 """See `maybe_increment_for_loop_variable` above for explanation."""
785 if self._for_loop_variable and leaf.type == token.NAME and leaf.value == "in":
786 self.bracket_tracker.depth -= 1
787 self._for_loop_variable = False
792 def append_comment(self, comment: Leaf) -> bool:
793 """Add an inline or standalone comment to the line."""
795 comment.type == STANDALONE_COMMENT
796 and self.bracket_tracker.any_open_brackets()
801 if comment.type != token.COMMENT:
804 after = len(self.leaves) - 1
806 comment.type = STANDALONE_COMMENT
811 self.comments.append((after, comment))
814 def comments_after(self, leaf: Leaf) -> Iterator[Leaf]:
815 """Generate comments that should appear directly after `leaf`."""
816 for _leaf_index, _leaf in enumerate(self.leaves):
823 for index, comment_after in self.comments:
824 if _leaf_index == index:
827 def remove_trailing_comma(self) -> None:
828 """Remove the trailing comma and moves the comments attached to it."""
829 comma_index = len(self.leaves) - 1
830 for i in range(len(self.comments)):
831 comment_index, comment = self.comments[i]
832 if comment_index == comma_index:
833 self.comments[i] = (comma_index - 1, comment)
836 def __str__(self) -> str:
837 """Render the line."""
841 indent = " " * self.depth
842 leaves = iter(self.leaves)
844 res = f"{first.prefix}{indent}{first.value}"
847 for _, comment in self.comments:
851 def __bool__(self) -> bool:
852 """Return True if the line has leaves or comments."""
853 return bool(self.leaves or self.comments)
856 class UnformattedLines(Line):
857 """Just like :class:`Line` but stores lines which aren't reformatted."""
859 def append(self, leaf: Leaf, preformatted: bool = True) -> None:
860 """Just add a new `leaf` to the end of the lines.
862 The `preformatted` argument is ignored.
864 Keeps track of indentation `depth`, which is useful when the user
865 says `# fmt: on`. Otherwise, doesn't do anything with the `leaf`.
868 list(generate_comments(leaf))
869 except FormatOn as f_on:
870 self.leaves.append(f_on.leaf_from_consumed(leaf))
873 self.leaves.append(leaf)
874 if leaf.type == token.INDENT:
876 elif leaf.type == token.DEDENT:
879 def __str__(self) -> str:
880 """Render unformatted lines from leaves which were added with `append()`.
882 `depth` is not used for indentation in this case.
888 for leaf in self.leaves:
892 def append_comment(self, comment: Leaf) -> bool:
893 """Not implemented in this class. Raises `NotImplementedError`."""
894 raise NotImplementedError("Unformatted lines don't store comments separately.")
896 def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
897 """Does nothing and returns False."""
900 def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
901 """Does nothing and returns False."""
906 class EmptyLineTracker:
907 """Provides a stateful method that returns the number of potential extra
908 empty lines needed before and after the currently processed line.
910 Note: this tracker works on lines that haven't been split yet. It assumes
911 the prefix of the first leaf consists of optional newlines. Those newlines
912 are consumed by `maybe_empty_lines()` and included in the computation.
914 previous_line: Optional[Line] = None
915 previous_after: int = 0
916 previous_defs: List[int] = Factory(list)
918 def maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
919 """Return the number of extra empty lines before and after the `current_line`.
921 This is for separating `def`, `async def` and `class` with extra empty
922 lines (two on module-level), as well as providing an extra empty line
923 after flow control keywords to make them more prominent.
925 if isinstance(current_line, UnformattedLines):
928 before, after = self._maybe_empty_lines(current_line)
929 before -= self.previous_after
930 self.previous_after = after
931 self.previous_line = current_line
934 def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
936 if current_line.depth == 0:
938 if current_line.leaves:
939 # Consume the first leaf's extra newlines.
940 first_leaf = current_line.leaves[0]
941 before = first_leaf.prefix.count("\n")
942 before = min(before, max_allowed)
943 first_leaf.prefix = ""
946 depth = current_line.depth
947 while self.previous_defs and self.previous_defs[-1] >= depth:
948 self.previous_defs.pop()
949 before = 1 if depth else 2
950 is_decorator = current_line.is_decorator
951 if is_decorator or current_line.is_def or current_line.is_class:
953 self.previous_defs.append(depth)
954 if self.previous_line is None:
955 # Don't insert empty lines before the first line in the file.
958 if self.previous_line and self.previous_line.is_decorator:
959 # Don't insert empty lines between decorators.
963 if current_line.depth:
967 if current_line.is_flow_control:
972 and self.previous_line.is_import
973 and not current_line.is_import
974 and depth == self.previous_line.depth
976 return (before or 1), 0
980 and self.previous_line.is_yield
981 and (not current_line.is_yield or depth != self.previous_line.depth)
983 return (before or 1), 0
989 class LineGenerator(Visitor[Line]):
990 """Generates reformatted Line objects. Empty lines are not emitted.
992 Note: destroys the tree it's visiting by mutating prefixes of its leaves
993 in ways that will no longer stringify to valid Python code on the tree.
995 current_line: Line = Factory(Line)
997 def line(self, indent: int = 0, type: Type[Line] = Line) -> Iterator[Line]:
1000 If the line is empty, only emit if it makes sense.
1001 If the line is too long, split it first and then generate.
1003 If any lines were generated, set up a new current_line.
1005 if not self.current_line:
1006 if self.current_line.__class__ == type:
1007 self.current_line.depth += indent
1009 self.current_line = type(depth=self.current_line.depth + indent)
1010 return # Line is empty, don't emit. Creating a new one unnecessary.
1012 complete_line = self.current_line
1013 self.current_line = type(depth=complete_line.depth + indent)
1016 def visit(self, node: LN) -> Iterator[Line]:
1017 """Main method to visit `node` and its children.
1019 Yields :class:`Line` objects.
1021 if isinstance(self.current_line, UnformattedLines):
1022 # File contained `# fmt: off`
1023 yield from self.visit_unformatted(node)
1026 yield from super().visit(node)
1028 def visit_default(self, node: LN) -> Iterator[Line]:
1029 """Default `visit_*()` implementation. Recurses to children of `node`."""
1030 if isinstance(node, Leaf):
1031 any_open_brackets = self.current_line.bracket_tracker.any_open_brackets()
1033 for comment in generate_comments(node):
1034 if any_open_brackets:
1035 # any comment within brackets is subject to splitting
1036 self.current_line.append(comment)
1037 elif comment.type == token.COMMENT:
1038 # regular trailing comment
1039 self.current_line.append(comment)
1040 yield from self.line()
1043 # regular standalone comment
1044 yield from self.line()
1046 self.current_line.append(comment)
1047 yield from self.line()
1049 except FormatOff as f_off:
1050 f_off.trim_prefix(node)
1051 yield from self.line(type=UnformattedLines)
1052 yield from self.visit(node)
1054 except FormatOn as f_on:
1055 # This only happens here if somebody says "fmt: on" multiple
1057 f_on.trim_prefix(node)
1058 yield from self.visit_default(node)
1061 normalize_prefix(node, inside_brackets=any_open_brackets)
1062 if node.type == token.STRING:
1063 normalize_string_quotes(node)
1064 if node.type not in WHITESPACE:
1065 self.current_line.append(node)
1066 yield from super().visit_default(node)
1068 def visit_INDENT(self, node: Node) -> Iterator[Line]:
1069 """Increase indentation level, maybe yield a line."""
1070 # In blib2to3 INDENT never holds comments.
1071 yield from self.line(+1)
1072 yield from self.visit_default(node)
1074 def visit_DEDENT(self, node: Node) -> Iterator[Line]:
1075 """Decrease indentation level, maybe yield a line."""
1076 # DEDENT has no value. Additionally, in blib2to3 it never holds comments.
1077 yield from self.line(-1)
1079 def visit_stmt(self, node: Node, keywords: Set[str]) -> Iterator[Line]:
1080 """Visit a statement.
1082 This implementation is shared for `if`, `while`, `for`, `try`, `except`,
1083 `def`, `with`, and `class`.
1085 The relevant Python language `keywords` for a given statement will be NAME
1086 leaves within it. This methods puts those on a separate line.
1088 for child in node.children:
1089 if child.type == token.NAME and child.value in keywords: # type: ignore
1090 yield from self.line()
1092 yield from self.visit(child)
1094 def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
1095 """Visit a statement without nested statements."""
1096 is_suite_like = node.parent and node.parent.type in STATEMENT
1098 yield from self.line(+1)
1099 yield from self.visit_default(node)
1100 yield from self.line(-1)
1103 yield from self.line()
1104 yield from self.visit_default(node)
1106 def visit_async_stmt(self, node: Node) -> Iterator[Line]:
1107 """Visit `async def`, `async for`, `async with`."""
1108 yield from self.line()
1110 children = iter(node.children)
1111 for child in children:
1112 yield from self.visit(child)
1114 if child.type == token.ASYNC:
1117 internal_stmt = next(children)
1118 for child in internal_stmt.children:
1119 yield from self.visit(child)
1121 def visit_decorators(self, node: Node) -> Iterator[Line]:
1122 """Visit decorators."""
1123 for child in node.children:
1124 yield from self.line()
1125 yield from self.visit(child)
1127 def visit_SEMI(self, leaf: Leaf) -> Iterator[Line]:
1128 """Remove a semicolon and put the other statement on a separate line."""
1129 yield from self.line()
1131 def visit_ENDMARKER(self, leaf: Leaf) -> Iterator[Line]:
1132 """End of file. Process outstanding comments and end with a newline."""
1133 yield from self.visit_default(leaf)
1134 yield from self.line()
1136 def visit_unformatted(self, node: LN) -> Iterator[Line]:
1137 """Used when file contained a `# fmt: off`."""
1138 if isinstance(node, Node):
1139 for child in node.children:
1140 yield from self.visit(child)
1144 self.current_line.append(node)
1145 except FormatOn as f_on:
1146 f_on.trim_prefix(node)
1147 yield from self.line()
1148 yield from self.visit(node)
1150 if node.type == token.ENDMARKER:
1151 # somebody decided not to put a final `# fmt: on`
1152 yield from self.line()
1154 def __attrs_post_init__(self) -> None:
1155 """You are in a twisty little maze of passages."""
1157 self.visit_if_stmt = partial(v, keywords={"if", "else", "elif"})
1158 self.visit_while_stmt = partial(v, keywords={"while", "else"})
1159 self.visit_for_stmt = partial(v, keywords={"for", "else"})
1160 self.visit_try_stmt = partial(v, keywords={"try", "except", "else", "finally"})
1161 self.visit_except_clause = partial(v, keywords={"except"})
1162 self.visit_funcdef = partial(v, keywords={"def"})
1163 self.visit_with_stmt = partial(v, keywords={"with"})
1164 self.visit_classdef = partial(v, keywords={"class"})
1165 self.visit_async_funcdef = self.visit_async_stmt
1166 self.visit_decorated = self.visit_decorators
1169 BRACKET = {token.LPAR: token.RPAR, token.LSQB: token.RSQB, token.LBRACE: token.RBRACE}
1170 OPENING_BRACKETS = set(BRACKET.keys())
1171 CLOSING_BRACKETS = set(BRACKET.values())
1172 BRACKETS = OPENING_BRACKETS | CLOSING_BRACKETS
1173 ALWAYS_NO_SPACE = CLOSING_BRACKETS | {token.COMMA, STANDALONE_COMMENT}
1176 def whitespace(leaf: Leaf) -> str: # noqa C901
1177 """Return whitespace prefix if needed for the given `leaf`."""
1184 if t in ALWAYS_NO_SPACE:
1187 if t == token.COMMENT:
1190 assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
1191 if t == token.COLON and p.type not in {syms.subscript, syms.subscriptlist}:
1194 prev = leaf.prev_sibling
1196 prevp = preceding_leaf(p)
1197 if not prevp or prevp.type in OPENING_BRACKETS:
1200 if t == token.COLON:
1201 return SPACE if prevp.type == token.COMMA else NO
1203 if prevp.type == token.EQUAL:
1205 if prevp.parent.type in {
1206 syms.arglist, syms.argument, syms.parameters, syms.varargslist
1210 elif prevp.parent.type == syms.typedargslist:
1211 # A bit hacky: if the equal sign has whitespace, it means we
1212 # previously found it's a typed argument. So, we're using
1216 elif prevp.type == token.DOUBLESTAR:
1217 if prevp.parent and prevp.parent.type in {
1227 elif prevp.type == token.COLON:
1228 if prevp.parent and prevp.parent.type in {syms.subscript, syms.sliceop}:
1233 and prevp.parent.type in {syms.factor, syms.star_expr}
1234 and prevp.type in MATH_OPERATORS
1239 prevp.type == token.RIGHTSHIFT
1241 and prevp.parent.type == syms.shift_expr
1242 and prevp.prev_sibling
1243 and prevp.prev_sibling.type == token.NAME
1244 and prevp.prev_sibling.value == "print" # type: ignore
1246 # Python 2 print chevron
1249 elif prev.type in OPENING_BRACKETS:
1252 if p.type in {syms.parameters, syms.arglist}:
1253 # untyped function signatures or calls
1257 if not prev or prev.type != token.COMMA:
1260 elif p.type == syms.varargslist:
1265 if prev and prev.type != token.COMMA:
1268 elif p.type == syms.typedargslist:
1269 # typed function signatures
1273 if t == token.EQUAL:
1274 if prev.type != syms.tname:
1277 elif prev.type == token.EQUAL:
1278 # A bit hacky: if the equal sign has whitespace, it means we
1279 # previously found it's a typed argument. So, we're using that, too.
1282 elif prev.type != token.COMMA:
1285 elif p.type == syms.tname:
1288 prevp = preceding_leaf(p)
1289 if not prevp or prevp.type != token.COMMA:
1292 elif p.type == syms.trailer:
1293 # attributes and calls
1294 if t == token.LPAR or t == token.RPAR:
1299 prevp = preceding_leaf(p)
1300 if not prevp or prevp.type != token.NUMBER:
1303 elif t == token.LSQB:
1306 elif prev.type != token.COMMA:
1309 elif p.type == syms.argument:
1311 if t == token.EQUAL:
1315 prevp = preceding_leaf(p)
1316 if not prevp or prevp.type == token.LPAR:
1319 elif prev.type == token.EQUAL or prev.type == token.DOUBLESTAR:
1322 elif p.type == syms.decorator:
1326 elif p.type == syms.dotted_name:
1330 prevp = preceding_leaf(p)
1331 if not prevp or prevp.type == token.AT or prevp.type == token.DOT:
1334 elif p.type == syms.classdef:
1338 if prev and prev.type == token.LPAR:
1341 elif p.type == syms.subscript:
1344 assert p.parent is not None, "subscripts are always parented"
1345 if p.parent.type == syms.subscriptlist:
1353 elif p.type == syms.atom:
1354 if prev and t == token.DOT:
1355 # dots, but not the first one.
1359 p.type == syms.listmaker
1360 or p.type == syms.testlist_gexp
1361 or p.type == syms.subscriptlist
1363 # list interior, including unpacking
1367 elif p.type == syms.dictsetmaker:
1368 # dict and set interior, including unpacking
1372 if prev.type == token.DOUBLESTAR:
1375 elif p.type in {syms.factor, syms.star_expr}:
1378 prevp = preceding_leaf(p)
1379 if not prevp or prevp.type in OPENING_BRACKETS:
1382 prevp_parent = prevp.parent
1383 assert prevp_parent is not None
1384 if prevp.type == token.COLON and prevp_parent.type in {
1385 syms.subscript, syms.sliceop
1389 elif prevp.type == token.EQUAL and prevp_parent.type == syms.argument:
1392 elif t == token.NAME or t == token.NUMBER:
1395 elif p.type == syms.import_from:
1397 if prev and prev.type == token.DOT:
1400 elif t == token.NAME:
1404 if prev and prev.type == token.DOT:
1407 elif p.type == syms.sliceop:
1413 def preceding_leaf(node: Optional[LN]) -> Optional[Leaf]:
1414 """Return the first leaf that precedes `node`, if any."""
1416 res = node.prev_sibling
1418 if isinstance(res, Leaf):
1422 return list(res.leaves())[-1]
1431 def is_split_after_delimiter(leaf: Leaf, previous: Leaf = None) -> int:
1432 """Return the priority of the `leaf` delimiter, given a line break after it.
1434 The delimiter priorities returned here are from those delimiters that would
1435 cause a line break after themselves.
1437 Higher numbers are higher priority.
1439 if leaf.type == token.COMMA:
1440 return COMMA_PRIORITY
1445 def is_split_before_delimiter(leaf: Leaf, previous: Leaf = None) -> int:
1446 """Return the priority of the `leaf` delimiter, given a line before after it.
1448 The delimiter priorities returned here are from those delimiters that would
1449 cause a line break before themselves.
1451 Higher numbers are higher priority.
1454 leaf.type in VARARGS
1456 and leaf.parent.type in {syms.argument, syms.typedargslist}
1458 # * and ** might also be MATH_OPERATORS but in this case they are not.
1459 # Don't treat them as a delimiter.
1463 leaf.type in MATH_OPERATORS
1465 and leaf.parent.type not in {syms.factor, syms.star_expr}
1467 return MATH_PRIORITY
1469 if leaf.type in COMPARATORS:
1470 return COMPARATOR_PRIORITY
1473 leaf.type == token.STRING
1474 and previous is not None
1475 and previous.type == token.STRING
1477 return STRING_PRIORITY
1480 leaf.type == token.NAME
1481 and leaf.value == "for"
1483 and leaf.parent.type in {syms.comp_for, syms.old_comp_for}
1485 return COMPREHENSION_PRIORITY
1488 leaf.type == token.NAME
1489 and leaf.value == "if"
1491 and leaf.parent.type in {syms.comp_if, syms.old_comp_if}
1493 return COMPREHENSION_PRIORITY
1495 if leaf.type == token.NAME and leaf.value in LOGIC_OPERATORS and leaf.parent:
1496 return LOGIC_PRIORITY
1501 def is_delimiter(leaf: Leaf, previous: Leaf = None) -> int:
1502 """Return the priority of the `leaf` delimiter. Return 0 if not delimiter.
1504 Higher numbers are higher priority.
1507 is_split_before_delimiter(leaf, previous),
1508 is_split_after_delimiter(leaf, previous),
1512 def generate_comments(leaf: Leaf) -> Iterator[Leaf]:
1513 """Clean the prefix of the `leaf` and generate comments from it, if any.
1515 Comments in lib2to3 are shoved into the whitespace prefix. This happens
1516 in `pgen2/driver.py:Driver.parse_tokens()`. This was a brilliant implementation
1517 move because it does away with modifying the grammar to include all the
1518 possible places in which comments can be placed.
1520 The sad consequence for us though is that comments don't "belong" anywhere.
1521 This is why this function generates simple parentless Leaf objects for
1522 comments. We simply don't know what the correct parent should be.
1524 No matter though, we can live without this. We really only need to
1525 differentiate between inline and standalone comments. The latter don't
1526 share the line with any code.
1528 Inline comments are emitted as regular token.COMMENT leaves. Standalone
1529 are emitted with a fake STANDALONE_COMMENT token identifier.
1540 for index, line in enumerate(p.split("\n")):
1541 consumed += len(line) + 1 # adding the length of the split '\n'
1542 line = line.lstrip()
1545 if not line.startswith("#"):
1548 if index == 0 and leaf.type != token.ENDMARKER:
1549 comment_type = token.COMMENT # simple trailing comment
1551 comment_type = STANDALONE_COMMENT
1552 comment = make_comment(line)
1553 yield Leaf(comment_type, comment, prefix="\n" * nlines)
1555 if comment in {"# fmt: on", "# yapf: enable"}:
1556 raise FormatOn(consumed)
1558 if comment in {"# fmt: off", "# yapf: disable"}:
1559 if comment_type == STANDALONE_COMMENT:
1560 raise FormatOff(consumed)
1562 prev = preceding_leaf(leaf)
1563 if not prev or prev.type in WHITESPACE: # standalone comment in disguise
1564 raise FormatOff(consumed)
1569 def make_comment(content: str) -> str:
1570 """Return a consistently formatted comment from the given `content` string.
1572 All comments (except for "##", "#!", "#:") should have a single space between
1573 the hash sign and the content.
1575 If `content` didn't start with a hash sign, one is provided.
1577 content = content.rstrip()
1581 if content[0] == "#":
1582 content = content[1:]
1583 if content and content[0] not in " !:#":
1584 content = " " + content
1585 return "#" + content
1589 line: Line, line_length: int, inner: bool = False, py36: bool = False
1590 ) -> Iterator[Line]:
1591 """Split a `line` into potentially many lines.
1593 They should fit in the allotted `line_length` but might not be able to.
1594 `inner` signifies that there were a pair of brackets somewhere around the
1595 current `line`, possibly transitively. This means we can fallback to splitting
1596 by delimiters if the LHS/RHS don't yield any results.
1598 If `py36` is True, splitting may generate syntax that is only compatible
1599 with Python 3.6 and later.
1601 if isinstance(line, UnformattedLines) or line.is_comment:
1605 line_str = str(line).strip("\n")
1607 len(line_str) <= line_length
1608 and "\n" not in line_str # multiline strings
1609 and not line.contains_standalone_comments
1614 split_funcs: List[SplitFunc]
1616 split_funcs = [left_hand_split]
1617 elif line.inside_brackets:
1618 split_funcs = [delimiter_split, standalone_comment_split, right_hand_split]
1620 split_funcs = [right_hand_split]
1621 for split_func in split_funcs:
1622 # We are accumulating lines in `result` because we might want to abort
1623 # mission and return the original line in the end, or attempt a different
1625 result: List[Line] = []
1627 for l in split_func(line, py36):
1628 if str(l).strip("\n") == line_str:
1629 raise CannotSplit("Split function returned an unchanged result")
1632 split_line(l, line_length=line_length, inner=True, py36=py36)
1634 except CannotSplit as cs:
1645 def left_hand_split(line: Line, py36: bool = False) -> Iterator[Line]:
1646 """Split line into many lines, starting with the first matching bracket pair.
1648 Note: this usually looks weird, only use this for function definitions.
1649 Prefer RHS otherwise.
1651 head = Line(depth=line.depth)
1652 body = Line(depth=line.depth + 1, inside_brackets=True)
1653 tail = Line(depth=line.depth)
1654 tail_leaves: List[Leaf] = []
1655 body_leaves: List[Leaf] = []
1656 head_leaves: List[Leaf] = []
1657 current_leaves = head_leaves
1658 matching_bracket = None
1659 for leaf in line.leaves:
1661 current_leaves is body_leaves
1662 and leaf.type in CLOSING_BRACKETS
1663 and leaf.opening_bracket is matching_bracket
1665 current_leaves = tail_leaves if body_leaves else head_leaves
1666 current_leaves.append(leaf)
1667 if current_leaves is head_leaves:
1668 if leaf.type in OPENING_BRACKETS:
1669 matching_bracket = leaf
1670 current_leaves = body_leaves
1671 # Since body is a new indent level, remove spurious leading whitespace.
1673 normalize_prefix(body_leaves[0], inside_brackets=True)
1674 # Build the new lines.
1675 for result, leaves in (
1676 (head, head_leaves), (body, body_leaves), (tail, tail_leaves)
1679 result.append(leaf, preformatted=True)
1680 for comment_after in line.comments_after(leaf):
1681 result.append(comment_after, preformatted=True)
1682 bracket_split_succeeded_or_raise(head, body, tail)
1683 for result in (head, body, tail):
1688 def right_hand_split(line: Line, py36: bool = False) -> Iterator[Line]:
1689 """Split line into many lines, starting with the last matching bracket pair."""
1690 head = Line(depth=line.depth)
1691 body = Line(depth=line.depth + 1, inside_brackets=True)
1692 tail = Line(depth=line.depth)
1693 tail_leaves: List[Leaf] = []
1694 body_leaves: List[Leaf] = []
1695 head_leaves: List[Leaf] = []
1696 current_leaves = tail_leaves
1697 opening_bracket = None
1698 for leaf in reversed(line.leaves):
1699 if current_leaves is body_leaves:
1700 if leaf is opening_bracket:
1701 current_leaves = head_leaves if body_leaves else tail_leaves
1702 current_leaves.append(leaf)
1703 if current_leaves is tail_leaves:
1704 if leaf.type in CLOSING_BRACKETS:
1705 opening_bracket = leaf.opening_bracket
1706 current_leaves = body_leaves
1707 tail_leaves.reverse()
1708 body_leaves.reverse()
1709 head_leaves.reverse()
1710 # Since body is a new indent level, remove spurious leading whitespace.
1712 normalize_prefix(body_leaves[0], inside_brackets=True)
1713 # Build the new lines.
1714 for result, leaves in (
1715 (head, head_leaves), (body, body_leaves), (tail, tail_leaves)
1718 result.append(leaf, preformatted=True)
1719 for comment_after in line.comments_after(leaf):
1720 result.append(comment_after, preformatted=True)
1721 bracket_split_succeeded_or_raise(head, body, tail)
1722 for result in (head, body, tail):
1727 def bracket_split_succeeded_or_raise(head: Line, body: Line, tail: Line) -> None:
1728 """Raise :exc:`CannotSplit` if the last left- or right-hand split failed.
1730 Do nothing otherwise.
1732 A left- or right-hand split is based on a pair of brackets. Content before
1733 (and including) the opening bracket is left on one line, content inside the
1734 brackets is put on a separate line, and finally content starting with and
1735 following the closing bracket is put on a separate line.
1737 Those are called `head`, `body`, and `tail`, respectively. If the split
1738 produced the same line (all content in `head`) or ended up with an empty `body`
1739 and the `tail` is just the closing bracket, then it's considered failed.
1741 tail_len = len(str(tail).strip())
1744 raise CannotSplit("Splitting brackets produced the same line")
1748 f"Splitting brackets on an empty body to save "
1749 f"{tail_len} characters is not worth it"
1753 def dont_increase_indentation(split_func: SplitFunc) -> SplitFunc:
1754 """Normalize prefix of the first leaf in every line returned by `split_func`.
1756 This is a decorator over relevant split functions.
1760 def split_wrapper(line: Line, py36: bool = False) -> Iterator[Line]:
1761 for l in split_func(line, py36):
1762 normalize_prefix(l.leaves[0], inside_brackets=True)
1765 return split_wrapper
1768 @dont_increase_indentation
1769 def delimiter_split(line: Line, py36: bool = False) -> Iterator[Line]:
1770 """Split according to delimiters of the highest priority.
1772 If `py36` is True, the split will add trailing commas also in function
1773 signatures that contain `*` and `**`.
1776 last_leaf = line.leaves[-1]
1778 raise CannotSplit("Line empty")
1780 delimiters = line.bracket_tracker.delimiters
1782 delimiter_priority = line.bracket_tracker.max_delimiter_priority(
1783 exclude={id(last_leaf)}
1786 raise CannotSplit("No delimiters found")
1788 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
1789 lowest_depth = sys.maxsize
1790 trailing_comma_safe = True
1792 def append_to_line(leaf: Leaf) -> Iterator[Line]:
1793 """Append `leaf` to current line or to new line if appending impossible."""
1794 nonlocal current_line
1796 current_line.append_safe(leaf, preformatted=True)
1797 except ValueError as ve:
1800 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
1801 current_line.append(leaf)
1803 for leaf in line.leaves:
1804 yield from append_to_line(leaf)
1806 for comment_after in line.comments_after(leaf):
1807 yield from append_to_line(comment_after)
1809 lowest_depth = min(lowest_depth, leaf.bracket_depth)
1811 leaf.bracket_depth == lowest_depth
1812 and leaf.type == token.STAR
1813 or leaf.type == token.DOUBLESTAR
1815 trailing_comma_safe = trailing_comma_safe and py36
1816 leaf_priority = delimiters.get(id(leaf))
1817 if leaf_priority == delimiter_priority:
1820 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
1824 and delimiter_priority == COMMA_PRIORITY
1825 and current_line.leaves[-1].type != token.COMMA
1826 and current_line.leaves[-1].type != STANDALONE_COMMENT
1828 current_line.append(Leaf(token.COMMA, ","))
1832 @dont_increase_indentation
1833 def standalone_comment_split(line: Line, py36: bool = False) -> Iterator[Line]:
1834 """Split standalone comments from the rest of the line."""
1835 for leaf in line.leaves:
1836 if leaf.type == STANDALONE_COMMENT:
1837 if leaf.bracket_depth == 0:
1841 raise CannotSplit("Line does not have any standalone comments")
1843 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
1845 def append_to_line(leaf: Leaf) -> Iterator[Line]:
1846 """Append `leaf` to current line or to new line if appending impossible."""
1847 nonlocal current_line
1849 current_line.append_safe(leaf, preformatted=True)
1850 except ValueError as ve:
1853 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
1854 current_line.append(leaf)
1856 for leaf in line.leaves:
1857 yield from append_to_line(leaf)
1859 for comment_after in line.comments_after(leaf):
1860 yield from append_to_line(comment_after)
1866 def is_import(leaf: Leaf) -> bool:
1867 """Return True if the given leaf starts an import statement."""
1874 (v == "import" and p and p.type == syms.import_name)
1875 or (v == "from" and p and p.type == syms.import_from)
1880 def normalize_prefix(leaf: Leaf, *, inside_brackets: bool) -> None:
1881 """Leave existing extra newlines if not `inside_brackets`. Remove everything
1884 Note: don't use backslashes for formatting or you'll lose your voting rights.
1886 if not inside_brackets:
1887 spl = leaf.prefix.split("#")
1888 if "\\" not in spl[0]:
1889 nl_count = spl[-1].count("\n")
1892 leaf.prefix = "\n" * nl_count
1898 def normalize_string_quotes(leaf: Leaf) -> None:
1899 """Prefer double quotes but only if it doesn't cause more escaping.
1901 Adds or removes backslashes as appropriate. Doesn't parse and fix
1902 strings nested in f-strings (yet).
1904 Note: Mutates its argument.
1906 value = leaf.value.lstrip("furbFURB")
1907 if value[:3] == '"""':
1910 elif value[:3] == "'''":
1913 elif value[0] == '"':
1919 first_quote_pos = leaf.value.find(orig_quote)
1920 if first_quote_pos == -1:
1921 return # There's an internal error
1923 prefix = leaf.value[:first_quote_pos]
1924 body = leaf.value[first_quote_pos + len(orig_quote):-len(orig_quote)]
1925 if "r" in prefix.casefold():
1926 if body.count(new_quote) != body.count(f"\\{new_quote}"):
1927 # There's at least one unescaped new_quote in this raw string
1928 # so converting is impossible
1931 # Do not introduce or remove backslashes in raw strings
1934 new_body = body.replace(f"\\{orig_quote}", orig_quote).replace(
1935 new_quote, f"\\{new_quote}"
1937 if new_quote == '"""' and new_body[-1] == '"':
1939 new_body = new_body[:-1] + '\\"'
1940 orig_escape_count = body.count("\\")
1941 new_escape_count = new_body.count("\\")
1942 if new_escape_count > orig_escape_count:
1943 return # Do not introduce more escaping
1945 if new_escape_count == orig_escape_count and orig_quote == '"':
1946 return # Prefer double quotes
1948 leaf.value = f"{prefix}{new_quote}{new_body}{new_quote}"
1951 def is_python36(node: Node) -> bool:
1952 """Return True if the current file is using Python 3.6+ features.
1954 Currently looking for:
1956 - trailing commas after * or ** in function signatures.
1958 for n in node.pre_order():
1959 if n.type == token.STRING:
1960 value_head = n.value[:2] # type: ignore
1961 if value_head in {'f"', 'F"', "f'", "F'", "rf", "fr", "RF", "FR"}:
1965 n.type == syms.typedargslist
1967 and n.children[-1].type == token.COMMA
1969 for ch in n.children:
1970 if ch.type == token.STAR or ch.type == token.DOUBLESTAR:
1976 PYTHON_EXTENSIONS = {".py"}
1977 BLACKLISTED_DIRECTORIES = {
1978 "build", "buck-out", "dist", "_build", ".git", ".hg", ".mypy_cache", ".tox", ".venv"
1982 def gen_python_files_in_dir(path: Path) -> Iterator[Path]:
1983 """Generate all files under `path` which aren't under BLACKLISTED_DIRECTORIES
1984 and have one of the PYTHON_EXTENSIONS.
1986 for child in path.iterdir():
1988 if child.name in BLACKLISTED_DIRECTORIES:
1991 yield from gen_python_files_in_dir(child)
1993 elif child.suffix in PYTHON_EXTENSIONS:
1999 """Provides a reformatting counter. Can be rendered with `str(report)`."""
2002 change_count: int = 0
2004 failure_count: int = 0
2006 def done(self, src: Path, changed: bool) -> None:
2007 """Increment the counter for successful reformatting. Write out a message."""
2009 reformatted = "would reformat" if self.check else "reformatted"
2011 out(f"{reformatted} {src}")
2012 self.change_count += 1
2015 out(f"{src} already well formatted, good job.", bold=False)
2016 self.same_count += 1
2018 def failed(self, src: Path, message: str) -> None:
2019 """Increment the counter for failed reformatting. Write out a message."""
2020 err(f"error: cannot format {src}: {message}")
2021 self.failure_count += 1
2024 def return_code(self) -> int:
2025 """Return the exit code that the app should use.
2027 This considers the current state of changed files and failures:
2028 - if there were any failures, return 123;
2029 - if any files were changed and --check is being used, return 1;
2030 - otherwise return 0.
2032 # According to http://tldp.org/LDP/abs/html/exitcodes.html starting with
2033 # 126 we have special returncodes reserved by the shell.
2034 if self.failure_count:
2037 elif self.change_count and self.check:
2042 def __str__(self) -> str:
2043 """Render a color report of the current state.
2045 Use `click.unstyle` to remove colors.
2048 reformatted = "would be reformatted"
2049 unchanged = "would be left unchanged"
2050 failed = "would fail to reformat"
2052 reformatted = "reformatted"
2053 unchanged = "left unchanged"
2054 failed = "failed to reformat"
2056 if self.change_count:
2057 s = "s" if self.change_count > 1 else ""
2059 click.style(f"{self.change_count} file{s} {reformatted}", bold=True)
2062 s = "s" if self.same_count > 1 else ""
2063 report.append(f"{self.same_count} file{s} {unchanged}")
2064 if self.failure_count:
2065 s = "s" if self.failure_count > 1 else ""
2067 click.style(f"{self.failure_count} file{s} {failed}", fg="red")
2069 return ", ".join(report) + "."
2072 def assert_equivalent(src: str, dst: str) -> None:
2073 """Raise AssertionError if `src` and `dst` aren't equivalent."""
2078 def _v(node: ast.AST, depth: int = 0) -> Iterator[str]:
2079 """Simple visitor generating strings to compare ASTs by content."""
2080 yield f"{' ' * depth}{node.__class__.__name__}("
2082 for field in sorted(node._fields):
2084 value = getattr(node, field)
2085 except AttributeError:
2088 yield f"{' ' * (depth+1)}{field}="
2090 if isinstance(value, list):
2092 if isinstance(item, ast.AST):
2093 yield from _v(item, depth + 2)
2095 elif isinstance(value, ast.AST):
2096 yield from _v(value, depth + 2)
2099 yield f"{' ' * (depth+2)}{value!r}, # {value.__class__.__name__}"
2101 yield f"{' ' * depth}) # /{node.__class__.__name__}"
2104 src_ast = ast.parse(src)
2105 except Exception as exc:
2106 major, minor = sys.version_info[:2]
2107 raise AssertionError(
2108 f"cannot use --safe with this file; failed to parse source file "
2109 f"with Python {major}.{minor}'s builtin AST. Re-run with --fast "
2110 f"or stop using deprecated Python 2 syntax. AST error message: {exc}"
2114 dst_ast = ast.parse(dst)
2115 except Exception as exc:
2116 log = dump_to_file("".join(traceback.format_tb(exc.__traceback__)), dst)
2117 raise AssertionError(
2118 f"INTERNAL ERROR: Black produced invalid code: {exc}. "
2119 f"Please report a bug on https://github.com/ambv/black/issues. "
2120 f"This invalid output might be helpful: {log}"
2123 src_ast_str = "\n".join(_v(src_ast))
2124 dst_ast_str = "\n".join(_v(dst_ast))
2125 if src_ast_str != dst_ast_str:
2126 log = dump_to_file(diff(src_ast_str, dst_ast_str, "src", "dst"))
2127 raise AssertionError(
2128 f"INTERNAL ERROR: Black produced code that is not equivalent to "
2130 f"Please report a bug on https://github.com/ambv/black/issues. "
2131 f"This diff might be helpful: {log}"
2135 def assert_stable(src: str, dst: str, line_length: int) -> None:
2136 """Raise AssertionError if `dst` reformats differently the second time."""
2137 newdst = format_str(dst, line_length=line_length)
2140 diff(src, dst, "source", "first pass"),
2141 diff(dst, newdst, "first pass", "second pass"),
2143 raise AssertionError(
2144 f"INTERNAL ERROR: Black produced different code on the second pass "
2145 f"of the formatter. "
2146 f"Please report a bug on https://github.com/ambv/black/issues. "
2147 f"This diff might be helpful: {log}"
2151 def dump_to_file(*output: str) -> str:
2152 """Dump `output` to a temporary file. Return path to the file."""
2155 with tempfile.NamedTemporaryFile(
2156 mode="w", prefix="blk_", suffix=".log", delete=False
2158 for lines in output:
2160 if lines and lines[-1] != "\n":
2165 def diff(a: str, b: str, a_name: str, b_name: str) -> str:
2166 """Return a unified diff string between strings `a` and `b`."""
2169 a_lines = [line + "\n" for line in a.split("\n")]
2170 b_lines = [line + "\n" for line in b.split("\n")]
2172 difflib.unified_diff(a_lines, b_lines, fromfile=a_name, tofile=b_name, n=5)
2176 def cancel(tasks: List[asyncio.Task]) -> None:
2177 """asyncio signal handler that cancels all `tasks` and reports to stderr."""
2183 def shutdown(loop: BaseEventLoop) -> None:
2184 """Cancel all pending tasks on `loop`, wait for them, and close the loop."""
2186 # This part is borrowed from asyncio/runners.py in Python 3.7b2.
2187 to_cancel = [task for task in asyncio.Task.all_tasks(loop) if not task.done()]
2191 for task in to_cancel:
2193 loop.run_until_complete(
2194 asyncio.gather(*to_cancel, loop=loop, return_exceptions=True)
2197 # `concurrent.futures.Future` objects cannot be cancelled once they
2198 # are already running. There might be some when the `shutdown()` happened.
2199 # Silence their logger's spew about the event loop being closed.
2200 cf_logger = logging.getLogger("concurrent.futures")
2201 cf_logger.setLevel(logging.CRITICAL)
2205 if __name__ == "__main__":