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.
3 from asyncio.base_events import BaseEventLoop
4 from concurrent.futures import Executor, ProcessPoolExecutor
5 from enum import Enum, Flag
6 from functools import partial, wraps
9 from multiprocessing import Manager
11 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
47 __version__ = "18.5b1"
48 DEFAULT_LINE_LENGTH = 88
49 CACHE_DIR = Path(user_cache_dir("black", version=__version__))
60 LN = Union[Leaf, Node]
61 SplitFunc = Callable[["Line", bool], Iterator["Line"]]
64 CacheInfo = Tuple[Timestamp, FileSize]
65 Cache = Dict[Path, CacheInfo]
66 out = partial(click.secho, bold=True, err=True)
67 err = partial(click.secho, fg="red", err=True)
69 pygram.initialize(CACHE_DIR)
70 syms = pygram.python_symbols
73 class NothingChanged(UserWarning):
74 """Raised by :func:`format_file` when reformatted code is the same as source."""
77 class CannotSplit(Exception):
78 """A readable split that fits the allotted line length is impossible.
80 Raised by :func:`left_hand_split`, :func:`right_hand_split`, and
81 :func:`delimiter_split`.
85 class FormatError(Exception):
86 """Base exception for `# fmt: on` and `# fmt: off` handling.
88 It holds the number of bytes of the prefix consumed before the format
89 control comment appeared.
92 def __init__(self, consumed: int) -> None:
93 super().__init__(consumed)
94 self.consumed = consumed
96 def trim_prefix(self, leaf: Leaf) -> None:
97 leaf.prefix = leaf.prefix[self.consumed :]
99 def leaf_from_consumed(self, leaf: Leaf) -> Leaf:
100 """Returns a new Leaf from the consumed part of the prefix."""
101 unformatted_prefix = leaf.prefix[: self.consumed]
102 return Leaf(token.NEWLINE, unformatted_prefix)
105 class FormatOn(FormatError):
106 """Found a comment like `# fmt: on` in the file."""
109 class FormatOff(FormatError):
110 """Found a comment like `# fmt: off` in the file."""
113 class WriteBack(Enum):
125 class FileMode(Flag):
129 NO_STRING_NORMALIZATION = 4
137 default=DEFAULT_LINE_LENGTH,
138 help="How many character per line to allow.",
145 "Don't write the files back, just return the status. Return code 0 "
146 "means nothing would change. Return code 1 means some files would be "
147 "reformatted. Return code 123 means there was an internal error."
153 help="Don't write the files back, just output a diff for each file on stdout.",
158 help="If --fast given, skip temporary sanity checks. [default: --safe]",
165 "Don't emit non-error messages to stderr. Errors are still emitted, "
166 "silence those with 2>/dev/null."
173 "Consider all input files typing stubs regardless of file extension "
174 "(useful when piping source on standard input)."
181 "Allow using Python 3.6-only syntax on all input files. This will put "
182 "trailing commas in function signatures and calls also after *args and "
183 "**kwargs. [default: per-file auto-detection]"
188 "--skip-string-normalization",
190 help="Don't normalize string quotes or prefixes.",
192 @click.version_option(version=__version__)
197 exists=True, file_okay=True, dir_okay=True, readable=True, allow_dash=True
209 skip_string_normalization: bool,
213 """The uncompromising code formatter."""
214 sources: List[Path] = []
218 sources.extend(gen_python_files_in_dir(p))
220 # if a file was explicitly given, we don't care about its extension
223 sources.append(Path("-"))
225 err(f"invalid path: {s}")
227 if check and not diff:
228 write_back = WriteBack.NO
230 write_back = WriteBack.DIFF
232 write_back = WriteBack.YES
233 mode = FileMode.AUTO_DETECT
235 mode |= FileMode.PYTHON36
238 if skip_string_normalization:
239 mode |= FileMode.NO_STRING_NORMALIZATION
240 report = Report(check=check, quiet=quiet)
241 if len(sources) == 0:
242 out("No paths given. Nothing to do 😴")
246 elif len(sources) == 1:
249 line_length=line_length,
251 write_back=write_back,
256 loop = asyncio.get_event_loop()
257 executor = ProcessPoolExecutor(max_workers=os.cpu_count())
259 loop.run_until_complete(
262 line_length=line_length,
264 write_back=write_back,
274 out("All done! ✨ 🍰 ✨")
275 click.echo(str(report))
276 ctx.exit(report.return_code)
283 write_back: WriteBack,
287 """Reformat a single file under `src` without spawning child processes.
289 If `quiet` is True, non-error messages are not output. `line_length`,
290 `write_back`, `fast` and `pyi` options are passed to
291 :func:`format_file_in_place` or :func:`format_stdin_to_stdout`.
295 if not src.is_file() and str(src) == "-":
296 if format_stdin_to_stdout(
297 line_length=line_length, fast=fast, write_back=write_back, mode=mode
299 changed = Changed.YES
302 if write_back != WriteBack.DIFF:
303 cache = read_cache(line_length, mode)
305 if src in cache and cache[src] == get_cache_info(src):
306 changed = Changed.CACHED
307 if changed is not Changed.CACHED and format_file_in_place(
309 line_length=line_length,
311 write_back=write_back,
314 changed = Changed.YES
315 if write_back == WriteBack.YES and changed is not Changed.NO:
316 write_cache(cache, [src], line_length, mode)
317 report.done(src, changed)
318 except Exception as exc:
319 report.failed(src, str(exc))
322 async def schedule_formatting(
326 write_back: WriteBack,
332 """Run formatting of `sources` in parallel using the provided `executor`.
334 (Use ProcessPoolExecutors for actual parallelism.)
336 `line_length`, `write_back`, `fast`, and `pyi` options are passed to
337 :func:`format_file_in_place`.
340 if write_back != WriteBack.DIFF:
341 cache = read_cache(line_length, mode)
342 sources, cached = filter_cached(cache, sources)
344 report.done(src, Changed.CACHED)
349 if write_back == WriteBack.DIFF:
350 # For diff output, we need locks to ensure we don't interleave output
351 # from different processes.
353 lock = manager.Lock()
355 loop.run_in_executor(
357 format_file_in_place,
365 for src in sorted(sources)
367 pending: Iterable[asyncio.Task] = tasks.keys()
369 loop.add_signal_handler(signal.SIGINT, cancel, pending)
370 loop.add_signal_handler(signal.SIGTERM, cancel, pending)
371 except NotImplementedError:
372 # There are no good alternatives for these on Windows
375 done, _ = await asyncio.wait(pending, return_when=asyncio.FIRST_COMPLETED)
377 src = tasks.pop(task)
379 cancelled.append(task)
380 elif task.exception():
381 report.failed(src, str(task.exception()))
383 formatted.append(src)
384 report.done(src, Changed.YES if task.result() else Changed.NO)
386 await asyncio.gather(*cancelled, loop=loop, return_exceptions=True)
387 if write_back == WriteBack.YES and formatted:
388 write_cache(cache, formatted, line_length, mode)
391 def format_file_in_place(
395 write_back: WriteBack = WriteBack.NO,
396 mode: FileMode = FileMode.AUTO_DETECT,
397 lock: Any = None, # multiprocessing.Manager().Lock() is some crazy proxy
399 """Format file under `src` path. Return True if changed.
401 If `write_back` is True, write reformatted code back to stdout.
402 `line_length` and `fast` options are passed to :func:`format_file_contents`.
404 if src.suffix == ".pyi":
406 with tokenize.open(src) as src_buffer:
407 src_contents = src_buffer.read()
409 dst_contents = format_file_contents(
410 src_contents, line_length=line_length, fast=fast, mode=mode
412 except NothingChanged:
415 if write_back == write_back.YES:
416 with open(src, "w", encoding=src_buffer.encoding) as f:
417 f.write(dst_contents)
418 elif write_back == write_back.DIFF:
419 src_name = f"{src} (original)"
420 dst_name = f"{src} (formatted)"
421 diff_contents = diff(src_contents, dst_contents, src_name, dst_name)
425 sys.stdout.write(diff_contents)
432 def format_stdin_to_stdout(
435 write_back: WriteBack = WriteBack.NO,
436 mode: FileMode = FileMode.AUTO_DETECT,
438 """Format file on stdin. Return True if changed.
440 If `write_back` is True, write reformatted code back to stdout.
441 `line_length`, `fast`, `is_pyi`, and `force_py36` arguments are passed to
442 :func:`format_file_contents`.
444 src = sys.stdin.read()
447 dst = format_file_contents(src, line_length=line_length, fast=fast, mode=mode)
450 except NothingChanged:
454 if write_back == WriteBack.YES:
455 sys.stdout.write(dst)
456 elif write_back == WriteBack.DIFF:
457 src_name = "<stdin> (original)"
458 dst_name = "<stdin> (formatted)"
459 sys.stdout.write(diff(src, dst, src_name, dst_name))
462 def format_file_contents(
467 mode: FileMode = FileMode.AUTO_DETECT,
469 """Reformat contents a file and return new contents.
471 If `fast` is False, additionally confirm that the reformatted code is
472 valid by calling :func:`assert_equivalent` and :func:`assert_stable` on it.
473 `line_length` is passed to :func:`format_str`.
475 if src_contents.strip() == "":
478 dst_contents = format_str(src_contents, line_length=line_length, mode=mode)
479 if src_contents == dst_contents:
483 assert_equivalent(src_contents, dst_contents)
484 assert_stable(src_contents, dst_contents, line_length=line_length, mode=mode)
489 src_contents: str, line_length: int, *, mode: FileMode = FileMode.AUTO_DETECT
491 """Reformat a string and return new contents.
493 `line_length` determines how many characters per line are allowed.
495 src_node = lib2to3_parse(src_contents)
497 future_imports = get_future_imports(src_node)
498 is_pyi = bool(mode & FileMode.PYI)
499 py36 = bool(mode & FileMode.PYTHON36) or is_python36(src_node)
500 normalize_strings = not bool(mode & FileMode.NO_STRING_NORMALIZATION)
501 lines = LineGenerator(
502 remove_u_prefix=py36 or "unicode_literals" in future_imports,
504 normalize_strings=normalize_strings,
506 elt = EmptyLineTracker(is_pyi=is_pyi)
509 for current_line in lines.visit(src_node):
510 for _ in range(after):
511 dst_contents += str(empty_line)
512 before, after = elt.maybe_empty_lines(current_line)
513 for _ in range(before):
514 dst_contents += str(empty_line)
515 for line in split_line(current_line, line_length=line_length, py36=py36):
516 dst_contents += str(line)
521 pygram.python_grammar_no_print_statement_no_exec_statement,
522 pygram.python_grammar_no_print_statement,
523 pygram.python_grammar,
527 def lib2to3_parse(src_txt: str) -> Node:
528 """Given a string with source, return the lib2to3 Node."""
529 grammar = pygram.python_grammar_no_print_statement
530 if src_txt[-1] != "\n":
531 nl = "\r\n" if "\r\n" in src_txt[:1024] else "\n"
533 for grammar in GRAMMARS:
534 drv = driver.Driver(grammar, pytree.convert)
536 result = drv.parse_string(src_txt, True)
539 except ParseError as pe:
540 lineno, column = pe.context[1]
541 lines = src_txt.splitlines()
543 faulty_line = lines[lineno - 1]
545 faulty_line = "<line number missing in source>"
546 exc = ValueError(f"Cannot parse: {lineno}:{column}: {faulty_line}")
550 if isinstance(result, Leaf):
551 result = Node(syms.file_input, [result])
555 def lib2to3_unparse(node: Node) -> str:
556 """Given a lib2to3 node, return its string representation."""
564 class Visitor(Generic[T]):
565 """Basic lib2to3 visitor that yields things of type `T` on `visit()`."""
567 def visit(self, node: LN) -> Iterator[T]:
568 """Main method to visit `node` and its children.
570 It tries to find a `visit_*()` method for the given `node.type`, like
571 `visit_simple_stmt` for Node objects or `visit_INDENT` for Leaf objects.
572 If no dedicated `visit_*()` method is found, chooses `visit_default()`
575 Then yields objects of type `T` from the selected visitor.
578 name = token.tok_name[node.type]
580 name = type_repr(node.type)
581 yield from getattr(self, f"visit_{name}", self.visit_default)(node)
583 def visit_default(self, node: LN) -> Iterator[T]:
584 """Default `visit_*()` implementation. Recurses to children of `node`."""
585 if isinstance(node, Node):
586 for child in node.children:
587 yield from self.visit(child)
591 class DebugVisitor(Visitor[T]):
594 def visit_default(self, node: LN) -> Iterator[T]:
595 indent = " " * (2 * self.tree_depth)
596 if isinstance(node, Node):
597 _type = type_repr(node.type)
598 out(f"{indent}{_type}", fg="yellow")
600 for child in node.children:
601 yield from self.visit(child)
604 out(f"{indent}/{_type}", fg="yellow", bold=False)
606 _type = token.tok_name.get(node.type, str(node.type))
607 out(f"{indent}{_type}", fg="blue", nl=False)
609 # We don't have to handle prefixes for `Node` objects since
610 # that delegates to the first child anyway.
611 out(f" {node.prefix!r}", fg="green", bold=False, nl=False)
612 out(f" {node.value!r}", fg="blue", bold=False)
615 def show(cls, code: str) -> None:
616 """Pretty-print the lib2to3 AST of a given string of `code`.
618 Convenience method for debugging.
620 v: DebugVisitor[None] = DebugVisitor()
621 list(v.visit(lib2to3_parse(code)))
624 KEYWORDS = set(keyword.kwlist)
625 WHITESPACE = {token.DEDENT, token.INDENT, token.NEWLINE}
626 FLOW_CONTROL = {"return", "raise", "break", "continue"}
637 STANDALONE_COMMENT = 153
638 LOGIC_OPERATORS = {"and", "or"}
663 STARS = {token.STAR, token.DOUBLESTAR}
666 syms.argument, # double star in arglist
667 syms.trailer, # single argument to call
669 syms.varargslist, # lambdas
671 UNPACKING_PARENTS = {
672 syms.atom, # single element of a list or set literal
710 COMPREHENSION_PRIORITY = 20
712 TERNARY_PRIORITY = 16
715 COMPARATOR_PRIORITY = 10
726 token.DOUBLESLASH: 4,
736 class BracketTracker:
737 """Keeps track of brackets on a line."""
740 bracket_match: Dict[Tuple[Depth, NodeType], Leaf] = Factory(dict)
741 delimiters: Dict[LeafID, Priority] = Factory(dict)
742 previous: Optional[Leaf] = None
743 _for_loop_variable: int = 0
744 _lambda_arguments: int = 0
746 def mark(self, leaf: Leaf) -> None:
747 """Mark `leaf` with bracket-related metadata. Keep track of delimiters.
749 All leaves receive an int `bracket_depth` field that stores how deep
750 within brackets a given leaf is. 0 means there are no enclosing brackets
751 that started on this line.
753 If a leaf is itself a closing bracket, it receives an `opening_bracket`
754 field that it forms a pair with. This is a one-directional link to
755 avoid reference cycles.
757 If a leaf is a delimiter (a token on which Black can split the line if
758 needed) and it's on depth 0, its `id()` is stored in the tracker's
761 if leaf.type == token.COMMENT:
764 self.maybe_decrement_after_for_loop_variable(leaf)
765 self.maybe_decrement_after_lambda_arguments(leaf)
766 if leaf.type in CLOSING_BRACKETS:
768 opening_bracket = self.bracket_match.pop((self.depth, leaf.type))
769 leaf.opening_bracket = opening_bracket
770 leaf.bracket_depth = self.depth
772 delim = is_split_before_delimiter(leaf, self.previous)
773 if delim and self.previous is not None:
774 self.delimiters[id(self.previous)] = delim
776 delim = is_split_after_delimiter(leaf, self.previous)
778 self.delimiters[id(leaf)] = delim
779 if leaf.type in OPENING_BRACKETS:
780 self.bracket_match[self.depth, BRACKET[leaf.type]] = leaf
783 self.maybe_increment_lambda_arguments(leaf)
784 self.maybe_increment_for_loop_variable(leaf)
786 def any_open_brackets(self) -> bool:
787 """Return True if there is an yet unmatched open bracket on the line."""
788 return bool(self.bracket_match)
790 def max_delimiter_priority(self, exclude: Iterable[LeafID] = ()) -> int:
791 """Return the highest priority of a delimiter found on the line.
793 Values are consistent with what `is_split_*_delimiter()` return.
794 Raises ValueError on no delimiters.
796 return max(v for k, v in self.delimiters.items() if k not in exclude)
798 def delimiter_count_with_priority(self, priority: int = 0) -> int:
799 """Return the number of delimiters with the given `priority`.
801 If no `priority` is passed, defaults to max priority on the line.
803 if not self.delimiters:
806 priority = priority or self.max_delimiter_priority()
807 return sum(1 for p in self.delimiters.values() if p == priority)
809 def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
810 """In a for loop, or comprehension, the variables are often unpacks.
812 To avoid splitting on the comma in this situation, increase the depth of
813 tokens between `for` and `in`.
815 if leaf.type == token.NAME and leaf.value == "for":
817 self._for_loop_variable += 1
822 def maybe_decrement_after_for_loop_variable(self, leaf: Leaf) -> bool:
823 """See `maybe_increment_for_loop_variable` above for explanation."""
824 if self._for_loop_variable and leaf.type == token.NAME and leaf.value == "in":
826 self._for_loop_variable -= 1
831 def maybe_increment_lambda_arguments(self, leaf: Leaf) -> bool:
832 """In a lambda expression, there might be more than one argument.
834 To avoid splitting on the comma in this situation, increase the depth of
835 tokens between `lambda` and `:`.
837 if leaf.type == token.NAME and leaf.value == "lambda":
839 self._lambda_arguments += 1
844 def maybe_decrement_after_lambda_arguments(self, leaf: Leaf) -> bool:
845 """See `maybe_increment_lambda_arguments` above for explanation."""
846 if self._lambda_arguments and leaf.type == token.COLON:
848 self._lambda_arguments -= 1
853 def get_open_lsqb(self) -> Optional[Leaf]:
854 """Return the most recent opening square bracket (if any)."""
855 return self.bracket_match.get((self.depth - 1, token.RSQB))
860 """Holds leaves and comments. Can be printed with `str(line)`."""
863 leaves: List[Leaf] = Factory(list)
864 comments: List[Tuple[Index, Leaf]] = Factory(list)
865 bracket_tracker: BracketTracker = Factory(BracketTracker)
866 inside_brackets: bool = False
867 should_explode: bool = False
869 def append(self, leaf: Leaf, preformatted: bool = False) -> None:
870 """Add a new `leaf` to the end of the line.
872 Unless `preformatted` is True, the `leaf` will receive a new consistent
873 whitespace prefix and metadata applied by :class:`BracketTracker`.
874 Trailing commas are maybe removed, unpacked for loop variables are
875 demoted from being delimiters.
877 Inline comments are put aside.
879 has_value = leaf.type in BRACKETS or bool(leaf.value.strip())
883 if token.COLON == leaf.type and self.is_class_paren_empty:
885 if self.leaves and not preformatted:
886 # Note: at this point leaf.prefix should be empty except for
887 # imports, for which we only preserve newlines.
888 leaf.prefix += whitespace(
889 leaf, complex_subscript=self.is_complex_subscript(leaf)
891 if self.inside_brackets or not preformatted:
892 self.bracket_tracker.mark(leaf)
893 self.maybe_remove_trailing_comma(leaf)
894 if not self.append_comment(leaf):
895 self.leaves.append(leaf)
897 def append_safe(self, leaf: Leaf, preformatted: bool = False) -> None:
898 """Like :func:`append()` but disallow invalid standalone comment structure.
900 Raises ValueError when any `leaf` is appended after a standalone comment
901 or when a standalone comment is not the first leaf on the line.
903 if self.bracket_tracker.depth == 0:
905 raise ValueError("cannot append to standalone comments")
907 if self.leaves and leaf.type == STANDALONE_COMMENT:
909 "cannot append standalone comments to a populated line"
912 self.append(leaf, preformatted=preformatted)
915 def is_comment(self) -> bool:
916 """Is this line a standalone comment?"""
917 return len(self.leaves) == 1 and self.leaves[0].type == STANDALONE_COMMENT
920 def is_decorator(self) -> bool:
921 """Is this line a decorator?"""
922 return bool(self) and self.leaves[0].type == token.AT
925 def is_import(self) -> bool:
926 """Is this an import line?"""
927 return bool(self) and is_import(self.leaves[0])
930 def is_class(self) -> bool:
931 """Is this line a class definition?"""
934 and self.leaves[0].type == token.NAME
935 and self.leaves[0].value == "class"
939 def is_stub_class(self) -> bool:
940 """Is this line a class definition with a body consisting only of "..."?"""
941 return self.is_class and self.leaves[-3:] == [
942 Leaf(token.DOT, ".") for _ in range(3)
946 def is_def(self) -> bool:
947 """Is this a function definition? (Also returns True for async defs.)"""
949 first_leaf = self.leaves[0]
954 second_leaf: Optional[Leaf] = self.leaves[1]
957 return (first_leaf.type == token.NAME and first_leaf.value == "def") or (
958 first_leaf.type == token.ASYNC
959 and second_leaf is not None
960 and second_leaf.type == token.NAME
961 and second_leaf.value == "def"
965 def is_class_paren_empty(self) -> bool:
966 """Is this a class with no base classes but using parentheses?
968 Those are unnecessary and should be removed.
972 and len(self.leaves) == 4
974 and self.leaves[2].type == token.LPAR
975 and self.leaves[2].value == "("
976 and self.leaves[3].type == token.RPAR
977 and self.leaves[3].value == ")"
981 def is_triple_quoted_string(self) -> bool:
982 """Is the line a triple quoted string?"""
985 and self.leaves[0].type == token.STRING
986 and self.leaves[0].value.startswith(('"""', "'''"))
989 def contains_standalone_comments(self, depth_limit: int = sys.maxsize) -> bool:
990 """If so, needs to be split before emitting."""
991 for leaf in self.leaves:
992 if leaf.type == STANDALONE_COMMENT:
993 if leaf.bracket_depth <= depth_limit:
998 def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
999 """Remove trailing comma if there is one and it's safe."""
1002 and self.leaves[-1].type == token.COMMA
1003 and closing.type in CLOSING_BRACKETS
1007 if closing.type == token.RBRACE:
1008 self.remove_trailing_comma()
1011 if closing.type == token.RSQB:
1012 comma = self.leaves[-1]
1013 if comma.parent and comma.parent.type == syms.listmaker:
1014 self.remove_trailing_comma()
1017 # For parens let's check if it's safe to remove the comma.
1018 # Imports are always safe.
1020 self.remove_trailing_comma()
1023 # Otheriwsse, if the trailing one is the only one, we might mistakenly
1024 # change a tuple into a different type by removing the comma.
1025 depth = closing.bracket_depth + 1
1027 opening = closing.opening_bracket
1028 for _opening_index, leaf in enumerate(self.leaves):
1035 for leaf in self.leaves[_opening_index + 1 :]:
1039 bracket_depth = leaf.bracket_depth
1040 if bracket_depth == depth and leaf.type == token.COMMA:
1042 if leaf.parent and leaf.parent.type == syms.arglist:
1047 self.remove_trailing_comma()
1052 def append_comment(self, comment: Leaf) -> bool:
1053 """Add an inline or standalone comment to the line."""
1055 comment.type == STANDALONE_COMMENT
1056 and self.bracket_tracker.any_open_brackets()
1061 if comment.type != token.COMMENT:
1064 after = len(self.leaves) - 1
1066 comment.type = STANDALONE_COMMENT
1071 self.comments.append((after, comment))
1074 def comments_after(self, leaf: Leaf, _index: int = -1) -> Iterator[Leaf]:
1075 """Generate comments that should appear directly after `leaf`.
1077 Provide a non-negative leaf `_index` to speed up the function.
1080 for _index, _leaf in enumerate(self.leaves):
1087 for index, comment_after in self.comments:
1091 def remove_trailing_comma(self) -> None:
1092 """Remove the trailing comma and moves the comments attached to it."""
1093 comma_index = len(self.leaves) - 1
1094 for i in range(len(self.comments)):
1095 comment_index, comment = self.comments[i]
1096 if comment_index == comma_index:
1097 self.comments[i] = (comma_index - 1, comment)
1100 def is_complex_subscript(self, leaf: Leaf) -> bool:
1101 """Return True iff `leaf` is part of a slice with non-trivial exprs."""
1103 leaf if leaf.type == token.LSQB else self.bracket_tracker.get_open_lsqb()
1105 if open_lsqb is None:
1108 subscript_start = open_lsqb.next_sibling
1110 isinstance(subscript_start, Node)
1111 and subscript_start.type == syms.subscriptlist
1113 subscript_start = child_towards(subscript_start, leaf)
1114 return subscript_start is not None and any(
1115 n.type in TEST_DESCENDANTS for n in subscript_start.pre_order()
1118 def __str__(self) -> str:
1119 """Render the line."""
1123 indent = " " * self.depth
1124 leaves = iter(self.leaves)
1125 first = next(leaves)
1126 res = f"{first.prefix}{indent}{first.value}"
1129 for _, comment in self.comments:
1133 def __bool__(self) -> bool:
1134 """Return True if the line has leaves or comments."""
1135 return bool(self.leaves or self.comments)
1138 class UnformattedLines(Line):
1139 """Just like :class:`Line` but stores lines which aren't reformatted."""
1141 def append(self, leaf: Leaf, preformatted: bool = True) -> None:
1142 """Just add a new `leaf` to the end of the lines.
1144 The `preformatted` argument is ignored.
1146 Keeps track of indentation `depth`, which is useful when the user
1147 says `# fmt: on`. Otherwise, doesn't do anything with the `leaf`.
1150 list(generate_comments(leaf))
1151 except FormatOn as f_on:
1152 self.leaves.append(f_on.leaf_from_consumed(leaf))
1155 self.leaves.append(leaf)
1156 if leaf.type == token.INDENT:
1158 elif leaf.type == token.DEDENT:
1161 def __str__(self) -> str:
1162 """Render unformatted lines from leaves which were added with `append()`.
1164 `depth` is not used for indentation in this case.
1170 for leaf in self.leaves:
1174 def append_comment(self, comment: Leaf) -> bool:
1175 """Not implemented in this class. Raises `NotImplementedError`."""
1176 raise NotImplementedError("Unformatted lines don't store comments separately.")
1178 def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
1179 """Does nothing and returns False."""
1182 def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
1183 """Does nothing and returns False."""
1188 class EmptyLineTracker:
1189 """Provides a stateful method that returns the number of potential extra
1190 empty lines needed before and after the currently processed line.
1192 Note: this tracker works on lines that haven't been split yet. It assumes
1193 the prefix of the first leaf consists of optional newlines. Those newlines
1194 are consumed by `maybe_empty_lines()` and included in the computation.
1197 is_pyi: bool = False
1198 previous_line: Optional[Line] = None
1199 previous_after: int = 0
1200 previous_defs: List[int] = Factory(list)
1202 def maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
1203 """Return the number of extra empty lines before and after the `current_line`.
1205 This is for separating `def`, `async def` and `class` with extra empty
1206 lines (two on module-level).
1208 if isinstance(current_line, UnformattedLines):
1211 before, after = self._maybe_empty_lines(current_line)
1212 before -= self.previous_after
1213 self.previous_after = after
1214 self.previous_line = current_line
1215 return before, after
1217 def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
1219 if current_line.depth == 0:
1220 max_allowed = 1 if self.is_pyi else 2
1221 if current_line.leaves:
1222 # Consume the first leaf's extra newlines.
1223 first_leaf = current_line.leaves[0]
1224 before = first_leaf.prefix.count("\n")
1225 before = min(before, max_allowed)
1226 first_leaf.prefix = ""
1229 depth = current_line.depth
1230 while self.previous_defs and self.previous_defs[-1] >= depth:
1231 self.previous_defs.pop()
1233 before = 0 if depth else 1
1235 before = 1 if depth else 2
1236 is_decorator = current_line.is_decorator
1237 if is_decorator or current_line.is_def or current_line.is_class:
1238 if not is_decorator:
1239 self.previous_defs.append(depth)
1240 if self.previous_line is None:
1241 # Don't insert empty lines before the first line in the file.
1244 if self.previous_line.is_decorator:
1247 if self.previous_line.depth < current_line.depth and (
1248 self.previous_line.is_class or self.previous_line.is_def
1253 self.previous_line.is_comment
1254 and self.previous_line.depth == current_line.depth
1260 if self.previous_line.depth > current_line.depth:
1262 elif current_line.is_class or self.previous_line.is_class:
1263 if current_line.is_stub_class and self.previous_line.is_stub_class:
1271 if current_line.depth and newlines:
1277 and self.previous_line.is_import
1278 and not current_line.is_import
1279 and depth == self.previous_line.depth
1281 return (before or 1), 0
1285 and self.previous_line.is_class
1286 and current_line.is_triple_quoted_string
1294 class LineGenerator(Visitor[Line]):
1295 """Generates reformatted Line objects. Empty lines are not emitted.
1297 Note: destroys the tree it's visiting by mutating prefixes of its leaves
1298 in ways that will no longer stringify to valid Python code on the tree.
1301 is_pyi: bool = False
1302 normalize_strings: bool = True
1303 current_line: Line = Factory(Line)
1304 remove_u_prefix: bool = False
1306 def line(self, indent: int = 0, type: Type[Line] = Line) -> Iterator[Line]:
1309 If the line is empty, only emit if it makes sense.
1310 If the line is too long, split it first and then generate.
1312 If any lines were generated, set up a new current_line.
1314 if not self.current_line:
1315 if self.current_line.__class__ == type:
1316 self.current_line.depth += indent
1318 self.current_line = type(depth=self.current_line.depth + indent)
1319 return # Line is empty, don't emit. Creating a new one unnecessary.
1321 complete_line = self.current_line
1322 self.current_line = type(depth=complete_line.depth + indent)
1325 def visit(self, node: LN) -> Iterator[Line]:
1326 """Main method to visit `node` and its children.
1328 Yields :class:`Line` objects.
1330 if isinstance(self.current_line, UnformattedLines):
1331 # File contained `# fmt: off`
1332 yield from self.visit_unformatted(node)
1335 yield from super().visit(node)
1337 def visit_default(self, node: LN) -> Iterator[Line]:
1338 """Default `visit_*()` implementation. Recurses to children of `node`."""
1339 if isinstance(node, Leaf):
1340 any_open_brackets = self.current_line.bracket_tracker.any_open_brackets()
1342 for comment in generate_comments(node):
1343 if any_open_brackets:
1344 # any comment within brackets is subject to splitting
1345 self.current_line.append(comment)
1346 elif comment.type == token.COMMENT:
1347 # regular trailing comment
1348 self.current_line.append(comment)
1349 yield from self.line()
1352 # regular standalone comment
1353 yield from self.line()
1355 self.current_line.append(comment)
1356 yield from self.line()
1358 except FormatOff as f_off:
1359 f_off.trim_prefix(node)
1360 yield from self.line(type=UnformattedLines)
1361 yield from self.visit(node)
1363 except FormatOn as f_on:
1364 # This only happens here if somebody says "fmt: on" multiple
1366 f_on.trim_prefix(node)
1367 yield from self.visit_default(node)
1370 normalize_prefix(node, inside_brackets=any_open_brackets)
1371 if self.normalize_strings and node.type == token.STRING:
1372 normalize_string_prefix(node, remove_u_prefix=self.remove_u_prefix)
1373 normalize_string_quotes(node)
1374 if node.type not in WHITESPACE:
1375 self.current_line.append(node)
1376 yield from super().visit_default(node)
1378 def visit_INDENT(self, node: Node) -> Iterator[Line]:
1379 """Increase indentation level, maybe yield a line."""
1380 # In blib2to3 INDENT never holds comments.
1381 yield from self.line(+1)
1382 yield from self.visit_default(node)
1384 def visit_DEDENT(self, node: Node) -> Iterator[Line]:
1385 """Decrease indentation level, maybe yield a line."""
1386 # The current line might still wait for trailing comments. At DEDENT time
1387 # there won't be any (they would be prefixes on the preceding NEWLINE).
1388 # Emit the line then.
1389 yield from self.line()
1391 # While DEDENT has no value, its prefix may contain standalone comments
1392 # that belong to the current indentation level. Get 'em.
1393 yield from self.visit_default(node)
1395 # Finally, emit the dedent.
1396 yield from self.line(-1)
1399 self, node: Node, keywords: Set[str], parens: Set[str]
1400 ) -> Iterator[Line]:
1401 """Visit a statement.
1403 This implementation is shared for `if`, `while`, `for`, `try`, `except`,
1404 `def`, `with`, `class`, `assert` and assignments.
1406 The relevant Python language `keywords` for a given statement will be
1407 NAME leaves within it. This methods puts those on a separate line.
1409 `parens` holds a set of string leaf values immediately after which
1410 invisible parens should be put.
1412 normalize_invisible_parens(node, parens_after=parens)
1413 for child in node.children:
1414 if child.type == token.NAME and child.value in keywords: # type: ignore
1415 yield from self.line()
1417 yield from self.visit(child)
1419 def visit_suite(self, node: Node) -> Iterator[Line]:
1420 """Visit a suite."""
1421 if self.is_pyi and is_stub_suite(node):
1422 yield from self.visit(node.children[2])
1424 yield from self.visit_default(node)
1426 def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
1427 """Visit a statement without nested statements."""
1428 is_suite_like = node.parent and node.parent.type in STATEMENT
1430 if self.is_pyi and is_stub_body(node):
1431 yield from self.visit_default(node)
1433 yield from self.line(+1)
1434 yield from self.visit_default(node)
1435 yield from self.line(-1)
1438 if not self.is_pyi or not node.parent or not is_stub_suite(node.parent):
1439 yield from self.line()
1440 yield from self.visit_default(node)
1442 def visit_async_stmt(self, node: Node) -> Iterator[Line]:
1443 """Visit `async def`, `async for`, `async with`."""
1444 yield from self.line()
1446 children = iter(node.children)
1447 for child in children:
1448 yield from self.visit(child)
1450 if child.type == token.ASYNC:
1453 internal_stmt = next(children)
1454 for child in internal_stmt.children:
1455 yield from self.visit(child)
1457 def visit_decorators(self, node: Node) -> Iterator[Line]:
1458 """Visit decorators."""
1459 for child in node.children:
1460 yield from self.line()
1461 yield from self.visit(child)
1463 def visit_SEMI(self, leaf: Leaf) -> Iterator[Line]:
1464 """Remove a semicolon and put the other statement on a separate line."""
1465 yield from self.line()
1467 def visit_ENDMARKER(self, leaf: Leaf) -> Iterator[Line]:
1468 """End of file. Process outstanding comments and end with a newline."""
1469 yield from self.visit_default(leaf)
1470 yield from self.line()
1472 def visit_unformatted(self, node: LN) -> Iterator[Line]:
1473 """Used when file contained a `# fmt: off`."""
1474 if isinstance(node, Node):
1475 for child in node.children:
1476 yield from self.visit(child)
1480 self.current_line.append(node)
1481 except FormatOn as f_on:
1482 f_on.trim_prefix(node)
1483 yield from self.line()
1484 yield from self.visit(node)
1486 if node.type == token.ENDMARKER:
1487 # somebody decided not to put a final `# fmt: on`
1488 yield from self.line()
1490 def __attrs_post_init__(self) -> None:
1491 """You are in a twisty little maze of passages."""
1494 self.visit_assert_stmt = partial(v, keywords={"assert"}, parens={"assert", ","})
1495 self.visit_if_stmt = partial(
1496 v, keywords={"if", "else", "elif"}, parens={"if", "elif"}
1498 self.visit_while_stmt = partial(v, keywords={"while", "else"}, parens={"while"})
1499 self.visit_for_stmt = partial(v, keywords={"for", "else"}, parens={"for", "in"})
1500 self.visit_try_stmt = partial(
1501 v, keywords={"try", "except", "else", "finally"}, parens=Ø
1503 self.visit_except_clause = partial(v, keywords={"except"}, parens=Ø)
1504 self.visit_with_stmt = partial(v, keywords={"with"}, parens=Ø)
1505 self.visit_funcdef = partial(v, keywords={"def"}, parens=Ø)
1506 self.visit_classdef = partial(v, keywords={"class"}, parens=Ø)
1507 self.visit_expr_stmt = partial(v, keywords=Ø, parens=ASSIGNMENTS)
1508 self.visit_return_stmt = partial(v, keywords={"return"}, parens={"return"})
1509 self.visit_import_from = partial(v, keywords=Ø, parens={"import"})
1510 self.visit_async_funcdef = self.visit_async_stmt
1511 self.visit_decorated = self.visit_decorators
1514 IMPLICIT_TUPLE = {syms.testlist, syms.testlist_star_expr, syms.exprlist}
1515 BRACKET = {token.LPAR: token.RPAR, token.LSQB: token.RSQB, token.LBRACE: token.RBRACE}
1516 OPENING_BRACKETS = set(BRACKET.keys())
1517 CLOSING_BRACKETS = set(BRACKET.values())
1518 BRACKETS = OPENING_BRACKETS | CLOSING_BRACKETS
1519 ALWAYS_NO_SPACE = CLOSING_BRACKETS | {token.COMMA, STANDALONE_COMMENT}
1522 def whitespace(leaf: Leaf, *, complex_subscript: bool) -> str: # noqa C901
1523 """Return whitespace prefix if needed for the given `leaf`.
1525 `complex_subscript` signals whether the given leaf is part of a subscription
1526 which has non-trivial arguments, like arithmetic expressions or function calls.
1534 if t in ALWAYS_NO_SPACE:
1537 if t == token.COMMENT:
1540 assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
1541 if t == token.COLON and p.type not in {
1548 prev = leaf.prev_sibling
1550 prevp = preceding_leaf(p)
1551 if not prevp or prevp.type in OPENING_BRACKETS:
1554 if t == token.COLON:
1555 if prevp.type == token.COLON:
1558 elif prevp.type != token.COMMA and not complex_subscript:
1563 if prevp.type == token.EQUAL:
1565 if prevp.parent.type in {
1573 elif prevp.parent.type == syms.typedargslist:
1574 # A bit hacky: if the equal sign has whitespace, it means we
1575 # previously found it's a typed argument. So, we're using
1579 elif prevp.type in STARS:
1580 if is_vararg(prevp, within=VARARGS_PARENTS | UNPACKING_PARENTS):
1583 elif prevp.type == token.COLON:
1584 if prevp.parent and prevp.parent.type in {syms.subscript, syms.sliceop}:
1585 return SPACE if complex_subscript else NO
1589 and prevp.parent.type == syms.factor
1590 and prevp.type in MATH_OPERATORS
1595 prevp.type == token.RIGHTSHIFT
1597 and prevp.parent.type == syms.shift_expr
1598 and prevp.prev_sibling
1599 and prevp.prev_sibling.type == token.NAME
1600 and prevp.prev_sibling.value == "print" # type: ignore
1602 # Python 2 print chevron
1605 elif prev.type in OPENING_BRACKETS:
1608 if p.type in {syms.parameters, syms.arglist}:
1609 # untyped function signatures or calls
1610 if not prev or prev.type != token.COMMA:
1613 elif p.type == syms.varargslist:
1615 if prev and prev.type != token.COMMA:
1618 elif p.type == syms.typedargslist:
1619 # typed function signatures
1623 if t == token.EQUAL:
1624 if prev.type != syms.tname:
1627 elif prev.type == token.EQUAL:
1628 # A bit hacky: if the equal sign has whitespace, it means we
1629 # previously found it's a typed argument. So, we're using that, too.
1632 elif prev.type != token.COMMA:
1635 elif p.type == syms.tname:
1638 prevp = preceding_leaf(p)
1639 if not prevp or prevp.type != token.COMMA:
1642 elif p.type == syms.trailer:
1643 # attributes and calls
1644 if t == token.LPAR or t == token.RPAR:
1649 prevp = preceding_leaf(p)
1650 if not prevp or prevp.type != token.NUMBER:
1653 elif t == token.LSQB:
1656 elif prev.type != token.COMMA:
1659 elif p.type == syms.argument:
1661 if t == token.EQUAL:
1665 prevp = preceding_leaf(p)
1666 if not prevp or prevp.type == token.LPAR:
1669 elif prev.type in {token.EQUAL} | STARS:
1672 elif p.type == syms.decorator:
1676 elif p.type == syms.dotted_name:
1680 prevp = preceding_leaf(p)
1681 if not prevp or prevp.type == token.AT or prevp.type == token.DOT:
1684 elif p.type == syms.classdef:
1688 if prev and prev.type == token.LPAR:
1691 elif p.type in {syms.subscript, syms.sliceop}:
1694 assert p.parent is not None, "subscripts are always parented"
1695 if p.parent.type == syms.subscriptlist:
1700 elif not complex_subscript:
1703 elif p.type == syms.atom:
1704 if prev and t == token.DOT:
1705 # dots, but not the first one.
1708 elif p.type == syms.dictsetmaker:
1710 if prev and prev.type == token.DOUBLESTAR:
1713 elif p.type in {syms.factor, syms.star_expr}:
1716 prevp = preceding_leaf(p)
1717 if not prevp or prevp.type in OPENING_BRACKETS:
1720 prevp_parent = prevp.parent
1721 assert prevp_parent is not None
1722 if prevp.type == token.COLON and prevp_parent.type in {
1728 elif prevp.type == token.EQUAL and prevp_parent.type == syms.argument:
1731 elif t == token.NAME or t == token.NUMBER:
1734 elif p.type == syms.import_from:
1736 if prev and prev.type == token.DOT:
1739 elif t == token.NAME:
1743 if prev and prev.type == token.DOT:
1746 elif p.type == syms.sliceop:
1752 def preceding_leaf(node: Optional[LN]) -> Optional[Leaf]:
1753 """Return the first leaf that precedes `node`, if any."""
1755 res = node.prev_sibling
1757 if isinstance(res, Leaf):
1761 return list(res.leaves())[-1]
1770 def child_towards(ancestor: Node, descendant: LN) -> Optional[LN]:
1771 """Return the child of `ancestor` that contains `descendant`."""
1772 node: Optional[LN] = descendant
1773 while node and node.parent != ancestor:
1778 def is_split_after_delimiter(leaf: Leaf, previous: Leaf = None) -> int:
1779 """Return the priority of the `leaf` delimiter, given a line break after it.
1781 The delimiter priorities returned here are from those delimiters that would
1782 cause a line break after themselves.
1784 Higher numbers are higher priority.
1786 if leaf.type == token.COMMA:
1787 return COMMA_PRIORITY
1792 def is_split_before_delimiter(leaf: Leaf, previous: Leaf = None) -> int:
1793 """Return the priority of the `leaf` delimiter, given a line before after it.
1795 The delimiter priorities returned here are from those delimiters that would
1796 cause a line break before themselves.
1798 Higher numbers are higher priority.
1800 if is_vararg(leaf, within=VARARGS_PARENTS | UNPACKING_PARENTS):
1801 # * and ** might also be MATH_OPERATORS but in this case they are not.
1802 # Don't treat them as a delimiter.
1806 leaf.type == token.DOT
1808 and leaf.parent.type not in {syms.import_from, syms.dotted_name}
1809 and (previous is None or previous.type in CLOSING_BRACKETS)
1814 leaf.type in MATH_OPERATORS
1816 and leaf.parent.type not in {syms.factor, syms.star_expr}
1818 return MATH_PRIORITIES[leaf.type]
1820 if leaf.type in COMPARATORS:
1821 return COMPARATOR_PRIORITY
1824 leaf.type == token.STRING
1825 and previous is not None
1826 and previous.type == token.STRING
1828 return STRING_PRIORITY
1830 if leaf.type != token.NAME:
1836 and leaf.parent.type in {syms.comp_for, syms.old_comp_for}
1838 return COMPREHENSION_PRIORITY
1843 and leaf.parent.type in {syms.comp_if, syms.old_comp_if}
1845 return COMPREHENSION_PRIORITY
1847 if leaf.value in {"if", "else"} and leaf.parent and leaf.parent.type == syms.test:
1848 return TERNARY_PRIORITY
1850 if leaf.value == "is":
1851 return COMPARATOR_PRIORITY
1856 and leaf.parent.type in {syms.comp_op, syms.comparison}
1858 previous is not None
1859 and previous.type == token.NAME
1860 and previous.value == "not"
1863 return COMPARATOR_PRIORITY
1868 and leaf.parent.type == syms.comp_op
1870 previous is not None
1871 and previous.type == token.NAME
1872 and previous.value == "is"
1875 return COMPARATOR_PRIORITY
1877 if leaf.value in LOGIC_OPERATORS and leaf.parent:
1878 return LOGIC_PRIORITY
1883 def generate_comments(leaf: LN) -> Iterator[Leaf]:
1884 """Clean the prefix of the `leaf` and generate comments from it, if any.
1886 Comments in lib2to3 are shoved into the whitespace prefix. This happens
1887 in `pgen2/driver.py:Driver.parse_tokens()`. This was a brilliant implementation
1888 move because it does away with modifying the grammar to include all the
1889 possible places in which comments can be placed.
1891 The sad consequence for us though is that comments don't "belong" anywhere.
1892 This is why this function generates simple parentless Leaf objects for
1893 comments. We simply don't know what the correct parent should be.
1895 No matter though, we can live without this. We really only need to
1896 differentiate between inline and standalone comments. The latter don't
1897 share the line with any code.
1899 Inline comments are emitted as regular token.COMMENT leaves. Standalone
1900 are emitted with a fake STANDALONE_COMMENT token identifier.
1911 for index, line in enumerate(p.split("\n")):
1912 consumed += len(line) + 1 # adding the length of the split '\n'
1913 line = line.lstrip()
1916 if not line.startswith("#"):
1919 if index == 0 and leaf.type != token.ENDMARKER:
1920 comment_type = token.COMMENT # simple trailing comment
1922 comment_type = STANDALONE_COMMENT
1923 comment = make_comment(line)
1924 yield Leaf(comment_type, comment, prefix="\n" * nlines)
1926 if comment in {"# fmt: on", "# yapf: enable"}:
1927 raise FormatOn(consumed)
1929 if comment in {"# fmt: off", "# yapf: disable"}:
1930 if comment_type == STANDALONE_COMMENT:
1931 raise FormatOff(consumed)
1933 prev = preceding_leaf(leaf)
1934 if not prev or prev.type in WHITESPACE: # standalone comment in disguise
1935 raise FormatOff(consumed)
1940 def make_comment(content: str) -> str:
1941 """Return a consistently formatted comment from the given `content` string.
1943 All comments (except for "##", "#!", "#:") should have a single space between
1944 the hash sign and the content.
1946 If `content` didn't start with a hash sign, one is provided.
1948 content = content.rstrip()
1952 if content[0] == "#":
1953 content = content[1:]
1954 if content and content[0] not in " !:#":
1955 content = " " + content
1956 return "#" + content
1960 line: Line, line_length: int, inner: bool = False, py36: bool = False
1961 ) -> Iterator[Line]:
1962 """Split a `line` into potentially many lines.
1964 They should fit in the allotted `line_length` but might not be able to.
1965 `inner` signifies that there were a pair of brackets somewhere around the
1966 current `line`, possibly transitively. This means we can fallback to splitting
1967 by delimiters if the LHS/RHS don't yield any results.
1969 If `py36` is True, splitting may generate syntax that is only compatible
1970 with Python 3.6 and later.
1972 if isinstance(line, UnformattedLines) or line.is_comment:
1976 line_str = str(line).strip("\n")
1977 if not line.should_explode and is_line_short_enough(
1978 line, line_length=line_length, line_str=line_str
1983 split_funcs: List[SplitFunc]
1985 split_funcs = [left_hand_split]
1988 def rhs(line: Line, py36: bool = False) -> Iterator[Line]:
1989 for omit in generate_trailers_to_omit(line, line_length):
1990 lines = list(right_hand_split(line, line_length, py36, omit=omit))
1991 if is_line_short_enough(lines[0], line_length=line_length):
1995 # All splits failed, best effort split with no omits.
1996 # This mostly happens to multiline strings that are by definition
1997 # reported as not fitting a single line.
1998 yield from right_hand_split(line, py36)
2000 if line.inside_brackets:
2001 split_funcs = [delimiter_split, standalone_comment_split, rhs]
2004 for split_func in split_funcs:
2005 # We are accumulating lines in `result` because we might want to abort
2006 # mission and return the original line in the end, or attempt a different
2008 result: List[Line] = []
2010 for l in split_func(line, py36):
2011 if str(l).strip("\n") == line_str:
2012 raise CannotSplit("Split function returned an unchanged result")
2015 split_line(l, line_length=line_length, inner=True, py36=py36)
2017 except CannotSplit as cs:
2028 def left_hand_split(line: Line, py36: bool = False) -> Iterator[Line]:
2029 """Split line into many lines, starting with the first matching bracket pair.
2031 Note: this usually looks weird, only use this for function definitions.
2032 Prefer RHS otherwise. This is why this function is not symmetrical with
2033 :func:`right_hand_split` which also handles optional parentheses.
2035 head = Line(depth=line.depth)
2036 body = Line(depth=line.depth + 1, inside_brackets=True)
2037 tail = Line(depth=line.depth)
2038 tail_leaves: List[Leaf] = []
2039 body_leaves: List[Leaf] = []
2040 head_leaves: List[Leaf] = []
2041 current_leaves = head_leaves
2042 matching_bracket = None
2043 for leaf in line.leaves:
2045 current_leaves is body_leaves
2046 and leaf.type in CLOSING_BRACKETS
2047 and leaf.opening_bracket is matching_bracket
2049 current_leaves = tail_leaves if body_leaves else head_leaves
2050 current_leaves.append(leaf)
2051 if current_leaves is head_leaves:
2052 if leaf.type in OPENING_BRACKETS:
2053 matching_bracket = leaf
2054 current_leaves = body_leaves
2055 # Since body is a new indent level, remove spurious leading whitespace.
2057 normalize_prefix(body_leaves[0], inside_brackets=True)
2058 # Build the new lines.
2059 for result, leaves in (head, head_leaves), (body, body_leaves), (tail, tail_leaves):
2061 result.append(leaf, preformatted=True)
2062 for comment_after in line.comments_after(leaf):
2063 result.append(comment_after, preformatted=True)
2064 bracket_split_succeeded_or_raise(head, body, tail)
2065 for result in (head, body, tail):
2070 def right_hand_split(
2071 line: Line, line_length: int, py36: bool = False, omit: Collection[LeafID] = ()
2072 ) -> Iterator[Line]:
2073 """Split line into many lines, starting with the last matching bracket pair.
2075 If the split was by optional parentheses, attempt splitting without them, too.
2076 `omit` is a collection of closing bracket IDs that shouldn't be considered for
2079 Note: running this function modifies `bracket_depth` on the leaves of `line`.
2081 head = Line(depth=line.depth)
2082 body = Line(depth=line.depth + 1, inside_brackets=True)
2083 tail = Line(depth=line.depth)
2084 tail_leaves: List[Leaf] = []
2085 body_leaves: List[Leaf] = []
2086 head_leaves: List[Leaf] = []
2087 current_leaves = tail_leaves
2088 opening_bracket = None
2089 closing_bracket = None
2090 for leaf in reversed(line.leaves):
2091 if current_leaves is body_leaves:
2092 if leaf is opening_bracket:
2093 current_leaves = head_leaves if body_leaves else tail_leaves
2094 current_leaves.append(leaf)
2095 if current_leaves is tail_leaves:
2096 if leaf.type in CLOSING_BRACKETS and id(leaf) not in omit:
2097 opening_bracket = leaf.opening_bracket
2098 closing_bracket = leaf
2099 current_leaves = body_leaves
2100 tail_leaves.reverse()
2101 body_leaves.reverse()
2102 head_leaves.reverse()
2103 # Since body is a new indent level, remove spurious leading whitespace.
2105 normalize_prefix(body_leaves[0], inside_brackets=True)
2107 # No `head` means the split failed. Either `tail` has all content or
2108 # the matching `opening_bracket` wasn't available on `line` anymore.
2109 raise CannotSplit("No brackets found")
2111 # Build the new lines.
2112 for result, leaves in (head, head_leaves), (body, body_leaves), (tail, tail_leaves):
2114 result.append(leaf, preformatted=True)
2115 for comment_after in line.comments_after(leaf):
2116 result.append(comment_after, preformatted=True)
2117 bracket_split_succeeded_or_raise(head, body, tail)
2118 assert opening_bracket and closing_bracket
2120 # the opening bracket is an optional paren
2121 opening_bracket.type == token.LPAR
2122 and not opening_bracket.value
2123 # the closing bracket is an optional paren
2124 and closing_bracket.type == token.RPAR
2125 and not closing_bracket.value
2126 # there are no standalone comments in the body
2127 and not line.contains_standalone_comments(0)
2128 # and it's not an import (optional parens are the only thing we can split
2129 # on in this case; attempting a split without them is a waste of time)
2130 and not line.is_import
2132 omit = {id(closing_bracket), *omit}
2133 if can_omit_invisible_parens(body, line_length):
2135 yield from right_hand_split(line, line_length, py36=py36, omit=omit)
2140 ensure_visible(opening_bracket)
2141 ensure_visible(closing_bracket)
2142 body.should_explode = should_explode(body, opening_bracket)
2143 for result in (head, body, tail):
2148 def bracket_split_succeeded_or_raise(head: Line, body: Line, tail: Line) -> None:
2149 """Raise :exc:`CannotSplit` if the last left- or right-hand split failed.
2151 Do nothing otherwise.
2153 A left- or right-hand split is based on a pair of brackets. Content before
2154 (and including) the opening bracket is left on one line, content inside the
2155 brackets is put on a separate line, and finally content starting with and
2156 following the closing bracket is put on a separate line.
2158 Those are called `head`, `body`, and `tail`, respectively. If the split
2159 produced the same line (all content in `head`) or ended up with an empty `body`
2160 and the `tail` is just the closing bracket, then it's considered failed.
2162 tail_len = len(str(tail).strip())
2165 raise CannotSplit("Splitting brackets produced the same line")
2169 f"Splitting brackets on an empty body to save "
2170 f"{tail_len} characters is not worth it"
2174 def dont_increase_indentation(split_func: SplitFunc) -> SplitFunc:
2175 """Normalize prefix of the first leaf in every line returned by `split_func`.
2177 This is a decorator over relevant split functions.
2181 def split_wrapper(line: Line, py36: bool = False) -> Iterator[Line]:
2182 for l in split_func(line, py36):
2183 normalize_prefix(l.leaves[0], inside_brackets=True)
2186 return split_wrapper
2189 @dont_increase_indentation
2190 def delimiter_split(line: Line, py36: bool = False) -> Iterator[Line]:
2191 """Split according to delimiters of the highest priority.
2193 If `py36` is True, the split will add trailing commas also in function
2194 signatures that contain `*` and `**`.
2197 last_leaf = line.leaves[-1]
2199 raise CannotSplit("Line empty")
2201 bt = line.bracket_tracker
2203 delimiter_priority = bt.max_delimiter_priority(exclude={id(last_leaf)})
2205 raise CannotSplit("No delimiters found")
2207 if delimiter_priority == DOT_PRIORITY:
2208 if bt.delimiter_count_with_priority(delimiter_priority) == 1:
2209 raise CannotSplit("Splitting a single attribute from its owner looks wrong")
2211 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
2212 lowest_depth = sys.maxsize
2213 trailing_comma_safe = True
2215 def append_to_line(leaf: Leaf) -> Iterator[Line]:
2216 """Append `leaf` to current line or to new line if appending impossible."""
2217 nonlocal current_line
2219 current_line.append_safe(leaf, preformatted=True)
2220 except ValueError as ve:
2223 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
2224 current_line.append(leaf)
2226 for index, leaf in enumerate(line.leaves):
2227 yield from append_to_line(leaf)
2229 for comment_after in line.comments_after(leaf, index):
2230 yield from append_to_line(comment_after)
2232 lowest_depth = min(lowest_depth, leaf.bracket_depth)
2233 if leaf.bracket_depth == lowest_depth and is_vararg(
2234 leaf, within=VARARGS_PARENTS
2236 trailing_comma_safe = trailing_comma_safe and py36
2237 leaf_priority = bt.delimiters.get(id(leaf))
2238 if leaf_priority == delimiter_priority:
2241 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
2245 and delimiter_priority == COMMA_PRIORITY
2246 and current_line.leaves[-1].type != token.COMMA
2247 and current_line.leaves[-1].type != STANDALONE_COMMENT
2249 current_line.append(Leaf(token.COMMA, ","))
2253 @dont_increase_indentation
2254 def standalone_comment_split(line: Line, py36: bool = False) -> Iterator[Line]:
2255 """Split standalone comments from the rest of the line."""
2256 if not line.contains_standalone_comments(0):
2257 raise CannotSplit("Line does not have any standalone comments")
2259 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
2261 def append_to_line(leaf: Leaf) -> Iterator[Line]:
2262 """Append `leaf` to current line or to new line if appending impossible."""
2263 nonlocal current_line
2265 current_line.append_safe(leaf, preformatted=True)
2266 except ValueError as ve:
2269 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
2270 current_line.append(leaf)
2272 for index, leaf in enumerate(line.leaves):
2273 yield from append_to_line(leaf)
2275 for comment_after in line.comments_after(leaf, index):
2276 yield from append_to_line(comment_after)
2282 def is_import(leaf: Leaf) -> bool:
2283 """Return True if the given leaf starts an import statement."""
2290 (v == "import" and p and p.type == syms.import_name)
2291 or (v == "from" and p and p.type == syms.import_from)
2296 def normalize_prefix(leaf: Leaf, *, inside_brackets: bool) -> None:
2297 """Leave existing extra newlines if not `inside_brackets`. Remove everything
2300 Note: don't use backslashes for formatting or you'll lose your voting rights.
2302 if not inside_brackets:
2303 spl = leaf.prefix.split("#")
2304 if "\\" not in spl[0]:
2305 nl_count = spl[-1].count("\n")
2308 leaf.prefix = "\n" * nl_count
2314 def normalize_string_prefix(leaf: Leaf, remove_u_prefix: bool = False) -> None:
2315 """Make all string prefixes lowercase.
2317 If remove_u_prefix is given, also removes any u prefix from the string.
2319 Note: Mutates its argument.
2321 match = re.match(r"^([furbFURB]*)(.*)$", leaf.value, re.DOTALL)
2322 assert match is not None, f"failed to match string {leaf.value!r}"
2323 orig_prefix = match.group(1)
2324 new_prefix = orig_prefix.lower()
2326 new_prefix = new_prefix.replace("u", "")
2327 leaf.value = f"{new_prefix}{match.group(2)}"
2330 def normalize_string_quotes(leaf: Leaf) -> None:
2331 """Prefer double quotes but only if it doesn't cause more escaping.
2333 Adds or removes backslashes as appropriate. Doesn't parse and fix
2334 strings nested in f-strings (yet).
2336 Note: Mutates its argument.
2338 value = leaf.value.lstrip("furbFURB")
2339 if value[:3] == '"""':
2342 elif value[:3] == "'''":
2345 elif value[0] == '"':
2351 first_quote_pos = leaf.value.find(orig_quote)
2352 if first_quote_pos == -1:
2353 return # There's an internal error
2355 prefix = leaf.value[:first_quote_pos]
2356 unescaped_new_quote = re.compile(rf"(([^\\]|^)(\\\\)*){new_quote}")
2357 escaped_new_quote = re.compile(rf"([^\\]|^)\\(\\\\)*{new_quote}")
2358 escaped_orig_quote = re.compile(rf"([^\\]|^)\\(\\\\)*{orig_quote}")
2359 body = leaf.value[first_quote_pos + len(orig_quote) : -len(orig_quote)]
2360 if "r" in prefix.casefold():
2361 if unescaped_new_quote.search(body):
2362 # There's at least one unescaped new_quote in this raw string
2363 # so converting is impossible
2366 # Do not introduce or remove backslashes in raw strings
2369 # remove unnecessary quotes
2370 new_body = sub_twice(escaped_new_quote, rf"\1\2{new_quote}", body)
2371 if body != new_body:
2372 # Consider the string without unnecessary quotes as the original
2374 leaf.value = f"{prefix}{orig_quote}{body}{orig_quote}"
2375 new_body = sub_twice(escaped_orig_quote, rf"\1\2{orig_quote}", new_body)
2376 new_body = sub_twice(unescaped_new_quote, rf"\1\\{new_quote}", new_body)
2377 if new_quote == '"""' and new_body[-1] == '"':
2379 new_body = new_body[:-1] + '\\"'
2380 orig_escape_count = body.count("\\")
2381 new_escape_count = new_body.count("\\")
2382 if new_escape_count > orig_escape_count:
2383 return # Do not introduce more escaping
2385 if new_escape_count == orig_escape_count and orig_quote == '"':
2386 return # Prefer double quotes
2388 leaf.value = f"{prefix}{new_quote}{new_body}{new_quote}"
2391 def normalize_invisible_parens(node: Node, parens_after: Set[str]) -> None:
2392 """Make existing optional parentheses invisible or create new ones.
2394 `parens_after` is a set of string leaf values immeditely after which parens
2397 Standardizes on visible parentheses for single-element tuples, and keeps
2398 existing visible parentheses for other tuples and generator expressions.
2401 list(generate_comments(node))
2403 return # This `node` has a prefix with `# fmt: off`, don't mess with parens.
2406 for index, child in enumerate(list(node.children)):
2408 if child.type == syms.atom:
2409 maybe_make_parens_invisible_in_atom(child)
2410 elif is_one_tuple(child):
2411 # wrap child in visible parentheses
2412 lpar = Leaf(token.LPAR, "(")
2413 rpar = Leaf(token.RPAR, ")")
2415 node.insert_child(index, Node(syms.atom, [lpar, child, rpar]))
2416 elif node.type == syms.import_from:
2417 # "import from" nodes store parentheses directly as part of
2419 if child.type == token.LPAR:
2420 # make parentheses invisible
2421 child.value = "" # type: ignore
2422 node.children[-1].value = "" # type: ignore
2423 elif child.type != token.STAR:
2424 # insert invisible parentheses
2425 node.insert_child(index, Leaf(token.LPAR, ""))
2426 node.append_child(Leaf(token.RPAR, ""))
2429 elif not (isinstance(child, Leaf) and is_multiline_string(child)):
2430 # wrap child in invisible parentheses
2431 lpar = Leaf(token.LPAR, "")
2432 rpar = Leaf(token.RPAR, "")
2433 index = child.remove() or 0
2434 node.insert_child(index, Node(syms.atom, [lpar, child, rpar]))
2436 check_lpar = isinstance(child, Leaf) and child.value in parens_after
2439 def maybe_make_parens_invisible_in_atom(node: LN) -> bool:
2440 """If it's safe, make the parens in the atom `node` invisible, recusively."""
2442 node.type != syms.atom
2443 or is_empty_tuple(node)
2444 or is_one_tuple(node)
2446 or max_delimiter_priority_in_atom(node) >= COMMA_PRIORITY
2450 first = node.children[0]
2451 last = node.children[-1]
2452 if first.type == token.LPAR and last.type == token.RPAR:
2453 # make parentheses invisible
2454 first.value = "" # type: ignore
2455 last.value = "" # type: ignore
2456 if len(node.children) > 1:
2457 maybe_make_parens_invisible_in_atom(node.children[1])
2463 def is_empty_tuple(node: LN) -> bool:
2464 """Return True if `node` holds an empty tuple."""
2466 node.type == syms.atom
2467 and len(node.children) == 2
2468 and node.children[0].type == token.LPAR
2469 and node.children[1].type == token.RPAR
2473 def is_one_tuple(node: LN) -> bool:
2474 """Return True if `node` holds a tuple with one element, with or without parens."""
2475 if node.type == syms.atom:
2476 if len(node.children) != 3:
2479 lpar, gexp, rpar = node.children
2481 lpar.type == token.LPAR
2482 and gexp.type == syms.testlist_gexp
2483 and rpar.type == token.RPAR
2487 return len(gexp.children) == 2 and gexp.children[1].type == token.COMMA
2490 node.type in IMPLICIT_TUPLE
2491 and len(node.children) == 2
2492 and node.children[1].type == token.COMMA
2496 def is_yield(node: LN) -> bool:
2497 """Return True if `node` holds a `yield` or `yield from` expression."""
2498 if node.type == syms.yield_expr:
2501 if node.type == token.NAME and node.value == "yield": # type: ignore
2504 if node.type != syms.atom:
2507 if len(node.children) != 3:
2510 lpar, expr, rpar = node.children
2511 if lpar.type == token.LPAR and rpar.type == token.RPAR:
2512 return is_yield(expr)
2517 def is_vararg(leaf: Leaf, within: Set[NodeType]) -> bool:
2518 """Return True if `leaf` is a star or double star in a vararg or kwarg.
2520 If `within` includes VARARGS_PARENTS, this applies to function signatures.
2521 If `within` includes UNPACKING_PARENTS, it applies to right hand-side
2522 extended iterable unpacking (PEP 3132) and additional unpacking
2523 generalizations (PEP 448).
2525 if leaf.type not in STARS or not leaf.parent:
2529 if p.type == syms.star_expr:
2530 # Star expressions are also used as assignment targets in extended
2531 # iterable unpacking (PEP 3132). See what its parent is instead.
2537 return p.type in within
2540 def is_multiline_string(leaf: Leaf) -> bool:
2541 """Return True if `leaf` is a multiline string that actually spans many lines."""
2542 value = leaf.value.lstrip("furbFURB")
2543 return value[:3] in {'"""', "'''"} and "\n" in value
2546 def is_stub_suite(node: Node) -> bool:
2547 """Return True if `node` is a suite with a stub body."""
2549 len(node.children) != 4
2550 or node.children[0].type != token.NEWLINE
2551 or node.children[1].type != token.INDENT
2552 or node.children[3].type != token.DEDENT
2556 return is_stub_body(node.children[2])
2559 def is_stub_body(node: LN) -> bool:
2560 """Return True if `node` is a simple statement containing an ellipsis."""
2561 if not isinstance(node, Node) or node.type != syms.simple_stmt:
2564 if len(node.children) != 2:
2567 child = node.children[0]
2569 child.type == syms.atom
2570 and len(child.children) == 3
2571 and all(leaf == Leaf(token.DOT, ".") for leaf in child.children)
2575 def max_delimiter_priority_in_atom(node: LN) -> int:
2576 """Return maximum delimiter priority inside `node`.
2578 This is specific to atoms with contents contained in a pair of parentheses.
2579 If `node` isn't an atom or there are no enclosing parentheses, returns 0.
2581 if node.type != syms.atom:
2584 first = node.children[0]
2585 last = node.children[-1]
2586 if not (first.type == token.LPAR and last.type == token.RPAR):
2589 bt = BracketTracker()
2590 for c in node.children[1:-1]:
2591 if isinstance(c, Leaf):
2594 for leaf in c.leaves():
2597 return bt.max_delimiter_priority()
2603 def ensure_visible(leaf: Leaf) -> None:
2604 """Make sure parentheses are visible.
2606 They could be invisible as part of some statements (see
2607 :func:`normalize_invible_parens` and :func:`visit_import_from`).
2609 if leaf.type == token.LPAR:
2611 elif leaf.type == token.RPAR:
2615 def should_explode(line: Line, opening_bracket: Leaf) -> bool:
2616 """Should `line` immediately be split with `delimiter_split()` after RHS?"""
2618 opening_bracket.parent
2619 and opening_bracket.parent.type in {syms.atom, syms.import_from}
2620 and opening_bracket.value in "[{("
2625 last_leaf = line.leaves[-1]
2626 exclude = {id(last_leaf)} if last_leaf.type == token.COMMA else set()
2627 max_priority = line.bracket_tracker.max_delimiter_priority(exclude=exclude)
2628 except (IndexError, ValueError):
2631 return max_priority == COMMA_PRIORITY
2634 def is_python36(node: Node) -> bool:
2635 """Return True if the current file is using Python 3.6+ features.
2637 Currently looking for:
2639 - trailing commas after * or ** in function signatures and calls.
2641 for n in node.pre_order():
2642 if n.type == token.STRING:
2643 value_head = n.value[:2] # type: ignore
2644 if value_head in {'f"', 'F"', "f'", "F'", "rf", "fr", "RF", "FR"}:
2648 n.type in {syms.typedargslist, syms.arglist}
2650 and n.children[-1].type == token.COMMA
2652 for ch in n.children:
2653 if ch.type in STARS:
2656 if ch.type == syms.argument:
2657 for argch in ch.children:
2658 if argch.type in STARS:
2664 def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[LeafID]]:
2665 """Generate sets of closing bracket IDs that should be omitted in a RHS.
2667 Brackets can be omitted if the entire trailer up to and including
2668 a preceding closing bracket fits in one line.
2670 Yielded sets are cumulative (contain results of previous yields, too). First
2674 omit: Set[LeafID] = set()
2677 length = 4 * line.depth
2678 opening_bracket = None
2679 closing_bracket = None
2680 optional_brackets: Set[LeafID] = set()
2681 inner_brackets: Set[LeafID] = set()
2682 for index, leaf, leaf_length in enumerate_with_length(line, reversed=True):
2683 length += leaf_length
2684 if length > line_length:
2687 has_inline_comment = leaf_length > len(leaf.value) + len(leaf.prefix)
2688 if leaf.type == STANDALONE_COMMENT or has_inline_comment:
2691 optional_brackets.discard(id(leaf))
2693 if leaf is opening_bracket:
2694 opening_bracket = None
2695 elif leaf.type in CLOSING_BRACKETS:
2696 inner_brackets.add(id(leaf))
2697 elif leaf.type in CLOSING_BRACKETS:
2699 optional_brackets.add(id(opening_bracket))
2702 if index > 0 and line.leaves[index - 1].type in OPENING_BRACKETS:
2703 # Empty brackets would fail a split so treat them as "inner"
2704 # brackets (e.g. only add them to the `omit` set if another
2705 # pair of brackets was good enough.
2706 inner_brackets.add(id(leaf))
2709 opening_bracket = leaf.opening_bracket
2711 omit.add(id(closing_bracket))
2712 omit.update(inner_brackets)
2713 inner_brackets.clear()
2715 closing_bracket = leaf
2718 def get_future_imports(node: Node) -> Set[str]:
2719 """Return a set of __future__ imports in the file."""
2721 for child in node.children:
2722 if child.type != syms.simple_stmt:
2724 first_child = child.children[0]
2725 if isinstance(first_child, Leaf):
2726 # Continue looking if we see a docstring; otherwise stop.
2728 len(child.children) == 2
2729 and first_child.type == token.STRING
2730 and child.children[1].type == token.NEWLINE
2735 elif first_child.type == syms.import_from:
2736 module_name = first_child.children[1]
2737 if not isinstance(module_name, Leaf) or module_name.value != "__future__":
2739 for import_from_child in first_child.children[3:]:
2740 if isinstance(import_from_child, Leaf):
2741 if import_from_child.type == token.NAME:
2742 imports.add(import_from_child.value)
2744 assert import_from_child.type == syms.import_as_names
2745 for leaf in import_from_child.children:
2746 if isinstance(leaf, Leaf) and leaf.type == token.NAME:
2747 imports.add(leaf.value)
2753 PYTHON_EXTENSIONS = {".py", ".pyi"}
2754 BLACKLISTED_DIRECTORIES = {
2767 def gen_python_files_in_dir(path: Path) -> Iterator[Path]:
2768 """Generate all files under `path` which aren't under BLACKLISTED_DIRECTORIES
2769 and have one of the PYTHON_EXTENSIONS.
2771 for child in path.iterdir():
2773 if child.name in BLACKLISTED_DIRECTORIES:
2776 yield from gen_python_files_in_dir(child)
2778 elif child.is_file() and child.suffix in PYTHON_EXTENSIONS:
2784 """Provides a reformatting counter. Can be rendered with `str(report)`."""
2788 change_count: int = 0
2790 failure_count: int = 0
2792 def done(self, src: Path, changed: Changed) -> None:
2793 """Increment the counter for successful reformatting. Write out a message."""
2794 if changed is Changed.YES:
2795 reformatted = "would reformat" if self.check else "reformatted"
2797 out(f"{reformatted} {src}")
2798 self.change_count += 1
2801 if changed is Changed.NO:
2802 msg = f"{src} already well formatted, good job."
2804 msg = f"{src} wasn't modified on disk since last run."
2805 out(msg, bold=False)
2806 self.same_count += 1
2808 def failed(self, src: Path, message: str) -> None:
2809 """Increment the counter for failed reformatting. Write out a message."""
2810 err(f"error: cannot format {src}: {message}")
2811 self.failure_count += 1
2814 def return_code(self) -> int:
2815 """Return the exit code that the app should use.
2817 This considers the current state of changed files and failures:
2818 - if there were any failures, return 123;
2819 - if any files were changed and --check is being used, return 1;
2820 - otherwise return 0.
2822 # According to http://tldp.org/LDP/abs/html/exitcodes.html starting with
2823 # 126 we have special returncodes reserved by the shell.
2824 if self.failure_count:
2827 elif self.change_count and self.check:
2832 def __str__(self) -> str:
2833 """Render a color report of the current state.
2835 Use `click.unstyle` to remove colors.
2838 reformatted = "would be reformatted"
2839 unchanged = "would be left unchanged"
2840 failed = "would fail to reformat"
2842 reformatted = "reformatted"
2843 unchanged = "left unchanged"
2844 failed = "failed to reformat"
2846 if self.change_count:
2847 s = "s" if self.change_count > 1 else ""
2849 click.style(f"{self.change_count} file{s} {reformatted}", bold=True)
2852 s = "s" if self.same_count > 1 else ""
2853 report.append(f"{self.same_count} file{s} {unchanged}")
2854 if self.failure_count:
2855 s = "s" if self.failure_count > 1 else ""
2857 click.style(f"{self.failure_count} file{s} {failed}", fg="red")
2859 return ", ".join(report) + "."
2862 def assert_equivalent(src: str, dst: str) -> None:
2863 """Raise AssertionError if `src` and `dst` aren't equivalent."""
2868 def _v(node: ast.AST, depth: int = 0) -> Iterator[str]:
2869 """Simple visitor generating strings to compare ASTs by content."""
2870 yield f"{' ' * depth}{node.__class__.__name__}("
2872 for field in sorted(node._fields):
2874 value = getattr(node, field)
2875 except AttributeError:
2878 yield f"{' ' * (depth+1)}{field}="
2880 if isinstance(value, list):
2882 if isinstance(item, ast.AST):
2883 yield from _v(item, depth + 2)
2885 elif isinstance(value, ast.AST):
2886 yield from _v(value, depth + 2)
2889 yield f"{' ' * (depth+2)}{value!r}, # {value.__class__.__name__}"
2891 yield f"{' ' * depth}) # /{node.__class__.__name__}"
2894 src_ast = ast.parse(src)
2895 except Exception as exc:
2896 major, minor = sys.version_info[:2]
2897 raise AssertionError(
2898 f"cannot use --safe with this file; failed to parse source file "
2899 f"with Python {major}.{minor}'s builtin AST. Re-run with --fast "
2900 f"or stop using deprecated Python 2 syntax. AST error message: {exc}"
2904 dst_ast = ast.parse(dst)
2905 except Exception as exc:
2906 log = dump_to_file("".join(traceback.format_tb(exc.__traceback__)), dst)
2907 raise AssertionError(
2908 f"INTERNAL ERROR: Black produced invalid code: {exc}. "
2909 f"Please report a bug on https://github.com/ambv/black/issues. "
2910 f"This invalid output might be helpful: {log}"
2913 src_ast_str = "\n".join(_v(src_ast))
2914 dst_ast_str = "\n".join(_v(dst_ast))
2915 if src_ast_str != dst_ast_str:
2916 log = dump_to_file(diff(src_ast_str, dst_ast_str, "src", "dst"))
2917 raise AssertionError(
2918 f"INTERNAL ERROR: Black produced code that is not equivalent to "
2920 f"Please report a bug on https://github.com/ambv/black/issues. "
2921 f"This diff might be helpful: {log}"
2926 src: str, dst: str, line_length: int, mode: FileMode = FileMode.AUTO_DETECT
2928 """Raise AssertionError if `dst` reformats differently the second time."""
2929 newdst = format_str(dst, line_length=line_length, mode=mode)
2932 diff(src, dst, "source", "first pass"),
2933 diff(dst, newdst, "first pass", "second pass"),
2935 raise AssertionError(
2936 f"INTERNAL ERROR: Black produced different code on the second pass "
2937 f"of the formatter. "
2938 f"Please report a bug on https://github.com/ambv/black/issues. "
2939 f"This diff might be helpful: {log}"
2943 def dump_to_file(*output: str) -> str:
2944 """Dump `output` to a temporary file. Return path to the file."""
2947 with tempfile.NamedTemporaryFile(
2948 mode="w", prefix="blk_", suffix=".log", delete=False, encoding="utf8"
2950 for lines in output:
2952 if lines and lines[-1] != "\n":
2957 def diff(a: str, b: str, a_name: str, b_name: str) -> str:
2958 """Return a unified diff string between strings `a` and `b`."""
2961 a_lines = [line + "\n" for line in a.split("\n")]
2962 b_lines = [line + "\n" for line in b.split("\n")]
2964 difflib.unified_diff(a_lines, b_lines, fromfile=a_name, tofile=b_name, n=5)
2968 def cancel(tasks: Iterable[asyncio.Task]) -> None:
2969 """asyncio signal handler that cancels all `tasks` and reports to stderr."""
2975 def shutdown(loop: BaseEventLoop) -> None:
2976 """Cancel all pending tasks on `loop`, wait for them, and close the loop."""
2978 # This part is borrowed from asyncio/runners.py in Python 3.7b2.
2979 to_cancel = [task for task in asyncio.Task.all_tasks(loop) if not task.done()]
2983 for task in to_cancel:
2985 loop.run_until_complete(
2986 asyncio.gather(*to_cancel, loop=loop, return_exceptions=True)
2989 # `concurrent.futures.Future` objects cannot be cancelled once they
2990 # are already running. There might be some when the `shutdown()` happened.
2991 # Silence their logger's spew about the event loop being closed.
2992 cf_logger = logging.getLogger("concurrent.futures")
2993 cf_logger.setLevel(logging.CRITICAL)
2997 def sub_twice(regex: Pattern[str], replacement: str, original: str) -> str:
2998 """Replace `regex` with `replacement` twice on `original`.
3000 This is used by string normalization to perform replaces on
3001 overlapping matches.
3003 return regex.sub(replacement, regex.sub(replacement, original))
3006 def enumerate_reversed(sequence: Sequence[T]) -> Iterator[Tuple[Index, T]]:
3007 """Like `reversed(enumerate(sequence))` if that were possible."""
3008 index = len(sequence) - 1
3009 for element in reversed(sequence):
3010 yield (index, element)
3014 def enumerate_with_length(
3015 line: Line, reversed: bool = False
3016 ) -> Iterator[Tuple[Index, Leaf, int]]:
3017 """Return an enumeration of leaves with their length.
3019 Stops prematurely on multiline strings and standalone comments.
3022 Callable[[Sequence[Leaf]], Iterator[Tuple[Index, Leaf]]],
3023 enumerate_reversed if reversed else enumerate,
3025 for index, leaf in op(line.leaves):
3026 length = len(leaf.prefix) + len(leaf.value)
3027 if "\n" in leaf.value:
3028 return # Multiline strings, we can't continue.
3030 comment: Optional[Leaf]
3031 for comment in line.comments_after(leaf, index):
3032 length += len(comment.value)
3034 yield index, leaf, length
3037 def is_line_short_enough(line: Line, *, line_length: int, line_str: str = "") -> bool:
3038 """Return True if `line` is no longer than `line_length`.
3040 Uses the provided `line_str` rendering, if any, otherwise computes a new one.
3043 line_str = str(line).strip("\n")
3045 len(line_str) <= line_length
3046 and "\n" not in line_str # multiline strings
3047 and not line.contains_standalone_comments()
3051 def can_omit_invisible_parens(line: Line, line_length: int) -> bool:
3052 """Does `line` have a shape safe to reformat without optional parens around it?
3054 Returns True for only a subset of potentially nice looking formattings but
3055 the point is to not return false positives that end up producing lines that
3058 bt = line.bracket_tracker
3059 if not bt.delimiters:
3060 # Without delimiters the optional parentheses are useless.
3063 max_priority = bt.max_delimiter_priority()
3064 if bt.delimiter_count_with_priority(max_priority) > 1:
3065 # With more than one delimiter of a kind the optional parentheses read better.
3068 if max_priority == DOT_PRIORITY:
3069 # A single stranded method call doesn't require optional parentheses.
3072 assert len(line.leaves) >= 2, "Stranded delimiter"
3074 first = line.leaves[0]
3075 second = line.leaves[1]
3076 penultimate = line.leaves[-2]
3077 last = line.leaves[-1]
3079 # With a single delimiter, omit if the expression starts or ends with
3081 if first.type in OPENING_BRACKETS and second.type not in CLOSING_BRACKETS:
3083 length = 4 * line.depth
3084 for _index, leaf, leaf_length in enumerate_with_length(line):
3085 if leaf.type in CLOSING_BRACKETS and leaf.opening_bracket is first:
3088 length += leaf_length
3089 if length > line_length:
3092 if leaf.type in OPENING_BRACKETS:
3093 # There are brackets we can further split on.
3097 # checked the entire string and line length wasn't exceeded
3098 if len(line.leaves) == _index + 1:
3101 # Note: we are not returning False here because a line might have *both*
3102 # a leading opening bracket and a trailing closing bracket. If the
3103 # opening bracket doesn't match our rule, maybe the closing will.
3106 last.type == token.RPAR
3107 or last.type == token.RBRACE
3109 # don't use indexing for omitting optional parentheses;
3111 last.type == token.RSQB
3113 and last.parent.type != syms.trailer
3116 if penultimate.type in OPENING_BRACKETS:
3117 # Empty brackets don't help.
3120 if is_multiline_string(first):
3121 # Additional wrapping of a multiline string in this situation is
3125 length = 4 * line.depth
3126 seen_other_brackets = False
3127 for _index, leaf, leaf_length in enumerate_with_length(line):
3128 length += leaf_length
3129 if leaf is last.opening_bracket:
3130 if seen_other_brackets or length <= line_length:
3133 elif leaf.type in OPENING_BRACKETS:
3134 # There are brackets we can further split on.
3135 seen_other_brackets = True
3140 def get_cache_file(line_length: int, mode: FileMode) -> Path:
3141 pyi = bool(mode & FileMode.PYI)
3142 py36 = bool(mode & FileMode.PYTHON36)
3145 / f"cache.{line_length}{'.pyi' if pyi else ''}{'.py36' if py36 else ''}.pickle"
3149 def read_cache(line_length: int, mode: FileMode) -> Cache:
3150 """Read the cache if it exists and is well formed.
3152 If it is not well formed, the call to write_cache later should resolve the issue.
3154 cache_file = get_cache_file(line_length, mode)
3155 if not cache_file.exists():
3158 with cache_file.open("rb") as fobj:
3160 cache: Cache = pickle.load(fobj)
3161 except pickle.UnpicklingError:
3167 def get_cache_info(path: Path) -> CacheInfo:
3168 """Return the information used to check if a file is already formatted or not."""
3170 return stat.st_mtime, stat.st_size
3174 cache: Cache, sources: Iterable[Path]
3175 ) -> Tuple[List[Path], List[Path]]:
3176 """Split a list of paths into two.
3178 The first list contains paths of files that modified on disk or are not in the
3179 cache. The other list contains paths to non-modified files.
3184 if cache.get(src) != get_cache_info(src):
3192 cache: Cache, sources: List[Path], line_length: int, mode: FileMode
3194 """Update the cache file."""
3195 cache_file = get_cache_file(line_length, mode)
3197 if not CACHE_DIR.exists():
3198 CACHE_DIR.mkdir(parents=True)
3199 new_cache = {**cache, **{src.resolve(): get_cache_info(src) for src in sources}}
3200 with cache_file.open("wb") as fobj:
3201 pickle.dump(new_cache, fobj, protocol=pickle.HIGHEST_PROTOCOL)
3206 if __name__ == "__main__":