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.
5 from asyncio.base_events import BaseEventLoop
6 from concurrent.futures import Executor, ProcessPoolExecutor
8 from functools import partial, wraps
11 from multiprocessing import Manager
13 from pathlib import Path
36 from appdirs import user_cache_dir
37 from attr import dataclass, Factory
41 from blib2to3.pytree import Node, Leaf, type_repr
42 from blib2to3 import pygram, pytree
43 from blib2to3.pgen2 import driver, token
44 from blib2to3.pgen2.parse import ParseError
46 __version__ = "18.4a4"
47 DEFAULT_LINE_LENGTH = 88
50 syms = pygram.python_symbols
58 LN = Union[Leaf, Node]
59 SplitFunc = Callable[["Line", bool], Iterator["Line"]]
62 CacheInfo = Tuple[Timestamp, FileSize]
63 Cache = Dict[Path, CacheInfo]
64 out = partial(click.secho, bold=True, err=True)
65 err = partial(click.secho, fg="red", err=True)
68 class NothingChanged(UserWarning):
69 """Raised by :func:`format_file` when reformatted code is the same as source."""
72 class CannotSplit(Exception):
73 """A readable split that fits the allotted line length is impossible.
75 Raised by :func:`left_hand_split`, :func:`right_hand_split`, and
76 :func:`delimiter_split`.
80 class FormatError(Exception):
81 """Base exception for `# fmt: on` and `# fmt: off` handling.
83 It holds the number of bytes of the prefix consumed before the format
84 control comment appeared.
87 def __init__(self, consumed: int) -> None:
88 super().__init__(consumed)
89 self.consumed = consumed
91 def trim_prefix(self, leaf: Leaf) -> None:
92 leaf.prefix = leaf.prefix[self.consumed :]
94 def leaf_from_consumed(self, leaf: Leaf) -> Leaf:
95 """Returns a new Leaf from the consumed part of the prefix."""
96 unformatted_prefix = leaf.prefix[: self.consumed]
97 return Leaf(token.NEWLINE, unformatted_prefix)
100 class FormatOn(FormatError):
101 """Found a comment like `# fmt: on` in the file."""
104 class FormatOff(FormatError):
105 """Found a comment like `# fmt: off` in the file."""
108 class WriteBack(Enum):
125 default=DEFAULT_LINE_LENGTH,
126 help="How many character per line to allow.",
133 "Don't write the files back, just return the status. Return code 0 "
134 "means nothing would change. Return code 1 means some files would be "
135 "reformatted. Return code 123 means there was an internal error."
141 help="Don't write the files back, just output a diff for each file on stdout.",
146 help="If --fast given, skip temporary sanity checks. [default: --safe]",
153 "Don't emit non-error messages to stderr. Errors are still emitted, "
154 "silence those with 2>/dev/null."
157 @click.version_option(version=__version__)
162 exists=True, file_okay=True, dir_okay=True, readable=True, allow_dash=True
175 """The uncompromising code formatter."""
176 sources: List[Path] = []
180 sources.extend(gen_python_files_in_dir(p))
182 # if a file was explicitly given, we don't care about its extension
185 sources.append(Path("-"))
187 err(f"invalid path: {s}")
189 if check and not diff:
190 write_back = WriteBack.NO
192 write_back = WriteBack.DIFF
194 write_back = WriteBack.YES
195 report = Report(check=check, quiet=quiet)
196 if len(sources) == 0:
200 elif len(sources) == 1:
201 reformat_one(sources[0], line_length, fast, write_back, report)
203 loop = asyncio.get_event_loop()
204 executor = ProcessPoolExecutor(max_workers=os.cpu_count())
206 loop.run_until_complete(
208 sources, line_length, fast, write_back, report, loop, executor
214 out("All done! ✨ 🍰 ✨")
215 click.echo(str(report))
216 ctx.exit(report.return_code)
220 src: Path, line_length: int, fast: bool, write_back: WriteBack, report: "Report"
222 """Reformat a single file under `src` without spawning child processes.
224 If `quiet` is True, non-error messages are not output. `line_length`,
225 `write_back`, and `fast` options are passed to :func:`format_file_in_place`.
229 if not src.is_file() and str(src) == "-":
230 if format_stdin_to_stdout(
231 line_length=line_length, fast=fast, write_back=write_back
233 changed = Changed.YES
236 if write_back != WriteBack.DIFF:
237 cache = read_cache(line_length)
239 if src in cache and cache[src] == get_cache_info(src):
240 changed = Changed.CACHED
242 changed is not Changed.CACHED
243 and format_file_in_place(
244 src, line_length=line_length, fast=fast, write_back=write_back
247 changed = Changed.YES
248 if write_back == WriteBack.YES and changed is not Changed.NO:
249 write_cache(cache, [src], line_length)
250 report.done(src, changed)
251 except Exception as exc:
252 report.failed(src, str(exc))
255 async def schedule_formatting(
259 write_back: WriteBack,
264 """Run formatting of `sources` in parallel using the provided `executor`.
266 (Use ProcessPoolExecutors for actual parallelism.)
268 `line_length`, `write_back`, and `fast` options are passed to
269 :func:`format_file_in_place`.
272 if write_back != WriteBack.DIFF:
273 cache = read_cache(line_length)
274 sources, cached = filter_cached(cache, sources)
276 report.done(src, Changed.CACHED)
281 if write_back == WriteBack.DIFF:
282 # For diff output, we need locks to ensure we don't interleave output
283 # from different processes.
285 lock = manager.Lock()
287 src: loop.run_in_executor(
288 executor, format_file_in_place, src, line_length, fast, write_back, lock
292 _task_values = list(tasks.values())
294 loop.add_signal_handler(signal.SIGINT, cancel, _task_values)
295 loop.add_signal_handler(signal.SIGTERM, cancel, _task_values)
296 except NotImplementedError:
297 # There are no good alternatives for these on Windows
299 await asyncio.wait(_task_values)
300 for src, task in tasks.items():
302 report.failed(src, "timed out, cancelling")
304 cancelled.append(task)
305 elif task.cancelled():
306 cancelled.append(task)
307 elif task.exception():
308 report.failed(src, str(task.exception()))
310 formatted.append(src)
311 report.done(src, Changed.YES if task.result() else Changed.NO)
314 await asyncio.gather(*cancelled, loop=loop, return_exceptions=True)
315 if write_back == WriteBack.YES and formatted:
316 write_cache(cache, formatted, line_length)
319 def format_file_in_place(
323 write_back: WriteBack = WriteBack.NO,
324 lock: Any = None, # multiprocessing.Manager().Lock() is some crazy proxy
326 """Format file under `src` path. Return True if changed.
328 If `write_back` is True, write reformatted code back to stdout.
329 `line_length` and `fast` options are passed to :func:`format_file_contents`.
332 with tokenize.open(src) as src_buffer:
333 src_contents = src_buffer.read()
335 dst_contents = format_file_contents(
336 src_contents, line_length=line_length, fast=fast
338 except NothingChanged:
341 if write_back == write_back.YES:
342 with open(src, "w", encoding=src_buffer.encoding) as f:
343 f.write(dst_contents)
344 elif write_back == write_back.DIFF:
345 src_name = f"{src} (original)"
346 dst_name = f"{src} (formatted)"
347 diff_contents = diff(src_contents, dst_contents, src_name, dst_name)
351 sys.stdout.write(diff_contents)
358 def format_stdin_to_stdout(
359 line_length: int, fast: bool, write_back: WriteBack = WriteBack.NO
361 """Format file on stdin. Return True if changed.
363 If `write_back` is True, write reformatted code back to stdout.
364 `line_length` and `fast` arguments are passed to :func:`format_file_contents`.
366 src = sys.stdin.read()
369 dst = format_file_contents(src, line_length=line_length, fast=fast)
372 except NothingChanged:
376 if write_back == WriteBack.YES:
377 sys.stdout.write(dst)
378 elif write_back == WriteBack.DIFF:
379 src_name = "<stdin> (original)"
380 dst_name = "<stdin> (formatted)"
381 sys.stdout.write(diff(src, dst, src_name, dst_name))
384 def format_file_contents(
385 src_contents: str, line_length: int, fast: bool
387 """Reformat contents a file and return new contents.
389 If `fast` is False, additionally confirm that the reformatted code is
390 valid by calling :func:`assert_equivalent` and :func:`assert_stable` on it.
391 `line_length` is passed to :func:`format_str`.
393 if src_contents.strip() == "":
396 dst_contents = format_str(src_contents, line_length=line_length)
397 if src_contents == dst_contents:
401 assert_equivalent(src_contents, dst_contents)
402 assert_stable(src_contents, dst_contents, line_length=line_length)
406 def format_str(src_contents: str, line_length: int) -> FileContent:
407 """Reformat a string and return new contents.
409 `line_length` determines how many characters per line are allowed.
411 src_node = lib2to3_parse(src_contents)
413 lines = LineGenerator()
414 elt = EmptyLineTracker()
415 py36 = is_python36(src_node)
418 for current_line in lines.visit(src_node):
419 for _ in range(after):
420 dst_contents += str(empty_line)
421 before, after = elt.maybe_empty_lines(current_line)
422 for _ in range(before):
423 dst_contents += str(empty_line)
424 for line in split_line(current_line, line_length=line_length, py36=py36):
425 dst_contents += str(line)
430 pygram.python_grammar_no_print_statement_no_exec_statement,
431 pygram.python_grammar_no_print_statement,
432 pygram.python_grammar,
436 def lib2to3_parse(src_txt: str) -> Node:
437 """Given a string with source, return the lib2to3 Node."""
438 grammar = pygram.python_grammar_no_print_statement
439 if src_txt[-1] != "\n":
440 nl = "\r\n" if "\r\n" in src_txt[:1024] else "\n"
442 for grammar in GRAMMARS:
443 drv = driver.Driver(grammar, pytree.convert)
445 result = drv.parse_string(src_txt, True)
448 except ParseError as pe:
449 lineno, column = pe.context[1]
450 lines = src_txt.splitlines()
452 faulty_line = lines[lineno - 1]
454 faulty_line = "<line number missing in source>"
455 exc = ValueError(f"Cannot parse: {lineno}:{column}: {faulty_line}")
459 if isinstance(result, Leaf):
460 result = Node(syms.file_input, [result])
464 def lib2to3_unparse(node: Node) -> str:
465 """Given a lib2to3 node, return its string representation."""
473 class Visitor(Generic[T]):
474 """Basic lib2to3 visitor that yields things of type `T` on `visit()`."""
476 def visit(self, node: LN) -> Iterator[T]:
477 """Main method to visit `node` and its children.
479 It tries to find a `visit_*()` method for the given `node.type`, like
480 `visit_simple_stmt` for Node objects or `visit_INDENT` for Leaf objects.
481 If no dedicated `visit_*()` method is found, chooses `visit_default()`
484 Then yields objects of type `T` from the selected visitor.
487 name = token.tok_name[node.type]
489 name = type_repr(node.type)
490 yield from getattr(self, f"visit_{name}", self.visit_default)(node)
492 def visit_default(self, node: LN) -> Iterator[T]:
493 """Default `visit_*()` implementation. Recurses to children of `node`."""
494 if isinstance(node, Node):
495 for child in node.children:
496 yield from self.visit(child)
500 class DebugVisitor(Visitor[T]):
503 def visit_default(self, node: LN) -> Iterator[T]:
504 indent = " " * (2 * self.tree_depth)
505 if isinstance(node, Node):
506 _type = type_repr(node.type)
507 out(f"{indent}{_type}", fg="yellow")
509 for child in node.children:
510 yield from self.visit(child)
513 out(f"{indent}/{_type}", fg="yellow", bold=False)
515 _type = token.tok_name.get(node.type, str(node.type))
516 out(f"{indent}{_type}", fg="blue", nl=False)
518 # We don't have to handle prefixes for `Node` objects since
519 # that delegates to the first child anyway.
520 out(f" {node.prefix!r}", fg="green", bold=False, nl=False)
521 out(f" {node.value!r}", fg="blue", bold=False)
524 def show(cls, code: str) -> None:
525 """Pretty-print the lib2to3 AST of a given string of `code`.
527 Convenience method for debugging.
529 v: DebugVisitor[None] = DebugVisitor()
530 list(v.visit(lib2to3_parse(code)))
533 KEYWORDS = set(keyword.kwlist)
534 WHITESPACE = {token.DEDENT, token.INDENT, token.NEWLINE}
535 FLOW_CONTROL = {"return", "raise", "break", "continue"}
546 STANDALONE_COMMENT = 153
547 LOGIC_OPERATORS = {"and", "or"}
571 STARS = {token.STAR, token.DOUBLESTAR}
574 syms.argument, # double star in arglist
575 syms.trailer, # single argument to call
577 syms.varargslist, # lambdas
579 UNPACKING_PARENTS = {
580 syms.atom, # single element of a list or set literal
602 COMPREHENSION_PRIORITY = 20
607 COMPARATOR_PRIORITY = 3
612 class BracketTracker:
613 """Keeps track of brackets on a line."""
616 bracket_match: Dict[Tuple[Depth, NodeType], Leaf] = Factory(dict)
617 delimiters: Dict[LeafID, Priority] = Factory(dict)
618 previous: Optional[Leaf] = None
619 _for_loop_variable: bool = False
620 _lambda_arguments: bool = False
622 def mark(self, leaf: Leaf) -> None:
623 """Mark `leaf` with bracket-related metadata. Keep track of delimiters.
625 All leaves receive an int `bracket_depth` field that stores how deep
626 within brackets a given leaf is. 0 means there are no enclosing brackets
627 that started on this line.
629 If a leaf is itself a closing bracket, it receives an `opening_bracket`
630 field that it forms a pair with. This is a one-directional link to
631 avoid reference cycles.
633 If a leaf is a delimiter (a token on which Black can split the line if
634 needed) and it's on depth 0, its `id()` is stored in the tracker's
637 if leaf.type == token.COMMENT:
640 self.maybe_decrement_after_for_loop_variable(leaf)
641 self.maybe_decrement_after_lambda_arguments(leaf)
642 if leaf.type in CLOSING_BRACKETS:
644 opening_bracket = self.bracket_match.pop((self.depth, leaf.type))
645 leaf.opening_bracket = opening_bracket
646 leaf.bracket_depth = self.depth
648 delim = is_split_before_delimiter(leaf, self.previous)
649 if delim and self.previous is not None:
650 self.delimiters[id(self.previous)] = delim
652 delim = is_split_after_delimiter(leaf, self.previous)
654 self.delimiters[id(leaf)] = delim
655 if leaf.type in OPENING_BRACKETS:
656 self.bracket_match[self.depth, BRACKET[leaf.type]] = leaf
659 self.maybe_increment_lambda_arguments(leaf)
660 self.maybe_increment_for_loop_variable(leaf)
662 def any_open_brackets(self) -> bool:
663 """Return True if there is an yet unmatched open bracket on the line."""
664 return bool(self.bracket_match)
666 def max_delimiter_priority(self, exclude: Iterable[LeafID] = ()) -> int:
667 """Return the highest priority of a delimiter found on the line.
669 Values are consistent with what `is_split_*_delimiter()` return.
670 Raises ValueError on no delimiters.
672 return max(v for k, v in self.delimiters.items() if k not in exclude)
674 def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
675 """In a for loop, or comprehension, the variables are often unpacks.
677 To avoid splitting on the comma in this situation, increase the depth of
678 tokens between `for` and `in`.
680 if leaf.type == token.NAME and leaf.value == "for":
682 self._for_loop_variable = True
687 def maybe_decrement_after_for_loop_variable(self, leaf: Leaf) -> bool:
688 """See `maybe_increment_for_loop_variable` above for explanation."""
689 if self._for_loop_variable and leaf.type == token.NAME and leaf.value == "in":
691 self._for_loop_variable = False
696 def maybe_increment_lambda_arguments(self, leaf: Leaf) -> bool:
697 """In a lambda expression, there might be more than one argument.
699 To avoid splitting on the comma in this situation, increase the depth of
700 tokens between `lambda` and `:`.
702 if leaf.type == token.NAME and leaf.value == "lambda":
704 self._lambda_arguments = True
709 def maybe_decrement_after_lambda_arguments(self, leaf: Leaf) -> bool:
710 """See `maybe_increment_lambda_arguments` above for explanation."""
711 if self._lambda_arguments and leaf.type == token.COLON:
713 self._lambda_arguments = False
718 def get_open_lsqb(self) -> Optional[Leaf]:
719 """Return the most recent opening square bracket (if any)."""
720 return self.bracket_match.get((self.depth - 1, token.RSQB))
725 """Holds leaves and comments. Can be printed with `str(line)`."""
728 leaves: List[Leaf] = Factory(list)
729 comments: List[Tuple[Index, Leaf]] = Factory(list)
730 bracket_tracker: BracketTracker = Factory(BracketTracker)
731 inside_brackets: bool = False
733 def append(self, leaf: Leaf, preformatted: bool = False) -> None:
734 """Add a new `leaf` to the end of the line.
736 Unless `preformatted` is True, the `leaf` will receive a new consistent
737 whitespace prefix and metadata applied by :class:`BracketTracker`.
738 Trailing commas are maybe removed, unpacked for loop variables are
739 demoted from being delimiters.
741 Inline comments are put aside.
743 has_value = leaf.type in BRACKETS or bool(leaf.value.strip())
747 if self.leaves and not preformatted:
748 # Note: at this point leaf.prefix should be empty except for
749 # imports, for which we only preserve newlines.
750 leaf.prefix += whitespace(
751 leaf, complex_subscript=self.is_complex_subscript(leaf)
753 if self.inside_brackets or not preformatted:
754 self.bracket_tracker.mark(leaf)
755 self.maybe_remove_trailing_comma(leaf)
757 if not self.append_comment(leaf):
758 self.leaves.append(leaf)
760 def append_safe(self, leaf: Leaf, preformatted: bool = False) -> None:
761 """Like :func:`append()` but disallow invalid standalone comment structure.
763 Raises ValueError when any `leaf` is appended after a standalone comment
764 or when a standalone comment is not the first leaf on the line.
766 if self.bracket_tracker.depth == 0:
768 raise ValueError("cannot append to standalone comments")
770 if self.leaves and leaf.type == STANDALONE_COMMENT:
772 "cannot append standalone comments to a populated line"
775 self.append(leaf, preformatted=preformatted)
778 def is_comment(self) -> bool:
779 """Is this line a standalone comment?"""
780 return len(self.leaves) == 1 and self.leaves[0].type == STANDALONE_COMMENT
783 def is_decorator(self) -> bool:
784 """Is this line a decorator?"""
785 return bool(self) and self.leaves[0].type == token.AT
788 def is_import(self) -> bool:
789 """Is this an import line?"""
790 return bool(self) and is_import(self.leaves[0])
793 def is_class(self) -> bool:
794 """Is this line a class definition?"""
797 and self.leaves[0].type == token.NAME
798 and self.leaves[0].value == "class"
802 def is_def(self) -> bool:
803 """Is this a function definition? (Also returns True for async defs.)"""
805 first_leaf = self.leaves[0]
810 second_leaf: Optional[Leaf] = self.leaves[1]
814 (first_leaf.type == token.NAME and first_leaf.value == "def")
816 first_leaf.type == token.ASYNC
817 and second_leaf is not None
818 and second_leaf.type == token.NAME
819 and second_leaf.value == "def"
824 def is_flow_control(self) -> bool:
825 """Is this line a flow control statement?
827 Those are `return`, `raise`, `break`, and `continue`.
831 and self.leaves[0].type == token.NAME
832 and self.leaves[0].value in FLOW_CONTROL
836 def is_yield(self) -> bool:
837 """Is this line a yield statement?"""
840 and self.leaves[0].type == token.NAME
841 and self.leaves[0].value == "yield"
844 def contains_standalone_comments(self, depth_limit: int = sys.maxsize) -> bool:
845 """If so, needs to be split before emitting."""
846 for leaf in self.leaves:
847 if leaf.type == STANDALONE_COMMENT:
848 if leaf.bracket_depth <= depth_limit:
853 def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
854 """Remove trailing comma if there is one and it's safe."""
857 and self.leaves[-1].type == token.COMMA
858 and closing.type in CLOSING_BRACKETS
862 if closing.type == token.RBRACE:
863 self.remove_trailing_comma()
866 if closing.type == token.RSQB:
867 comma = self.leaves[-1]
868 if comma.parent and comma.parent.type == syms.listmaker:
869 self.remove_trailing_comma()
872 # For parens let's check if it's safe to remove the comma. If the
873 # trailing one is the only one, we might mistakenly change a tuple
874 # into a different type by removing the comma.
875 depth = closing.bracket_depth + 1
877 opening = closing.opening_bracket
878 for _opening_index, leaf in enumerate(self.leaves):
885 for leaf in self.leaves[_opening_index + 1 :]:
889 bracket_depth = leaf.bracket_depth
890 if bracket_depth == depth and leaf.type == token.COMMA:
892 if leaf.parent and leaf.parent.type == syms.arglist:
897 self.remove_trailing_comma()
902 def append_comment(self, comment: Leaf) -> bool:
903 """Add an inline or standalone comment to the line."""
905 comment.type == STANDALONE_COMMENT
906 and self.bracket_tracker.any_open_brackets()
911 if comment.type != token.COMMENT:
914 after = len(self.leaves) - 1
916 comment.type = STANDALONE_COMMENT
921 self.comments.append((after, comment))
924 def comments_after(self, leaf: Leaf) -> Iterator[Leaf]:
925 """Generate comments that should appear directly after `leaf`."""
926 for _leaf_index, _leaf in enumerate(self.leaves):
933 for index, comment_after in self.comments:
934 if _leaf_index == index:
937 def remove_trailing_comma(self) -> None:
938 """Remove the trailing comma and moves the comments attached to it."""
939 comma_index = len(self.leaves) - 1
940 for i in range(len(self.comments)):
941 comment_index, comment = self.comments[i]
942 if comment_index == comma_index:
943 self.comments[i] = (comma_index - 1, comment)
946 def is_complex_subscript(self, leaf: Leaf) -> bool:
947 """Return True iff `leaf` is part of a slice with non-trivial exprs."""
949 leaf if leaf.type == token.LSQB else self.bracket_tracker.get_open_lsqb()
951 if open_lsqb is None:
954 subscript_start = open_lsqb.next_sibling
956 isinstance(subscript_start, Node)
957 and subscript_start.type == syms.subscriptlist
959 subscript_start = child_towards(subscript_start, leaf)
960 return subscript_start is not None and any(
961 n.type in TEST_DESCENDANTS for n in subscript_start.pre_order()
964 def __str__(self) -> str:
965 """Render the line."""
969 indent = " " * self.depth
970 leaves = iter(self.leaves)
972 res = f"{first.prefix}{indent}{first.value}"
975 for _, comment in self.comments:
979 def __bool__(self) -> bool:
980 """Return True if the line has leaves or comments."""
981 return bool(self.leaves or self.comments)
984 class UnformattedLines(Line):
985 """Just like :class:`Line` but stores lines which aren't reformatted."""
987 def append(self, leaf: Leaf, preformatted: bool = True) -> None:
988 """Just add a new `leaf` to the end of the lines.
990 The `preformatted` argument is ignored.
992 Keeps track of indentation `depth`, which is useful when the user
993 says `# fmt: on`. Otherwise, doesn't do anything with the `leaf`.
996 list(generate_comments(leaf))
997 except FormatOn as f_on:
998 self.leaves.append(f_on.leaf_from_consumed(leaf))
1001 self.leaves.append(leaf)
1002 if leaf.type == token.INDENT:
1004 elif leaf.type == token.DEDENT:
1007 def __str__(self) -> str:
1008 """Render unformatted lines from leaves which were added with `append()`.
1010 `depth` is not used for indentation in this case.
1016 for leaf in self.leaves:
1020 def append_comment(self, comment: Leaf) -> bool:
1021 """Not implemented in this class. Raises `NotImplementedError`."""
1022 raise NotImplementedError("Unformatted lines don't store comments separately.")
1024 def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
1025 """Does nothing and returns False."""
1028 def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
1029 """Does nothing and returns False."""
1034 class EmptyLineTracker:
1035 """Provides a stateful method that returns the number of potential extra
1036 empty lines needed before and after the currently processed line.
1038 Note: this tracker works on lines that haven't been split yet. It assumes
1039 the prefix of the first leaf consists of optional newlines. Those newlines
1040 are consumed by `maybe_empty_lines()` and included in the computation.
1042 previous_line: Optional[Line] = None
1043 previous_after: int = 0
1044 previous_defs: List[int] = Factory(list)
1046 def maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
1047 """Return the number of extra empty lines before and after the `current_line`.
1049 This is for separating `def`, `async def` and `class` with extra empty
1050 lines (two on module-level), as well as providing an extra empty line
1051 after flow control keywords to make them more prominent.
1053 if isinstance(current_line, UnformattedLines):
1056 before, after = self._maybe_empty_lines(current_line)
1057 before -= self.previous_after
1058 self.previous_after = after
1059 self.previous_line = current_line
1060 return before, after
1062 def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
1064 if current_line.depth == 0:
1066 if current_line.leaves:
1067 # Consume the first leaf's extra newlines.
1068 first_leaf = current_line.leaves[0]
1069 before = first_leaf.prefix.count("\n")
1070 before = min(before, max_allowed)
1071 first_leaf.prefix = ""
1074 depth = current_line.depth
1075 while self.previous_defs and self.previous_defs[-1] >= depth:
1076 self.previous_defs.pop()
1077 before = 1 if depth else 2
1078 is_decorator = current_line.is_decorator
1079 if is_decorator or current_line.is_def or current_line.is_class:
1080 if not is_decorator:
1081 self.previous_defs.append(depth)
1082 if self.previous_line is None:
1083 # Don't insert empty lines before the first line in the file.
1086 if self.previous_line.is_decorator:
1090 self.previous_line.is_comment
1091 and self.previous_line.depth == current_line.depth
1097 if current_line.depth:
1103 and self.previous_line.is_import
1104 and not current_line.is_import
1105 and depth == self.previous_line.depth
1107 return (before or 1), 0
1113 class LineGenerator(Visitor[Line]):
1114 """Generates reformatted Line objects. Empty lines are not emitted.
1116 Note: destroys the tree it's visiting by mutating prefixes of its leaves
1117 in ways that will no longer stringify to valid Python code on the tree.
1119 current_line: Line = Factory(Line)
1121 def line(self, indent: int = 0, type: Type[Line] = Line) -> Iterator[Line]:
1124 If the line is empty, only emit if it makes sense.
1125 If the line is too long, split it first and then generate.
1127 If any lines were generated, set up a new current_line.
1129 if not self.current_line:
1130 if self.current_line.__class__ == type:
1131 self.current_line.depth += indent
1133 self.current_line = type(depth=self.current_line.depth + indent)
1134 return # Line is empty, don't emit. Creating a new one unnecessary.
1136 complete_line = self.current_line
1137 self.current_line = type(depth=complete_line.depth + indent)
1140 def visit(self, node: LN) -> Iterator[Line]:
1141 """Main method to visit `node` and its children.
1143 Yields :class:`Line` objects.
1145 if isinstance(self.current_line, UnformattedLines):
1146 # File contained `# fmt: off`
1147 yield from self.visit_unformatted(node)
1150 yield from super().visit(node)
1152 def visit_default(self, node: LN) -> Iterator[Line]:
1153 """Default `visit_*()` implementation. Recurses to children of `node`."""
1154 if isinstance(node, Leaf):
1155 any_open_brackets = self.current_line.bracket_tracker.any_open_brackets()
1157 for comment in generate_comments(node):
1158 if any_open_brackets:
1159 # any comment within brackets is subject to splitting
1160 self.current_line.append(comment)
1161 elif comment.type == token.COMMENT:
1162 # regular trailing comment
1163 self.current_line.append(comment)
1164 yield from self.line()
1167 # regular standalone comment
1168 yield from self.line()
1170 self.current_line.append(comment)
1171 yield from self.line()
1173 except FormatOff as f_off:
1174 f_off.trim_prefix(node)
1175 yield from self.line(type=UnformattedLines)
1176 yield from self.visit(node)
1178 except FormatOn as f_on:
1179 # This only happens here if somebody says "fmt: on" multiple
1181 f_on.trim_prefix(node)
1182 yield from self.visit_default(node)
1185 normalize_prefix(node, inside_brackets=any_open_brackets)
1186 if node.type == token.STRING:
1187 normalize_string_quotes(node)
1188 if node.type not in WHITESPACE:
1189 self.current_line.append(node)
1190 yield from super().visit_default(node)
1192 def visit_INDENT(self, node: Node) -> Iterator[Line]:
1193 """Increase indentation level, maybe yield a line."""
1194 # In blib2to3 INDENT never holds comments.
1195 yield from self.line(+1)
1196 yield from self.visit_default(node)
1198 def visit_DEDENT(self, node: Node) -> Iterator[Line]:
1199 """Decrease indentation level, maybe yield a line."""
1200 # The current line might still wait for trailing comments. At DEDENT time
1201 # there won't be any (they would be prefixes on the preceding NEWLINE).
1202 # Emit the line then.
1203 yield from self.line()
1205 # While DEDENT has no value, its prefix may contain standalone comments
1206 # that belong to the current indentation level. Get 'em.
1207 yield from self.visit_default(node)
1209 # Finally, emit the dedent.
1210 yield from self.line(-1)
1213 self, node: Node, keywords: Set[str], parens: Set[str]
1214 ) -> Iterator[Line]:
1215 """Visit a statement.
1217 This implementation is shared for `if`, `while`, `for`, `try`, `except`,
1218 `def`, `with`, `class`, and `assert`.
1220 The relevant Python language `keywords` for a given statement will be
1221 NAME leaves within it. This methods puts those on a separate line.
1223 `parens` holds pairs of nodes where invisible parentheses should be put.
1224 Keys hold nodes after which opening parentheses should be put, values
1225 hold nodes before which closing parentheses should be put.
1227 normalize_invisible_parens(node, parens_after=parens)
1228 for child in node.children:
1229 if child.type == token.NAME and child.value in keywords: # type: ignore
1230 yield from self.line()
1232 yield from self.visit(child)
1234 def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
1235 """Visit a statement without nested statements."""
1236 is_suite_like = node.parent and node.parent.type in STATEMENT
1238 yield from self.line(+1)
1239 yield from self.visit_default(node)
1240 yield from self.line(-1)
1243 yield from self.line()
1244 yield from self.visit_default(node)
1246 def visit_async_stmt(self, node: Node) -> Iterator[Line]:
1247 """Visit `async def`, `async for`, `async with`."""
1248 yield from self.line()
1250 children = iter(node.children)
1251 for child in children:
1252 yield from self.visit(child)
1254 if child.type == token.ASYNC:
1257 internal_stmt = next(children)
1258 for child in internal_stmt.children:
1259 yield from self.visit(child)
1261 def visit_decorators(self, node: Node) -> Iterator[Line]:
1262 """Visit decorators."""
1263 for child in node.children:
1264 yield from self.line()
1265 yield from self.visit(child)
1267 def visit_import_from(self, node: Node) -> Iterator[Line]:
1268 """Visit import_from and maybe put invisible parentheses.
1270 This is separate from `visit_stmt` because import statements don't
1271 support arbitrary atoms and thus handling of parentheses is custom.
1274 for index, child in enumerate(node.children):
1276 if child.type == token.LPAR:
1277 # make parentheses invisible
1278 child.value = "" # type: ignore
1279 node.children[-1].value = "" # type: ignore
1281 # insert invisible parentheses
1282 node.insert_child(index, Leaf(token.LPAR, ""))
1283 node.append_child(Leaf(token.RPAR, ""))
1287 child.type == token.NAME and child.value == "import" # type: ignore
1290 for child in node.children:
1291 yield from self.visit(child)
1293 def visit_SEMI(self, leaf: Leaf) -> Iterator[Line]:
1294 """Remove a semicolon and put the other statement on a separate line."""
1295 yield from self.line()
1297 def visit_ENDMARKER(self, leaf: Leaf) -> Iterator[Line]:
1298 """End of file. Process outstanding comments and end with a newline."""
1299 yield from self.visit_default(leaf)
1300 yield from self.line()
1302 def visit_unformatted(self, node: LN) -> Iterator[Line]:
1303 """Used when file contained a `# fmt: off`."""
1304 if isinstance(node, Node):
1305 for child in node.children:
1306 yield from self.visit(child)
1310 self.current_line.append(node)
1311 except FormatOn as f_on:
1312 f_on.trim_prefix(node)
1313 yield from self.line()
1314 yield from self.visit(node)
1316 if node.type == token.ENDMARKER:
1317 # somebody decided not to put a final `# fmt: on`
1318 yield from self.line()
1320 def __attrs_post_init__(self) -> None:
1321 """You are in a twisty little maze of passages."""
1324 self.visit_assert_stmt = partial(v, keywords={"assert"}, parens={"assert", ","})
1325 self.visit_if_stmt = partial(v, keywords={"if", "else", "elif"}, parens={"if"})
1326 self.visit_while_stmt = partial(v, keywords={"while", "else"}, parens={"while"})
1327 self.visit_for_stmt = partial(v, keywords={"for", "else"}, parens={"for", "in"})
1328 self.visit_try_stmt = partial(
1329 v, keywords={"try", "except", "else", "finally"}, parens=Ø
1331 self.visit_except_clause = partial(v, keywords={"except"}, parens=Ø)
1332 self.visit_with_stmt = partial(v, keywords={"with"}, parens=Ø)
1333 self.visit_funcdef = partial(v, keywords={"def"}, parens=Ø)
1334 self.visit_classdef = partial(v, keywords={"class"}, parens=Ø)
1335 self.visit_async_funcdef = self.visit_async_stmt
1336 self.visit_decorated = self.visit_decorators
1339 IMPLICIT_TUPLE = {syms.testlist, syms.testlist_star_expr, syms.exprlist}
1340 BRACKET = {token.LPAR: token.RPAR, token.LSQB: token.RSQB, token.LBRACE: token.RBRACE}
1341 OPENING_BRACKETS = set(BRACKET.keys())
1342 CLOSING_BRACKETS = set(BRACKET.values())
1343 BRACKETS = OPENING_BRACKETS | CLOSING_BRACKETS
1344 ALWAYS_NO_SPACE = CLOSING_BRACKETS | {token.COMMA, STANDALONE_COMMENT}
1347 def whitespace(leaf: Leaf, *, complex_subscript: bool) -> str: # noqa C901
1348 """Return whitespace prefix if needed for the given `leaf`.
1350 `complex_subscript` signals whether the given leaf is part of a subscription
1351 which has non-trivial arguments, like arithmetic expressions or function calls.
1359 if t in ALWAYS_NO_SPACE:
1362 if t == token.COMMENT:
1365 assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
1368 and p.type not in {syms.subscript, syms.subscriptlist, syms.sliceop}
1372 prev = leaf.prev_sibling
1374 prevp = preceding_leaf(p)
1375 if not prevp or prevp.type in OPENING_BRACKETS:
1378 if t == token.COLON:
1379 if prevp.type == token.COLON:
1382 elif prevp.type != token.COMMA and not complex_subscript:
1387 if prevp.type == token.EQUAL:
1389 if prevp.parent.type in {
1390 syms.arglist, syms.argument, syms.parameters, syms.varargslist
1394 elif prevp.parent.type == syms.typedargslist:
1395 # A bit hacky: if the equal sign has whitespace, it means we
1396 # previously found it's a typed argument. So, we're using
1400 elif prevp.type in STARS:
1401 if is_vararg(prevp, within=VARARGS_PARENTS | UNPACKING_PARENTS):
1404 elif prevp.type == token.COLON:
1405 if prevp.parent and prevp.parent.type in {syms.subscript, syms.sliceop}:
1406 return SPACE if complex_subscript else NO
1410 and prevp.parent.type == syms.factor
1411 and prevp.type in MATH_OPERATORS
1416 prevp.type == token.RIGHTSHIFT
1418 and prevp.parent.type == syms.shift_expr
1419 and prevp.prev_sibling
1420 and prevp.prev_sibling.type == token.NAME
1421 and prevp.prev_sibling.value == "print" # type: ignore
1423 # Python 2 print chevron
1426 elif prev.type in OPENING_BRACKETS:
1429 if p.type in {syms.parameters, syms.arglist}:
1430 # untyped function signatures or calls
1431 if not prev or prev.type != token.COMMA:
1434 elif p.type == syms.varargslist:
1436 if prev and prev.type != token.COMMA:
1439 elif p.type == syms.typedargslist:
1440 # typed function signatures
1444 if t == token.EQUAL:
1445 if prev.type != syms.tname:
1448 elif prev.type == token.EQUAL:
1449 # A bit hacky: if the equal sign has whitespace, it means we
1450 # previously found it's a typed argument. So, we're using that, too.
1453 elif prev.type != token.COMMA:
1456 elif p.type == syms.tname:
1459 prevp = preceding_leaf(p)
1460 if not prevp or prevp.type != token.COMMA:
1463 elif p.type == syms.trailer:
1464 # attributes and calls
1465 if t == token.LPAR or t == token.RPAR:
1470 prevp = preceding_leaf(p)
1471 if not prevp or prevp.type != token.NUMBER:
1474 elif t == token.LSQB:
1477 elif prev.type != token.COMMA:
1480 elif p.type == syms.argument:
1482 if t == token.EQUAL:
1486 prevp = preceding_leaf(p)
1487 if not prevp or prevp.type == token.LPAR:
1490 elif prev.type in {token.EQUAL} | STARS:
1493 elif p.type == syms.decorator:
1497 elif p.type == syms.dotted_name:
1501 prevp = preceding_leaf(p)
1502 if not prevp or prevp.type == token.AT or prevp.type == token.DOT:
1505 elif p.type == syms.classdef:
1509 if prev and prev.type == token.LPAR:
1512 elif p.type in {syms.subscript, syms.sliceop}:
1515 assert p.parent is not None, "subscripts are always parented"
1516 if p.parent.type == syms.subscriptlist:
1521 elif not complex_subscript:
1524 elif p.type == syms.atom:
1525 if prev and t == token.DOT:
1526 # dots, but not the first one.
1529 elif p.type == syms.dictsetmaker:
1531 if prev and prev.type == token.DOUBLESTAR:
1534 elif p.type in {syms.factor, syms.star_expr}:
1537 prevp = preceding_leaf(p)
1538 if not prevp or prevp.type in OPENING_BRACKETS:
1541 prevp_parent = prevp.parent
1542 assert prevp_parent is not None
1544 prevp.type == token.COLON
1545 and prevp_parent.type in {syms.subscript, syms.sliceop}
1549 elif prevp.type == token.EQUAL and prevp_parent.type == syms.argument:
1552 elif t == token.NAME or t == token.NUMBER:
1555 elif p.type == syms.import_from:
1557 if prev and prev.type == token.DOT:
1560 elif t == token.NAME:
1564 if prev and prev.type == token.DOT:
1567 elif p.type == syms.sliceop:
1573 def preceding_leaf(node: Optional[LN]) -> Optional[Leaf]:
1574 """Return the first leaf that precedes `node`, if any."""
1576 res = node.prev_sibling
1578 if isinstance(res, Leaf):
1582 return list(res.leaves())[-1]
1591 def child_towards(ancestor: Node, descendant: LN) -> Optional[LN]:
1592 """Return the child of `ancestor` that contains `descendant`."""
1593 node: Optional[LN] = descendant
1594 while node and node.parent != ancestor:
1599 def is_split_after_delimiter(leaf: Leaf, previous: Leaf = None) -> int:
1600 """Return the priority of the `leaf` delimiter, given a line break after it.
1602 The delimiter priorities returned here are from those delimiters that would
1603 cause a line break after themselves.
1605 Higher numbers are higher priority.
1607 if leaf.type == token.COMMA:
1608 return COMMA_PRIORITY
1613 def is_split_before_delimiter(leaf: Leaf, previous: Leaf = None) -> int:
1614 """Return the priority of the `leaf` delimiter, given a line before after it.
1616 The delimiter priorities returned here are from those delimiters that would
1617 cause a line break before themselves.
1619 Higher numbers are higher priority.
1621 if is_vararg(leaf, within=VARARGS_PARENTS | UNPACKING_PARENTS):
1622 # * and ** might also be MATH_OPERATORS but in this case they are not.
1623 # Don't treat them as a delimiter.
1627 leaf.type in MATH_OPERATORS
1629 and leaf.parent.type not in {syms.factor, syms.star_expr}
1631 return MATH_PRIORITY
1633 if leaf.type in COMPARATORS:
1634 return COMPARATOR_PRIORITY
1637 leaf.type == token.STRING
1638 and previous is not None
1639 and previous.type == token.STRING
1641 return STRING_PRIORITY
1644 leaf.type == token.NAME
1645 and leaf.value == "for"
1647 and leaf.parent.type in {syms.comp_for, syms.old_comp_for}
1649 return COMPREHENSION_PRIORITY
1652 leaf.type == token.NAME
1653 and leaf.value == "if"
1655 and leaf.parent.type in {syms.comp_if, syms.old_comp_if}
1657 return COMPREHENSION_PRIORITY
1660 leaf.type == token.NAME
1661 and leaf.value in {"if", "else"}
1663 and leaf.parent.type == syms.test
1665 return TERNARY_PRIORITY
1667 if leaf.type == token.NAME and leaf.value in LOGIC_OPERATORS and leaf.parent:
1668 return LOGIC_PRIORITY
1673 def generate_comments(leaf: Leaf) -> Iterator[Leaf]:
1674 """Clean the prefix of the `leaf` and generate comments from it, if any.
1676 Comments in lib2to3 are shoved into the whitespace prefix. This happens
1677 in `pgen2/driver.py:Driver.parse_tokens()`. This was a brilliant implementation
1678 move because it does away with modifying the grammar to include all the
1679 possible places in which comments can be placed.
1681 The sad consequence for us though is that comments don't "belong" anywhere.
1682 This is why this function generates simple parentless Leaf objects for
1683 comments. We simply don't know what the correct parent should be.
1685 No matter though, we can live without this. We really only need to
1686 differentiate between inline and standalone comments. The latter don't
1687 share the line with any code.
1689 Inline comments are emitted as regular token.COMMENT leaves. Standalone
1690 are emitted with a fake STANDALONE_COMMENT token identifier.
1701 for index, line in enumerate(p.split("\n")):
1702 consumed += len(line) + 1 # adding the length of the split '\n'
1703 line = line.lstrip()
1706 if not line.startswith("#"):
1709 if index == 0 and leaf.type != token.ENDMARKER:
1710 comment_type = token.COMMENT # simple trailing comment
1712 comment_type = STANDALONE_COMMENT
1713 comment = make_comment(line)
1714 yield Leaf(comment_type, comment, prefix="\n" * nlines)
1716 if comment in {"# fmt: on", "# yapf: enable"}:
1717 raise FormatOn(consumed)
1719 if comment in {"# fmt: off", "# yapf: disable"}:
1720 if comment_type == STANDALONE_COMMENT:
1721 raise FormatOff(consumed)
1723 prev = preceding_leaf(leaf)
1724 if not prev or prev.type in WHITESPACE: # standalone comment in disguise
1725 raise FormatOff(consumed)
1730 def make_comment(content: str) -> str:
1731 """Return a consistently formatted comment from the given `content` string.
1733 All comments (except for "##", "#!", "#:") should have a single space between
1734 the hash sign and the content.
1736 If `content` didn't start with a hash sign, one is provided.
1738 content = content.rstrip()
1742 if content[0] == "#":
1743 content = content[1:]
1744 if content and content[0] not in " !:#":
1745 content = " " + content
1746 return "#" + content
1750 line: Line, line_length: int, inner: bool = False, py36: bool = False
1751 ) -> Iterator[Line]:
1752 """Split a `line` into potentially many lines.
1754 They should fit in the allotted `line_length` but might not be able to.
1755 `inner` signifies that there were a pair of brackets somewhere around the
1756 current `line`, possibly transitively. This means we can fallback to splitting
1757 by delimiters if the LHS/RHS don't yield any results.
1759 If `py36` is True, splitting may generate syntax that is only compatible
1760 with Python 3.6 and later.
1762 if isinstance(line, UnformattedLines) or line.is_comment:
1766 line_str = str(line).strip("\n")
1768 len(line_str) <= line_length
1769 and "\n" not in line_str # multiline strings
1770 and not line.contains_standalone_comments()
1775 split_funcs: List[SplitFunc]
1777 split_funcs = [left_hand_split]
1778 elif line.is_import:
1779 split_funcs = [explode_split]
1780 elif line.inside_brackets:
1781 split_funcs = [delimiter_split, standalone_comment_split, right_hand_split]
1783 split_funcs = [right_hand_split]
1784 for split_func in split_funcs:
1785 # We are accumulating lines in `result` because we might want to abort
1786 # mission and return the original line in the end, or attempt a different
1788 result: List[Line] = []
1790 for l in split_func(line, py36):
1791 if str(l).strip("\n") == line_str:
1792 raise CannotSplit("Split function returned an unchanged result")
1795 split_line(l, line_length=line_length, inner=True, py36=py36)
1797 except CannotSplit as cs:
1808 def left_hand_split(line: Line, py36: bool = False) -> Iterator[Line]:
1809 """Split line into many lines, starting with the first matching bracket pair.
1811 Note: this usually looks weird, only use this for function definitions.
1812 Prefer RHS otherwise.
1814 head = Line(depth=line.depth)
1815 body = Line(depth=line.depth + 1, inside_brackets=True)
1816 tail = Line(depth=line.depth)
1817 tail_leaves: List[Leaf] = []
1818 body_leaves: List[Leaf] = []
1819 head_leaves: List[Leaf] = []
1820 current_leaves = head_leaves
1821 matching_bracket = None
1822 for leaf in line.leaves:
1824 current_leaves is body_leaves
1825 and leaf.type in CLOSING_BRACKETS
1826 and leaf.opening_bracket is matching_bracket
1828 current_leaves = tail_leaves if body_leaves else head_leaves
1829 current_leaves.append(leaf)
1830 if current_leaves is head_leaves:
1831 if leaf.type in OPENING_BRACKETS:
1832 matching_bracket = leaf
1833 current_leaves = body_leaves
1834 # Since body is a new indent level, remove spurious leading whitespace.
1836 normalize_prefix(body_leaves[0], inside_brackets=True)
1837 # Build the new lines.
1838 for result, leaves in (head, head_leaves), (body, body_leaves), (tail, tail_leaves):
1840 result.append(leaf, preformatted=True)
1841 for comment_after in line.comments_after(leaf):
1842 result.append(comment_after, preformatted=True)
1843 bracket_split_succeeded_or_raise(head, body, tail)
1844 for result in (head, body, tail):
1849 def right_hand_split(
1850 line: Line, py36: bool = False, omit: Collection[LeafID] = ()
1851 ) -> Iterator[Line]:
1852 """Split line into many lines, starting with the last matching bracket pair."""
1853 head = Line(depth=line.depth)
1854 body = Line(depth=line.depth + 1, inside_brackets=True)
1855 tail = Line(depth=line.depth)
1856 tail_leaves: List[Leaf] = []
1857 body_leaves: List[Leaf] = []
1858 head_leaves: List[Leaf] = []
1859 current_leaves = tail_leaves
1860 opening_bracket = None
1861 closing_bracket = None
1862 for leaf in reversed(line.leaves):
1863 if current_leaves is body_leaves:
1864 if leaf is opening_bracket:
1865 current_leaves = head_leaves if body_leaves else tail_leaves
1866 current_leaves.append(leaf)
1867 if current_leaves is tail_leaves:
1868 if leaf.type in CLOSING_BRACKETS and id(leaf) not in omit:
1869 opening_bracket = leaf.opening_bracket
1870 closing_bracket = leaf
1871 current_leaves = body_leaves
1872 tail_leaves.reverse()
1873 body_leaves.reverse()
1874 head_leaves.reverse()
1875 # Since body is a new indent level, remove spurious leading whitespace.
1877 normalize_prefix(body_leaves[0], inside_brackets=True)
1878 elif not head_leaves:
1879 # No `head` and no `body` means the split failed. `tail` has all content.
1880 raise CannotSplit("No brackets found")
1882 # Build the new lines.
1883 for result, leaves in (head, head_leaves), (body, body_leaves), (tail, tail_leaves):
1885 result.append(leaf, preformatted=True)
1886 for comment_after in line.comments_after(leaf):
1887 result.append(comment_after, preformatted=True)
1888 bracket_split_succeeded_or_raise(head, body, tail)
1889 assert opening_bracket and closing_bracket
1891 opening_bracket.type == token.LPAR
1892 and not opening_bracket.value
1893 and closing_bracket.type == token.RPAR
1894 and not closing_bracket.value
1896 # These parens were optional. If there aren't any delimiters or standalone
1897 # comments in the body, they were unnecessary and another split without
1898 # them should be attempted.
1900 body.bracket_tracker.delimiters or line.contains_standalone_comments(0)
1902 omit = {id(closing_bracket), *omit}
1903 yield from right_hand_split(line, py36=py36, omit=omit)
1906 ensure_visible(opening_bracket)
1907 ensure_visible(closing_bracket)
1908 for result in (head, body, tail):
1913 def bracket_split_succeeded_or_raise(head: Line, body: Line, tail: Line) -> None:
1914 """Raise :exc:`CannotSplit` if the last left- or right-hand split failed.
1916 Do nothing otherwise.
1918 A left- or right-hand split is based on a pair of brackets. Content before
1919 (and including) the opening bracket is left on one line, content inside the
1920 brackets is put on a separate line, and finally content starting with and
1921 following the closing bracket is put on a separate line.
1923 Those are called `head`, `body`, and `tail`, respectively. If the split
1924 produced the same line (all content in `head`) or ended up with an empty `body`
1925 and the `tail` is just the closing bracket, then it's considered failed.
1927 tail_len = len(str(tail).strip())
1930 raise CannotSplit("Splitting brackets produced the same line")
1934 f"Splitting brackets on an empty body to save "
1935 f"{tail_len} characters is not worth it"
1939 def dont_increase_indentation(split_func: SplitFunc) -> SplitFunc:
1940 """Normalize prefix of the first leaf in every line returned by `split_func`.
1942 This is a decorator over relevant split functions.
1946 def split_wrapper(line: Line, py36: bool = False) -> Iterator[Line]:
1947 for l in split_func(line, py36):
1948 normalize_prefix(l.leaves[0], inside_brackets=True)
1951 return split_wrapper
1954 @dont_increase_indentation
1955 def delimiter_split(line: Line, py36: bool = False) -> Iterator[Line]:
1956 """Split according to delimiters of the highest priority.
1958 If `py36` is True, the split will add trailing commas also in function
1959 signatures that contain `*` and `**`.
1962 last_leaf = line.leaves[-1]
1964 raise CannotSplit("Line empty")
1966 delimiters = line.bracket_tracker.delimiters
1968 delimiter_priority = line.bracket_tracker.max_delimiter_priority(
1969 exclude={id(last_leaf)}
1972 raise CannotSplit("No delimiters found")
1974 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
1975 lowest_depth = sys.maxsize
1976 trailing_comma_safe = True
1978 def append_to_line(leaf: Leaf) -> Iterator[Line]:
1979 """Append `leaf` to current line or to new line if appending impossible."""
1980 nonlocal current_line
1982 current_line.append_safe(leaf, preformatted=True)
1983 except ValueError as ve:
1986 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
1987 current_line.append(leaf)
1989 for leaf in line.leaves:
1990 yield from append_to_line(leaf)
1992 for comment_after in line.comments_after(leaf):
1993 yield from append_to_line(comment_after)
1995 lowest_depth = min(lowest_depth, leaf.bracket_depth)
1997 leaf.bracket_depth == lowest_depth
1998 and is_vararg(leaf, within=VARARGS_PARENTS)
2000 trailing_comma_safe = trailing_comma_safe and py36
2001 leaf_priority = delimiters.get(id(leaf))
2002 if leaf_priority == delimiter_priority:
2005 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
2009 and delimiter_priority == COMMA_PRIORITY
2010 and current_line.leaves[-1].type != token.COMMA
2011 and current_line.leaves[-1].type != STANDALONE_COMMENT
2013 current_line.append(Leaf(token.COMMA, ","))
2017 @dont_increase_indentation
2018 def standalone_comment_split(line: Line, py36: bool = False) -> Iterator[Line]:
2019 """Split standalone comments from the rest of the line."""
2020 if not line.contains_standalone_comments(0):
2021 raise CannotSplit("Line does not have any standalone comments")
2023 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
2025 def append_to_line(leaf: Leaf) -> Iterator[Line]:
2026 """Append `leaf` to current line or to new line if appending impossible."""
2027 nonlocal current_line
2029 current_line.append_safe(leaf, preformatted=True)
2030 except ValueError as ve:
2033 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
2034 current_line.append(leaf)
2036 for leaf in line.leaves:
2037 yield from append_to_line(leaf)
2039 for comment_after in line.comments_after(leaf):
2040 yield from append_to_line(comment_after)
2047 line: Line, py36: bool = False, omit: Collection[LeafID] = ()
2048 ) -> Iterator[Line]:
2049 """Split by rightmost bracket and immediately split contents by a delimiter."""
2050 new_lines = list(right_hand_split(line, py36, omit))
2051 if len(new_lines) != 3:
2052 yield from new_lines
2058 yield from delimiter_split(new_lines[1], py36)
2066 def is_import(leaf: Leaf) -> bool:
2067 """Return True if the given leaf starts an import statement."""
2074 (v == "import" and p and p.type == syms.import_name)
2075 or (v == "from" and p and p.type == syms.import_from)
2080 def normalize_prefix(leaf: Leaf, *, inside_brackets: bool) -> None:
2081 """Leave existing extra newlines if not `inside_brackets`. Remove everything
2084 Note: don't use backslashes for formatting or you'll lose your voting rights.
2086 if not inside_brackets:
2087 spl = leaf.prefix.split("#")
2088 if "\\" not in spl[0]:
2089 nl_count = spl[-1].count("\n")
2092 leaf.prefix = "\n" * nl_count
2098 def normalize_string_quotes(leaf: Leaf) -> None:
2099 """Prefer double quotes but only if it doesn't cause more escaping.
2101 Adds or removes backslashes as appropriate. Doesn't parse and fix
2102 strings nested in f-strings (yet).
2104 Note: Mutates its argument.
2106 value = leaf.value.lstrip("furbFURB")
2107 if value[:3] == '"""':
2110 elif value[:3] == "'''":
2113 elif value[0] == '"':
2119 first_quote_pos = leaf.value.find(orig_quote)
2120 if first_quote_pos == -1:
2121 return # There's an internal error
2123 prefix = leaf.value[:first_quote_pos]
2124 unescaped_new_quote = re.compile(rf"(([^\\]|^)(\\\\)*){new_quote}")
2125 escaped_new_quote = re.compile(rf"([^\\]|^)\\(\\\\)*{new_quote}")
2126 escaped_orig_quote = re.compile(rf"([^\\]|^)\\(\\\\)*{orig_quote}")
2127 body = leaf.value[first_quote_pos + len(orig_quote) : -len(orig_quote)]
2128 if "r" in prefix.casefold():
2129 if unescaped_new_quote.search(body):
2130 # There's at least one unescaped new_quote in this raw string
2131 # so converting is impossible
2134 # Do not introduce or remove backslashes in raw strings
2137 # remove unnecessary quotes
2138 new_body = sub_twice(escaped_new_quote, rf"\1\2{new_quote}", body)
2139 if body != new_body:
2140 # Consider the string without unnecessary quotes as the original
2142 leaf.value = f"{prefix}{orig_quote}{body}{orig_quote}"
2143 new_body = sub_twice(escaped_orig_quote, rf"\1\2{orig_quote}", new_body)
2144 new_body = sub_twice(unescaped_new_quote, rf"\1\\{new_quote}", new_body)
2145 if new_quote == '"""' and new_body[-1] == '"':
2147 new_body = new_body[:-1] + '\\"'
2148 orig_escape_count = body.count("\\")
2149 new_escape_count = new_body.count("\\")
2150 if new_escape_count > orig_escape_count:
2151 return # Do not introduce more escaping
2153 if new_escape_count == orig_escape_count and orig_quote == '"':
2154 return # Prefer double quotes
2156 leaf.value = f"{prefix}{new_quote}{new_body}{new_quote}"
2159 def normalize_invisible_parens(node: Node, parens_after: Set[str]) -> None:
2160 """Make existing optional parentheses invisible or create new ones.
2162 Standardizes on visible parentheses for single-element tuples, and keeps
2163 existing visible parentheses for other tuples and generator expressions.
2166 for child in list(node.children):
2168 if child.type == syms.atom:
2170 is_empty_tuple(child)
2171 or is_one_tuple(child)
2172 or max_delimiter_priority_in_atom(child) >= COMMA_PRIORITY
2174 first = child.children[0]
2175 last = child.children[-1]
2176 if first.type == token.LPAR and last.type == token.RPAR:
2177 # make parentheses invisible
2178 first.value = "" # type: ignore
2179 last.value = "" # type: ignore
2180 elif is_one_tuple(child):
2181 # wrap child in visible parentheses
2182 lpar = Leaf(token.LPAR, "(")
2183 rpar = Leaf(token.RPAR, ")")
2184 index = child.remove() or 0
2185 node.insert_child(index, Node(syms.atom, [lpar, child, rpar]))
2187 # wrap child in invisible parentheses
2188 lpar = Leaf(token.LPAR, "")
2189 rpar = Leaf(token.RPAR, "")
2190 index = child.remove() or 0
2191 node.insert_child(index, Node(syms.atom, [lpar, child, rpar]))
2193 check_lpar = isinstance(child, Leaf) and child.value in parens_after
2196 def is_empty_tuple(node: LN) -> bool:
2197 """Return True if `node` holds an empty tuple."""
2199 node.type == syms.atom
2200 and len(node.children) == 2
2201 and node.children[0].type == token.LPAR
2202 and node.children[1].type == token.RPAR
2206 def is_one_tuple(node: LN) -> bool:
2207 """Return True if `node` holds a tuple with one element, with or without parens."""
2208 if node.type == syms.atom:
2209 if len(node.children) != 3:
2212 lpar, gexp, rpar = node.children
2214 lpar.type == token.LPAR
2215 and gexp.type == syms.testlist_gexp
2216 and rpar.type == token.RPAR
2220 return len(gexp.children) == 2 and gexp.children[1].type == token.COMMA
2223 node.type in IMPLICIT_TUPLE
2224 and len(node.children) == 2
2225 and node.children[1].type == token.COMMA
2229 def is_vararg(leaf: Leaf, within: Set[NodeType]) -> bool:
2230 """Return True if `leaf` is a star or double star in a vararg or kwarg.
2232 If `within` includes VARARGS_PARENTS, this applies to function signatures.
2233 If `within` includes COLLECTION_LIBERALS_PARENTS, it applies to right
2234 hand-side extended iterable unpacking (PEP 3132) and additional unpacking
2235 generalizations (PEP 448).
2237 if leaf.type not in STARS or not leaf.parent:
2241 if p.type == syms.star_expr:
2242 # Star expressions are also used as assignment targets in extended
2243 # iterable unpacking (PEP 3132). See what its parent is instead.
2249 return p.type in within
2252 def max_delimiter_priority_in_atom(node: LN) -> int:
2253 """Return maximum delimiter priority inside `node`.
2255 This is specific to atoms with contents contained in a pair of parentheses.
2256 If `node` isn't an atom or there are no enclosing parentheses, returns 0.
2258 if node.type != syms.atom:
2261 first = node.children[0]
2262 last = node.children[-1]
2263 if not (first.type == token.LPAR and last.type == token.RPAR):
2266 bt = BracketTracker()
2267 for c in node.children[1:-1]:
2268 if isinstance(c, Leaf):
2271 for leaf in c.leaves():
2274 return bt.max_delimiter_priority()
2280 def ensure_visible(leaf: Leaf) -> None:
2281 """Make sure parentheses are visible.
2283 They could be invisible as part of some statements (see
2284 :func:`normalize_invible_parens` and :func:`visit_import_from`).
2286 if leaf.type == token.LPAR:
2288 elif leaf.type == token.RPAR:
2292 def is_python36(node: Node) -> bool:
2293 """Return True if the current file is using Python 3.6+ features.
2295 Currently looking for:
2297 - trailing commas after * or ** in function signatures.
2299 for n in node.pre_order():
2300 if n.type == token.STRING:
2301 value_head = n.value[:2] # type: ignore
2302 if value_head in {'f"', 'F"', "f'", "F'", "rf", "fr", "RF", "FR"}:
2306 n.type == syms.typedargslist
2308 and n.children[-1].type == token.COMMA
2310 for ch in n.children:
2311 if ch.type in STARS:
2317 PYTHON_EXTENSIONS = {".py"}
2318 BLACKLISTED_DIRECTORIES = {
2319 "build", "buck-out", "dist", "_build", ".git", ".hg", ".mypy_cache", ".tox", ".venv"
2323 def gen_python_files_in_dir(path: Path) -> Iterator[Path]:
2324 """Generate all files under `path` which aren't under BLACKLISTED_DIRECTORIES
2325 and have one of the PYTHON_EXTENSIONS.
2327 for child in path.iterdir():
2329 if child.name in BLACKLISTED_DIRECTORIES:
2332 yield from gen_python_files_in_dir(child)
2334 elif child.suffix in PYTHON_EXTENSIONS:
2340 """Provides a reformatting counter. Can be rendered with `str(report)`."""
2343 change_count: int = 0
2345 failure_count: int = 0
2347 def done(self, src: Path, changed: Changed) -> None:
2348 """Increment the counter for successful reformatting. Write out a message."""
2349 if changed is Changed.YES:
2350 reformatted = "would reformat" if self.check else "reformatted"
2352 out(f"{reformatted} {src}")
2353 self.change_count += 1
2356 if changed is Changed.NO:
2357 msg = f"{src} already well formatted, good job."
2359 msg = f"{src} wasn't modified on disk since last run."
2360 out(msg, bold=False)
2361 self.same_count += 1
2363 def failed(self, src: Path, message: str) -> None:
2364 """Increment the counter for failed reformatting. Write out a message."""
2365 err(f"error: cannot format {src}: {message}")
2366 self.failure_count += 1
2369 def return_code(self) -> int:
2370 """Return the exit code that the app should use.
2372 This considers the current state of changed files and failures:
2373 - if there were any failures, return 123;
2374 - if any files were changed and --check is being used, return 1;
2375 - otherwise return 0.
2377 # According to http://tldp.org/LDP/abs/html/exitcodes.html starting with
2378 # 126 we have special returncodes reserved by the shell.
2379 if self.failure_count:
2382 elif self.change_count and self.check:
2387 def __str__(self) -> str:
2388 """Render a color report of the current state.
2390 Use `click.unstyle` to remove colors.
2393 reformatted = "would be reformatted"
2394 unchanged = "would be left unchanged"
2395 failed = "would fail to reformat"
2397 reformatted = "reformatted"
2398 unchanged = "left unchanged"
2399 failed = "failed to reformat"
2401 if self.change_count:
2402 s = "s" if self.change_count > 1 else ""
2404 click.style(f"{self.change_count} file{s} {reformatted}", bold=True)
2407 s = "s" if self.same_count > 1 else ""
2408 report.append(f"{self.same_count} file{s} {unchanged}")
2409 if self.failure_count:
2410 s = "s" if self.failure_count > 1 else ""
2412 click.style(f"{self.failure_count} file{s} {failed}", fg="red")
2414 return ", ".join(report) + "."
2417 def assert_equivalent(src: str, dst: str) -> None:
2418 """Raise AssertionError if `src` and `dst` aren't equivalent."""
2423 def _v(node: ast.AST, depth: int = 0) -> Iterator[str]:
2424 """Simple visitor generating strings to compare ASTs by content."""
2425 yield f"{' ' * depth}{node.__class__.__name__}("
2427 for field in sorted(node._fields):
2429 value = getattr(node, field)
2430 except AttributeError:
2433 yield f"{' ' * (depth+1)}{field}="
2435 if isinstance(value, list):
2437 if isinstance(item, ast.AST):
2438 yield from _v(item, depth + 2)
2440 elif isinstance(value, ast.AST):
2441 yield from _v(value, depth + 2)
2444 yield f"{' ' * (depth+2)}{value!r}, # {value.__class__.__name__}"
2446 yield f"{' ' * depth}) # /{node.__class__.__name__}"
2449 src_ast = ast.parse(src)
2450 except Exception as exc:
2451 major, minor = sys.version_info[:2]
2452 raise AssertionError(
2453 f"cannot use --safe with this file; failed to parse source file "
2454 f"with Python {major}.{minor}'s builtin AST. Re-run with --fast "
2455 f"or stop using deprecated Python 2 syntax. AST error message: {exc}"
2459 dst_ast = ast.parse(dst)
2460 except Exception as exc:
2461 log = dump_to_file("".join(traceback.format_tb(exc.__traceback__)), dst)
2462 raise AssertionError(
2463 f"INTERNAL ERROR: Black produced invalid code: {exc}. "
2464 f"Please report a bug on https://github.com/ambv/black/issues. "
2465 f"This invalid output might be helpful: {log}"
2468 src_ast_str = "\n".join(_v(src_ast))
2469 dst_ast_str = "\n".join(_v(dst_ast))
2470 if src_ast_str != dst_ast_str:
2471 log = dump_to_file(diff(src_ast_str, dst_ast_str, "src", "dst"))
2472 raise AssertionError(
2473 f"INTERNAL ERROR: Black produced code that is not equivalent to "
2475 f"Please report a bug on https://github.com/ambv/black/issues. "
2476 f"This diff might be helpful: {log}"
2480 def assert_stable(src: str, dst: str, line_length: int) -> None:
2481 """Raise AssertionError if `dst` reformats differently the second time."""
2482 newdst = format_str(dst, line_length=line_length)
2485 diff(src, dst, "source", "first pass"),
2486 diff(dst, newdst, "first pass", "second pass"),
2488 raise AssertionError(
2489 f"INTERNAL ERROR: Black produced different code on the second pass "
2490 f"of the formatter. "
2491 f"Please report a bug on https://github.com/ambv/black/issues. "
2492 f"This diff might be helpful: {log}"
2496 def dump_to_file(*output: str) -> str:
2497 """Dump `output` to a temporary file. Return path to the file."""
2500 with tempfile.NamedTemporaryFile(
2501 mode="w", prefix="blk_", suffix=".log", delete=False, encoding="utf8"
2503 for lines in output:
2505 if lines and lines[-1] != "\n":
2510 def diff(a: str, b: str, a_name: str, b_name: str) -> str:
2511 """Return a unified diff string between strings `a` and `b`."""
2514 a_lines = [line + "\n" for line in a.split("\n")]
2515 b_lines = [line + "\n" for line in b.split("\n")]
2517 difflib.unified_diff(a_lines, b_lines, fromfile=a_name, tofile=b_name, n=5)
2521 def cancel(tasks: List[asyncio.Task]) -> None:
2522 """asyncio signal handler that cancels all `tasks` and reports to stderr."""
2528 def shutdown(loop: BaseEventLoop) -> None:
2529 """Cancel all pending tasks on `loop`, wait for them, and close the loop."""
2531 # This part is borrowed from asyncio/runners.py in Python 3.7b2.
2532 to_cancel = [task for task in asyncio.Task.all_tasks(loop) if not task.done()]
2536 for task in to_cancel:
2538 loop.run_until_complete(
2539 asyncio.gather(*to_cancel, loop=loop, return_exceptions=True)
2542 # `concurrent.futures.Future` objects cannot be cancelled once they
2543 # are already running. There might be some when the `shutdown()` happened.
2544 # Silence their logger's spew about the event loop being closed.
2545 cf_logger = logging.getLogger("concurrent.futures")
2546 cf_logger.setLevel(logging.CRITICAL)
2550 def sub_twice(regex: Pattern[str], replacement: str, original: str) -> str:
2551 """Replace `regex` with `replacement` twice on `original`.
2553 This is used by string normalization to perform replaces on
2554 overlapping matches.
2556 return regex.sub(replacement, regex.sub(replacement, original))
2559 CACHE_DIR = Path(user_cache_dir("black", version=__version__))
2562 def get_cache_file(line_length: int) -> Path:
2563 return CACHE_DIR / f"cache.{line_length}.pickle"
2566 def read_cache(line_length: int) -> Cache:
2567 """Read the cache if it exists and is well formed.
2569 If it is not well formed, the call to write_cache later should resolve the issue.
2571 cache_file = get_cache_file(line_length)
2572 if not cache_file.exists():
2575 with cache_file.open("rb") as fobj:
2577 cache: Cache = pickle.load(fobj)
2578 except pickle.UnpicklingError:
2584 def get_cache_info(path: Path) -> CacheInfo:
2585 """Return the information used to check if a file is already formatted or not."""
2587 return stat.st_mtime, stat.st_size
2591 cache: Cache, sources: Iterable[Path]
2592 ) -> Tuple[List[Path], List[Path]]:
2593 """Split a list of paths into two.
2595 The first list contains paths of files that modified on disk or are not in the
2596 cache. The other list contains paths to non-modified files.
2601 if cache.get(src) != get_cache_info(src):
2608 def write_cache(cache: Cache, sources: List[Path], line_length: int) -> None:
2609 """Update the cache file."""
2610 cache_file = get_cache_file(line_length)
2612 if not CACHE_DIR.exists():
2613 CACHE_DIR.mkdir(parents=True)
2614 new_cache = {**cache, **{src.resolve(): get_cache_info(src) for src in sources}}
2615 with cache_file.open("wb") as fobj:
2616 pickle.dump(new_cache, fobj, protocol=pickle.HIGHEST_PROTOCOL)
2621 if __name__ == "__main__":