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.
2 from asyncio.base_events import BaseEventLoop
3 from concurrent.futures import Executor, ProcessPoolExecutor
4 from datetime import datetime
5 from enum import Enum, Flag
6 from functools import lru_cache, partial, wraps
10 from multiprocessing import Manager
12 from pathlib import Path
38 from appdirs import user_cache_dir
39 from attr import dataclass, Factory
44 from blib2to3.pytree import Node, Leaf, type_repr
45 from blib2to3 import pygram, pytree
46 from blib2to3.pgen2 import driver, token
47 from blib2to3.pgen2.parse import ParseError
50 __version__ = "18.6b4"
51 DEFAULT_LINE_LENGTH = 88
53 r"/(\.git|\.hg|\.mypy_cache|\.tox|\.venv|_build|buck-out|build|dist)/"
55 DEFAULT_INCLUDES = r"\.pyi?$"
56 CACHE_DIR = Path(user_cache_dir("black", version=__version__))
68 LN = Union[Leaf, Node]
69 SplitFunc = Callable[["Line", bool], Iterator["Line"]]
72 CacheInfo = Tuple[Timestamp, FileSize]
73 Cache = Dict[Path, CacheInfo]
74 out = partial(click.secho, bold=True, err=True)
75 err = partial(click.secho, fg="red", err=True)
77 pygram.initialize(CACHE_DIR)
78 syms = pygram.python_symbols
81 class NothingChanged(UserWarning):
82 """Raised by :func:`format_file` when reformatted code is the same as source."""
85 class CannotSplit(Exception):
86 """A readable split that fits the allotted line length is impossible.
88 Raised by :func:`left_hand_split`, :func:`right_hand_split`, and
89 :func:`delimiter_split`.
93 class WriteBack(Enum):
100 def from_configuration(cls, *, check: bool, diff: bool) -> "WriteBack":
101 if check and not diff:
104 return cls.DIFF if diff else cls.YES
113 class FileMode(Flag):
117 NO_STRING_NORMALIZATION = 4
120 def from_configuration(
121 cls, *, py36: bool, pyi: bool, skip_string_normalization: bool
123 mode = cls.AUTO_DETECT
128 if skip_string_normalization:
129 mode |= cls.NO_STRING_NORMALIZATION
133 def read_pyproject_toml(
134 ctx: click.Context, param: click.Parameter, value: Union[str, int, bool, None]
136 """Inject Black configuration from "pyproject.toml" into defaults in `ctx`.
138 Returns the path to a successfully found and read configuration file, None
141 assert not isinstance(value, (int, bool)), "Invalid parameter type passed"
143 root = find_project_root(ctx.params.get("src", ()))
144 path = root / "pyproject.toml"
151 pyproject_toml = toml.load(value)
152 config = pyproject_toml.get("tool", {}).get("black", {})
153 except (toml.TomlDecodeError, OSError) as e:
154 raise click.BadOptionUsage(f"Error reading configuration file: {e}", ctx)
159 if ctx.default_map is None:
161 ctx.default_map.update( # type: ignore # bad types in .pyi
162 {k.replace("--", "").replace("-", "_"): v for k, v in config.items()}
167 @click.command(context_settings=dict(help_option_names=["-h", "--help"]))
172 default=DEFAULT_LINE_LENGTH,
173 help="How many character per line to allow.",
180 "Allow using Python 3.6-only syntax on all input files. This will put "
181 "trailing commas in function signatures and calls also after *args and "
182 "**kwargs. [default: per-file auto-detection]"
189 "Format all input files like typing stubs regardless of file extension "
190 "(useful when piping source on standard input)."
195 "--skip-string-normalization",
197 help="Don't normalize string quotes or prefixes.",
203 "Don't write the files back, just return the status. Return code 0 "
204 "means nothing would change. Return code 1 means some files would be "
205 "reformatted. Return code 123 means there was an internal error."
211 help="Don't write the files back, just output a diff for each file on stdout.",
216 help="If --fast given, skip temporary sanity checks. [default: --safe]",
221 default=DEFAULT_INCLUDES,
223 "A regular expression that matches files and directories that should be "
224 "included on recursive searches. An empty value means all files are "
225 "included regardless of the name. Use forward slashes for directories on "
226 "all platforms (Windows, too). Exclusions are calculated first, inclusions "
234 default=DEFAULT_EXCLUDES,
236 "A regular expression that matches files and directories that should be "
237 "excluded on recursive searches. An empty value means no paths are excluded. "
238 "Use forward slashes for directories on all platforms (Windows, too). "
239 "Exclusions are calculated first, inclusions later."
248 "Don't emit non-error messages to stderr. Errors are still emitted, "
249 "silence those with 2>/dev/null."
257 "Also emit messages to stderr about files that were not changed or were "
258 "ignored due to --exclude=."
261 @click.version_option(version=__version__)
266 exists=True, file_okay=True, dir_okay=True, readable=True, allow_dash=True
273 exists=False, file_okay=True, dir_okay=False, readable=True, allow_dash=False
276 callback=read_pyproject_toml,
277 help="Read configuration from PATH.",
288 skip_string_normalization: bool,
294 config: Optional[str],
296 """The uncompromising code formatter."""
297 write_back = WriteBack.from_configuration(check=check, diff=diff)
298 mode = FileMode.from_configuration(
299 py36=py36, pyi=pyi, skip_string_normalization=skip_string_normalization
301 if config and verbose:
302 out(f"Using configuration from {config}.", bold=False, fg="blue")
304 include_regex = re_compile_maybe_verbose(include)
306 err(f"Invalid regular expression for include given: {include!r}")
309 exclude_regex = re_compile_maybe_verbose(exclude)
311 err(f"Invalid regular expression for exclude given: {exclude!r}")
313 report = Report(check=check, quiet=quiet, verbose=verbose)
314 root = find_project_root(src)
315 sources: Set[Path] = set()
320 gen_python_files_in_dir(p, root, include_regex, exclude_regex, report)
322 elif p.is_file() or s == "-":
323 # if a file was explicitly given, we don't care about its extension
326 err(f"invalid path: {s}")
327 if len(sources) == 0:
328 if verbose or not quiet:
329 out("No paths given. Nothing to do 😴")
332 if len(sources) == 1:
335 line_length=line_length,
337 write_back=write_back,
342 loop = asyncio.get_event_loop()
343 executor = ProcessPoolExecutor(max_workers=os.cpu_count())
345 loop.run_until_complete(
348 line_length=line_length,
350 write_back=write_back,
359 if verbose or not quiet:
360 bang = "💥 💔 💥" if report.return_code else "✨ 🍰 ✨"
361 out(f"All done! {bang}")
362 click.secho(str(report), err=True)
363 ctx.exit(report.return_code)
370 write_back: WriteBack,
374 """Reformat a single file under `src` without spawning child processes.
376 If `quiet` is True, non-error messages are not output. `line_length`,
377 `write_back`, `fast` and `pyi` options are passed to
378 :func:`format_file_in_place` or :func:`format_stdin_to_stdout`.
382 if not src.is_file() and str(src) == "-":
383 if format_stdin_to_stdout(
384 line_length=line_length, fast=fast, write_back=write_back, mode=mode
386 changed = Changed.YES
389 if write_back != WriteBack.DIFF:
390 cache = read_cache(line_length, mode)
391 res_src = src.resolve()
392 if res_src in cache and cache[res_src] == get_cache_info(res_src):
393 changed = Changed.CACHED
394 if changed is not Changed.CACHED and format_file_in_place(
396 line_length=line_length,
398 write_back=write_back,
401 changed = Changed.YES
402 if (write_back is WriteBack.YES and changed is not Changed.CACHED) or (
403 write_back is WriteBack.CHECK and changed is Changed.NO
405 write_cache(cache, [src], line_length, mode)
406 report.done(src, changed)
407 except Exception as exc:
408 report.failed(src, str(exc))
411 async def schedule_formatting(
415 write_back: WriteBack,
421 """Run formatting of `sources` in parallel using the provided `executor`.
423 (Use ProcessPoolExecutors for actual parallelism.)
425 `line_length`, `write_back`, `fast`, and `pyi` options are passed to
426 :func:`format_file_in_place`.
429 if write_back != WriteBack.DIFF:
430 cache = read_cache(line_length, mode)
431 sources, cached = filter_cached(cache, sources)
432 for src in sorted(cached):
433 report.done(src, Changed.CACHED)
435 sources_to_cache = []
438 if write_back == WriteBack.DIFF:
439 # For diff output, we need locks to ensure we don't interleave output
440 # from different processes.
442 lock = manager.Lock()
444 loop.run_in_executor(
446 format_file_in_place,
454 for src in sorted(sources)
456 pending: Iterable[asyncio.Task] = tasks.keys()
458 loop.add_signal_handler(signal.SIGINT, cancel, pending)
459 loop.add_signal_handler(signal.SIGTERM, cancel, pending)
460 except NotImplementedError:
461 # There are no good alternatives for these on Windows
464 done, _ = await asyncio.wait(pending, return_when=asyncio.FIRST_COMPLETED)
466 src = tasks.pop(task)
468 cancelled.append(task)
469 elif task.exception():
470 report.failed(src, str(task.exception()))
472 changed = Changed.YES if task.result() else Changed.NO
473 # If the file was written back or was successfully checked as
474 # well-formatted, store this information in the cache.
475 if write_back is WriteBack.YES or (
476 write_back is WriteBack.CHECK and changed is Changed.NO
478 sources_to_cache.append(src)
479 report.done(src, changed)
481 await asyncio.gather(*cancelled, loop=loop, return_exceptions=True)
483 write_cache(cache, sources_to_cache, line_length, mode)
486 def format_file_in_place(
490 write_back: WriteBack = WriteBack.NO,
491 mode: FileMode = FileMode.AUTO_DETECT,
492 lock: Any = None, # multiprocessing.Manager().Lock() is some crazy proxy
494 """Format file under `src` path. Return True if changed.
496 If `write_back` is DIFF, write a diff to stdout. If it is YES, write reformatted
498 `line_length` and `fast` options are passed to :func:`format_file_contents`.
500 if src.suffix == ".pyi":
503 then = datetime.utcfromtimestamp(src.stat().st_mtime)
504 with open(src, "rb") as buf:
505 src_contents, encoding, newline = decode_bytes(buf.read())
507 dst_contents = format_file_contents(
508 src_contents, line_length=line_length, fast=fast, mode=mode
510 except NothingChanged:
513 if write_back == write_back.YES:
514 with open(src, "w", encoding=encoding, newline=newline) as f:
515 f.write(dst_contents)
516 elif write_back == write_back.DIFF:
517 now = datetime.utcnow()
518 src_name = f"{src}\t{then} +0000"
519 dst_name = f"{src}\t{now} +0000"
520 diff_contents = diff(src_contents, dst_contents, src_name, dst_name)
524 f = io.TextIOWrapper(
530 f.write(diff_contents)
538 def format_stdin_to_stdout(
541 write_back: WriteBack = WriteBack.NO,
542 mode: FileMode = FileMode.AUTO_DETECT,
544 """Format file on stdin. Return True if changed.
546 If `write_back` is YES, write reformatted code back to stdout. If it is DIFF,
547 write a diff to stdout.
548 `line_length`, `fast`, `is_pyi`, and `force_py36` arguments are passed to
549 :func:`format_file_contents`.
551 then = datetime.utcnow()
552 src, encoding, newline = decode_bytes(sys.stdin.buffer.read())
555 dst = format_file_contents(src, line_length=line_length, fast=fast, mode=mode)
558 except NothingChanged:
562 f = io.TextIOWrapper(
563 sys.stdout.buffer, encoding=encoding, newline=newline, write_through=True
565 if write_back == WriteBack.YES:
567 elif write_back == WriteBack.DIFF:
568 now = datetime.utcnow()
569 src_name = f"STDIN\t{then} +0000"
570 dst_name = f"STDOUT\t{now} +0000"
571 f.write(diff(src, dst, src_name, dst_name))
575 def format_file_contents(
580 mode: FileMode = FileMode.AUTO_DETECT,
582 """Reformat contents a file and return new contents.
584 If `fast` is False, additionally confirm that the reformatted code is
585 valid by calling :func:`assert_equivalent` and :func:`assert_stable` on it.
586 `line_length` is passed to :func:`format_str`.
588 if src_contents.strip() == "":
591 dst_contents = format_str(src_contents, line_length=line_length, mode=mode)
592 if src_contents == dst_contents:
596 assert_equivalent(src_contents, dst_contents)
597 assert_stable(src_contents, dst_contents, line_length=line_length, mode=mode)
602 src_contents: str, line_length: int, *, mode: FileMode = FileMode.AUTO_DETECT
604 """Reformat a string and return new contents.
606 `line_length` determines how many characters per line are allowed.
608 src_node = lib2to3_parse(src_contents)
610 future_imports = get_future_imports(src_node)
611 is_pyi = bool(mode & FileMode.PYI)
612 py36 = bool(mode & FileMode.PYTHON36) or is_python36(src_node)
613 normalize_strings = not bool(mode & FileMode.NO_STRING_NORMALIZATION)
614 normalize_fmt_off(src_node)
615 lines = LineGenerator(
616 remove_u_prefix=py36 or "unicode_literals" in future_imports,
618 normalize_strings=normalize_strings,
619 allow_underscores=py36,
621 elt = EmptyLineTracker(is_pyi=is_pyi)
624 for current_line in lines.visit(src_node):
625 for _ in range(after):
626 dst_contents += str(empty_line)
627 before, after = elt.maybe_empty_lines(current_line)
628 for _ in range(before):
629 dst_contents += str(empty_line)
630 for line in split_line(current_line, line_length=line_length, py36=py36):
631 dst_contents += str(line)
635 def decode_bytes(src: bytes) -> Tuple[FileContent, Encoding, NewLine]:
636 """Return a tuple of (decoded_contents, encoding, newline).
638 `newline` is either CRLF or LF but `decoded_contents` is decoded with
639 universal newlines (i.e. only contains LF).
641 srcbuf = io.BytesIO(src)
642 encoding, lines = tokenize.detect_encoding(srcbuf.readline)
644 return "", encoding, "\n"
646 newline = "\r\n" if b"\r\n" == lines[0][-2:] else "\n"
648 with io.TextIOWrapper(srcbuf, encoding) as tiow:
649 return tiow.read(), encoding, newline
653 pygram.python_grammar_no_print_statement_no_exec_statement,
654 pygram.python_grammar_no_print_statement,
655 pygram.python_grammar,
659 def lib2to3_parse(src_txt: str) -> Node:
660 """Given a string with source, return the lib2to3 Node."""
661 grammar = pygram.python_grammar_no_print_statement
662 if src_txt[-1:] != "\n":
664 for grammar in GRAMMARS:
665 drv = driver.Driver(grammar, pytree.convert)
667 result = drv.parse_string(src_txt, True)
670 except ParseError as pe:
671 lineno, column = pe.context[1]
672 lines = src_txt.splitlines()
674 faulty_line = lines[lineno - 1]
676 faulty_line = "<line number missing in source>"
677 exc = ValueError(f"Cannot parse: {lineno}:{column}: {faulty_line}")
681 if isinstance(result, Leaf):
682 result = Node(syms.file_input, [result])
686 def lib2to3_unparse(node: Node) -> str:
687 """Given a lib2to3 node, return its string representation."""
695 class Visitor(Generic[T]):
696 """Basic lib2to3 visitor that yields things of type `T` on `visit()`."""
698 def visit(self, node: LN) -> Iterator[T]:
699 """Main method to visit `node` and its children.
701 It tries to find a `visit_*()` method for the given `node.type`, like
702 `visit_simple_stmt` for Node objects or `visit_INDENT` for Leaf objects.
703 If no dedicated `visit_*()` method is found, chooses `visit_default()`
706 Then yields objects of type `T` from the selected visitor.
709 name = token.tok_name[node.type]
711 name = type_repr(node.type)
712 yield from getattr(self, f"visit_{name}", self.visit_default)(node)
714 def visit_default(self, node: LN) -> Iterator[T]:
715 """Default `visit_*()` implementation. Recurses to children of `node`."""
716 if isinstance(node, Node):
717 for child in node.children:
718 yield from self.visit(child)
722 class DebugVisitor(Visitor[T]):
725 def visit_default(self, node: LN) -> Iterator[T]:
726 indent = " " * (2 * self.tree_depth)
727 if isinstance(node, Node):
728 _type = type_repr(node.type)
729 out(f"{indent}{_type}", fg="yellow")
731 for child in node.children:
732 yield from self.visit(child)
735 out(f"{indent}/{_type}", fg="yellow", bold=False)
737 _type = token.tok_name.get(node.type, str(node.type))
738 out(f"{indent}{_type}", fg="blue", nl=False)
740 # We don't have to handle prefixes for `Node` objects since
741 # that delegates to the first child anyway.
742 out(f" {node.prefix!r}", fg="green", bold=False, nl=False)
743 out(f" {node.value!r}", fg="blue", bold=False)
746 def show(cls, code: Union[str, Leaf, Node]) -> None:
747 """Pretty-print the lib2to3 AST of a given string of `code`.
749 Convenience method for debugging.
751 v: DebugVisitor[None] = DebugVisitor()
752 if isinstance(code, str):
753 code = lib2to3_parse(code)
757 KEYWORDS = set(keyword.kwlist)
758 WHITESPACE = {token.DEDENT, token.INDENT, token.NEWLINE}
759 FLOW_CONTROL = {"return", "raise", "break", "continue"}
770 STANDALONE_COMMENT = 153
771 token.tok_name[STANDALONE_COMMENT] = "STANDALONE_COMMENT"
772 LOGIC_OPERATORS = {"and", "or"}
797 STARS = {token.STAR, token.DOUBLESTAR}
800 syms.argument, # double star in arglist
801 syms.trailer, # single argument to call
803 syms.varargslist, # lambdas
805 UNPACKING_PARENTS = {
806 syms.atom, # single element of a list or set literal
810 syms.testlist_star_expr,
845 COMPREHENSION_PRIORITY = 20
847 TERNARY_PRIORITY = 16
850 COMPARATOR_PRIORITY = 10
861 token.DOUBLESLASH: 4,
871 class BracketTracker:
872 """Keeps track of brackets on a line."""
875 bracket_match: Dict[Tuple[Depth, NodeType], Leaf] = Factory(dict)
876 delimiters: Dict[LeafID, Priority] = Factory(dict)
877 previous: Optional[Leaf] = None
878 _for_loop_variable: int = 0
879 _lambda_arguments: int = 0
881 def mark(self, leaf: Leaf) -> None:
882 """Mark `leaf` with bracket-related metadata. Keep track of delimiters.
884 All leaves receive an int `bracket_depth` field that stores how deep
885 within brackets a given leaf is. 0 means there are no enclosing brackets
886 that started on this line.
888 If a leaf is itself a closing bracket, it receives an `opening_bracket`
889 field that it forms a pair with. This is a one-directional link to
890 avoid reference cycles.
892 If a leaf is a delimiter (a token on which Black can split the line if
893 needed) and it's on depth 0, its `id()` is stored in the tracker's
896 if leaf.type == token.COMMENT:
899 self.maybe_decrement_after_for_loop_variable(leaf)
900 self.maybe_decrement_after_lambda_arguments(leaf)
901 if leaf.type in CLOSING_BRACKETS:
903 opening_bracket = self.bracket_match.pop((self.depth, leaf.type))
904 leaf.opening_bracket = opening_bracket
905 leaf.bracket_depth = self.depth
907 delim = is_split_before_delimiter(leaf, self.previous)
908 if delim and self.previous is not None:
909 self.delimiters[id(self.previous)] = delim
911 delim = is_split_after_delimiter(leaf, self.previous)
913 self.delimiters[id(leaf)] = delim
914 if leaf.type in OPENING_BRACKETS:
915 self.bracket_match[self.depth, BRACKET[leaf.type]] = leaf
918 self.maybe_increment_lambda_arguments(leaf)
919 self.maybe_increment_for_loop_variable(leaf)
921 def any_open_brackets(self) -> bool:
922 """Return True if there is an yet unmatched open bracket on the line."""
923 return bool(self.bracket_match)
925 def max_delimiter_priority(self, exclude: Iterable[LeafID] = ()) -> int:
926 """Return the highest priority of a delimiter found on the line.
928 Values are consistent with what `is_split_*_delimiter()` return.
929 Raises ValueError on no delimiters.
931 return max(v for k, v in self.delimiters.items() if k not in exclude)
933 def delimiter_count_with_priority(self, priority: int = 0) -> int:
934 """Return the number of delimiters with the given `priority`.
936 If no `priority` is passed, defaults to max priority on the line.
938 if not self.delimiters:
941 priority = priority or self.max_delimiter_priority()
942 return sum(1 for p in self.delimiters.values() if p == priority)
944 def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
945 """In a for loop, or comprehension, the variables are often unpacks.
947 To avoid splitting on the comma in this situation, increase the depth of
948 tokens between `for` and `in`.
950 if leaf.type == token.NAME and leaf.value == "for":
952 self._for_loop_variable += 1
957 def maybe_decrement_after_for_loop_variable(self, leaf: Leaf) -> bool:
958 """See `maybe_increment_for_loop_variable` above for explanation."""
959 if self._for_loop_variable and leaf.type == token.NAME and leaf.value == "in":
961 self._for_loop_variable -= 1
966 def maybe_increment_lambda_arguments(self, leaf: Leaf) -> bool:
967 """In a lambda expression, there might be more than one argument.
969 To avoid splitting on the comma in this situation, increase the depth of
970 tokens between `lambda` and `:`.
972 if leaf.type == token.NAME and leaf.value == "lambda":
974 self._lambda_arguments += 1
979 def maybe_decrement_after_lambda_arguments(self, leaf: Leaf) -> bool:
980 """See `maybe_increment_lambda_arguments` above for explanation."""
981 if self._lambda_arguments and leaf.type == token.COLON:
983 self._lambda_arguments -= 1
988 def get_open_lsqb(self) -> Optional[Leaf]:
989 """Return the most recent opening square bracket (if any)."""
990 return self.bracket_match.get((self.depth - 1, token.RSQB))
995 """Holds leaves and comments. Can be printed with `str(line)`."""
998 leaves: List[Leaf] = Factory(list)
999 comments: List[Tuple[Index, Leaf]] = Factory(list)
1000 bracket_tracker: BracketTracker = Factory(BracketTracker)
1001 inside_brackets: bool = False
1002 should_explode: bool = False
1004 def append(self, leaf: Leaf, preformatted: bool = False) -> None:
1005 """Add a new `leaf` to the end of the line.
1007 Unless `preformatted` is True, the `leaf` will receive a new consistent
1008 whitespace prefix and metadata applied by :class:`BracketTracker`.
1009 Trailing commas are maybe removed, unpacked for loop variables are
1010 demoted from being delimiters.
1012 Inline comments are put aside.
1014 has_value = leaf.type in BRACKETS or bool(leaf.value.strip())
1018 if token.COLON == leaf.type and self.is_class_paren_empty:
1019 del self.leaves[-2:]
1020 if self.leaves and not preformatted:
1021 # Note: at this point leaf.prefix should be empty except for
1022 # imports, for which we only preserve newlines.
1023 leaf.prefix += whitespace(
1024 leaf, complex_subscript=self.is_complex_subscript(leaf)
1026 if self.inside_brackets or not preformatted:
1027 self.bracket_tracker.mark(leaf)
1028 self.maybe_remove_trailing_comma(leaf)
1029 if not self.append_comment(leaf):
1030 self.leaves.append(leaf)
1032 def append_safe(self, leaf: Leaf, preformatted: bool = False) -> None:
1033 """Like :func:`append()` but disallow invalid standalone comment structure.
1035 Raises ValueError when any `leaf` is appended after a standalone comment
1036 or when a standalone comment is not the first leaf on the line.
1038 if self.bracket_tracker.depth == 0:
1040 raise ValueError("cannot append to standalone comments")
1042 if self.leaves and leaf.type == STANDALONE_COMMENT:
1044 "cannot append standalone comments to a populated line"
1047 self.append(leaf, preformatted=preformatted)
1050 def is_comment(self) -> bool:
1051 """Is this line a standalone comment?"""
1052 return len(self.leaves) == 1 and self.leaves[0].type == STANDALONE_COMMENT
1055 def is_decorator(self) -> bool:
1056 """Is this line a decorator?"""
1057 return bool(self) and self.leaves[0].type == token.AT
1060 def is_import(self) -> bool:
1061 """Is this an import line?"""
1062 return bool(self) and is_import(self.leaves[0])
1065 def is_class(self) -> bool:
1066 """Is this line a class definition?"""
1069 and self.leaves[0].type == token.NAME
1070 and self.leaves[0].value == "class"
1074 def is_stub_class(self) -> bool:
1075 """Is this line a class definition with a body consisting only of "..."?"""
1076 return self.is_class and self.leaves[-3:] == [
1077 Leaf(token.DOT, ".") for _ in range(3)
1081 def is_def(self) -> bool:
1082 """Is this a function definition? (Also returns True for async defs.)"""
1084 first_leaf = self.leaves[0]
1089 second_leaf: Optional[Leaf] = self.leaves[1]
1092 return (first_leaf.type == token.NAME and first_leaf.value == "def") or (
1093 first_leaf.type == token.ASYNC
1094 and second_leaf is not None
1095 and second_leaf.type == token.NAME
1096 and second_leaf.value == "def"
1100 def is_class_paren_empty(self) -> bool:
1101 """Is this a class with no base classes but using parentheses?
1103 Those are unnecessary and should be removed.
1107 and len(self.leaves) == 4
1109 and self.leaves[2].type == token.LPAR
1110 and self.leaves[2].value == "("
1111 and self.leaves[3].type == token.RPAR
1112 and self.leaves[3].value == ")"
1116 def is_triple_quoted_string(self) -> bool:
1117 """Is the line a triple quoted string?"""
1120 and self.leaves[0].type == token.STRING
1121 and self.leaves[0].value.startswith(('"""', "'''"))
1124 def contains_standalone_comments(self, depth_limit: int = sys.maxsize) -> bool:
1125 """If so, needs to be split before emitting."""
1126 for leaf in self.leaves:
1127 if leaf.type == STANDALONE_COMMENT:
1128 if leaf.bracket_depth <= depth_limit:
1133 def contains_multiline_strings(self) -> bool:
1134 for leaf in self.leaves:
1135 if is_multiline_string(leaf):
1140 def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
1141 """Remove trailing comma if there is one and it's safe."""
1144 and self.leaves[-1].type == token.COMMA
1145 and closing.type in CLOSING_BRACKETS
1149 if closing.type == token.RBRACE:
1150 self.remove_trailing_comma()
1153 if closing.type == token.RSQB:
1154 comma = self.leaves[-1]
1155 if comma.parent and comma.parent.type == syms.listmaker:
1156 self.remove_trailing_comma()
1159 # For parens let's check if it's safe to remove the comma.
1160 # Imports are always safe.
1162 self.remove_trailing_comma()
1165 # Otheriwsse, if the trailing one is the only one, we might mistakenly
1166 # change a tuple into a different type by removing the comma.
1167 depth = closing.bracket_depth + 1
1169 opening = closing.opening_bracket
1170 for _opening_index, leaf in enumerate(self.leaves):
1177 for leaf in self.leaves[_opening_index + 1 :]:
1181 bracket_depth = leaf.bracket_depth
1182 if bracket_depth == depth and leaf.type == token.COMMA:
1184 if leaf.parent and leaf.parent.type == syms.arglist:
1189 self.remove_trailing_comma()
1194 def append_comment(self, comment: Leaf) -> bool:
1195 """Add an inline or standalone comment to the line."""
1197 comment.type == STANDALONE_COMMENT
1198 and self.bracket_tracker.any_open_brackets()
1203 if comment.type != token.COMMENT:
1206 after = len(self.leaves) - 1
1208 comment.type = STANDALONE_COMMENT
1213 self.comments.append((after, comment))
1216 def comments_after(self, leaf: Leaf, _index: int = -1) -> Iterator[Leaf]:
1217 """Generate comments that should appear directly after `leaf`.
1219 Provide a non-negative leaf `_index` to speed up the function.
1221 if not self.comments:
1225 for _index, _leaf in enumerate(self.leaves):
1232 for index, comment_after in self.comments:
1236 def remove_trailing_comma(self) -> None:
1237 """Remove the trailing comma and moves the comments attached to it."""
1238 comma_index = len(self.leaves) - 1
1239 for i in range(len(self.comments)):
1240 comment_index, comment = self.comments[i]
1241 if comment_index == comma_index:
1242 self.comments[i] = (comma_index - 1, comment)
1245 def is_complex_subscript(self, leaf: Leaf) -> bool:
1246 """Return True iff `leaf` is part of a slice with non-trivial exprs."""
1247 open_lsqb = self.bracket_tracker.get_open_lsqb()
1248 if open_lsqb is None:
1251 subscript_start = open_lsqb.next_sibling
1253 if isinstance(subscript_start, Node):
1254 if subscript_start.type == syms.listmaker:
1257 if subscript_start.type == syms.subscriptlist:
1258 subscript_start = child_towards(subscript_start, leaf)
1259 return subscript_start is not None and any(
1260 n.type in TEST_DESCENDANTS for n in subscript_start.pre_order()
1263 def __str__(self) -> str:
1264 """Render the line."""
1268 indent = " " * self.depth
1269 leaves = iter(self.leaves)
1270 first = next(leaves)
1271 res = f"{first.prefix}{indent}{first.value}"
1274 for _, comment in self.comments:
1278 def __bool__(self) -> bool:
1279 """Return True if the line has leaves or comments."""
1280 return bool(self.leaves or self.comments)
1284 class EmptyLineTracker:
1285 """Provides a stateful method that returns the number of potential extra
1286 empty lines needed before and after the currently processed line.
1288 Note: this tracker works on lines that haven't been split yet. It assumes
1289 the prefix of the first leaf consists of optional newlines. Those newlines
1290 are consumed by `maybe_empty_lines()` and included in the computation.
1293 is_pyi: bool = False
1294 previous_line: Optional[Line] = None
1295 previous_after: int = 0
1296 previous_defs: List[int] = Factory(list)
1298 def maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
1299 """Return the number of extra empty lines before and after the `current_line`.
1301 This is for separating `def`, `async def` and `class` with extra empty
1302 lines (two on module-level).
1304 before, after = self._maybe_empty_lines(current_line)
1305 before -= self.previous_after
1306 self.previous_after = after
1307 self.previous_line = current_line
1308 return before, after
1310 def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
1312 if current_line.depth == 0:
1313 max_allowed = 1 if self.is_pyi else 2
1314 if current_line.leaves:
1315 # Consume the first leaf's extra newlines.
1316 first_leaf = current_line.leaves[0]
1317 before = first_leaf.prefix.count("\n")
1318 before = min(before, max_allowed)
1319 first_leaf.prefix = ""
1322 depth = current_line.depth
1323 while self.previous_defs and self.previous_defs[-1] >= depth:
1324 self.previous_defs.pop()
1326 before = 0 if depth else 1
1328 before = 1 if depth else 2
1329 if current_line.is_decorator or current_line.is_def or current_line.is_class:
1330 return self._maybe_empty_lines_for_class_or_def(current_line, before)
1334 and self.previous_line.is_import
1335 and not current_line.is_import
1336 and depth == self.previous_line.depth
1338 return (before or 1), 0
1342 and self.previous_line.is_class
1343 and current_line.is_triple_quoted_string
1349 def _maybe_empty_lines_for_class_or_def(
1350 self, current_line: Line, before: int
1351 ) -> Tuple[int, int]:
1352 if not current_line.is_decorator:
1353 self.previous_defs.append(current_line.depth)
1354 if self.previous_line is None:
1355 # Don't insert empty lines before the first line in the file.
1358 if self.previous_line.is_decorator:
1361 if self.previous_line.depth < current_line.depth and (
1362 self.previous_line.is_class or self.previous_line.is_def
1367 self.previous_line.is_comment
1368 and self.previous_line.depth == current_line.depth
1374 if self.previous_line.depth > current_line.depth:
1376 elif current_line.is_class or self.previous_line.is_class:
1377 if current_line.is_stub_class and self.previous_line.is_stub_class:
1378 # No blank line between classes with an emty body
1382 elif current_line.is_def and not self.previous_line.is_def:
1383 # Blank line between a block of functions and a block of non-functions
1389 if current_line.depth and newlines:
1395 class LineGenerator(Visitor[Line]):
1396 """Generates reformatted Line objects. Empty lines are not emitted.
1398 Note: destroys the tree it's visiting by mutating prefixes of its leaves
1399 in ways that will no longer stringify to valid Python code on the tree.
1402 is_pyi: bool = False
1403 normalize_strings: bool = True
1404 current_line: Line = Factory(Line)
1405 remove_u_prefix: bool = False
1406 allow_underscores: bool = False
1408 def line(self, indent: int = 0) -> Iterator[Line]:
1411 If the line is empty, only emit if it makes sense.
1412 If the line is too long, split it first and then generate.
1414 If any lines were generated, set up a new current_line.
1416 if not self.current_line:
1417 self.current_line.depth += indent
1418 return # Line is empty, don't emit. Creating a new one unnecessary.
1420 complete_line = self.current_line
1421 self.current_line = Line(depth=complete_line.depth + indent)
1424 def visit_default(self, node: LN) -> Iterator[Line]:
1425 """Default `visit_*()` implementation. Recurses to children of `node`."""
1426 if isinstance(node, Leaf):
1427 any_open_brackets = self.current_line.bracket_tracker.any_open_brackets()
1428 for comment in generate_comments(node):
1429 if any_open_brackets:
1430 # any comment within brackets is subject to splitting
1431 self.current_line.append(comment)
1432 elif comment.type == token.COMMENT:
1433 # regular trailing comment
1434 self.current_line.append(comment)
1435 yield from self.line()
1438 # regular standalone comment
1439 yield from self.line()
1441 self.current_line.append(comment)
1442 yield from self.line()
1444 normalize_prefix(node, inside_brackets=any_open_brackets)
1445 if self.normalize_strings and node.type == token.STRING:
1446 normalize_string_prefix(node, remove_u_prefix=self.remove_u_prefix)
1447 normalize_string_quotes(node)
1448 if node.type == token.NUMBER:
1449 normalize_numeric_literal(node, self.allow_underscores)
1450 if node.type not in WHITESPACE:
1451 self.current_line.append(node)
1452 yield from super().visit_default(node)
1454 def visit_INDENT(self, node: Node) -> Iterator[Line]:
1455 """Increase indentation level, maybe yield a line."""
1456 # In blib2to3 INDENT never holds comments.
1457 yield from self.line(+1)
1458 yield from self.visit_default(node)
1460 def visit_DEDENT(self, node: Node) -> Iterator[Line]:
1461 """Decrease indentation level, maybe yield a line."""
1462 # The current line might still wait for trailing comments. At DEDENT time
1463 # there won't be any (they would be prefixes on the preceding NEWLINE).
1464 # Emit the line then.
1465 yield from self.line()
1467 # While DEDENT has no value, its prefix may contain standalone comments
1468 # that belong to the current indentation level. Get 'em.
1469 yield from self.visit_default(node)
1471 # Finally, emit the dedent.
1472 yield from self.line(-1)
1475 self, node: Node, keywords: Set[str], parens: Set[str]
1476 ) -> Iterator[Line]:
1477 """Visit a statement.
1479 This implementation is shared for `if`, `while`, `for`, `try`, `except`,
1480 `def`, `with`, `class`, `assert` and assignments.
1482 The relevant Python language `keywords` for a given statement will be
1483 NAME leaves within it. This methods puts those on a separate line.
1485 `parens` holds a set of string leaf values immediately after which
1486 invisible parens should be put.
1488 normalize_invisible_parens(node, parens_after=parens)
1489 for child in node.children:
1490 if child.type == token.NAME and child.value in keywords: # type: ignore
1491 yield from self.line()
1493 yield from self.visit(child)
1495 def visit_suite(self, node: Node) -> Iterator[Line]:
1496 """Visit a suite."""
1497 if self.is_pyi and is_stub_suite(node):
1498 yield from self.visit(node.children[2])
1500 yield from self.visit_default(node)
1502 def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
1503 """Visit a statement without nested statements."""
1504 is_suite_like = node.parent and node.parent.type in STATEMENT
1506 if self.is_pyi and is_stub_body(node):
1507 yield from self.visit_default(node)
1509 yield from self.line(+1)
1510 yield from self.visit_default(node)
1511 yield from self.line(-1)
1514 if not self.is_pyi or not node.parent or not is_stub_suite(node.parent):
1515 yield from self.line()
1516 yield from self.visit_default(node)
1518 def visit_async_stmt(self, node: Node) -> Iterator[Line]:
1519 """Visit `async def`, `async for`, `async with`."""
1520 yield from self.line()
1522 children = iter(node.children)
1523 for child in children:
1524 yield from self.visit(child)
1526 if child.type == token.ASYNC:
1529 internal_stmt = next(children)
1530 for child in internal_stmt.children:
1531 yield from self.visit(child)
1533 def visit_decorators(self, node: Node) -> Iterator[Line]:
1534 """Visit decorators."""
1535 for child in node.children:
1536 yield from self.line()
1537 yield from self.visit(child)
1539 def visit_SEMI(self, leaf: Leaf) -> Iterator[Line]:
1540 """Remove a semicolon and put the other statement on a separate line."""
1541 yield from self.line()
1543 def visit_ENDMARKER(self, leaf: Leaf) -> Iterator[Line]:
1544 """End of file. Process outstanding comments and end with a newline."""
1545 yield from self.visit_default(leaf)
1546 yield from self.line()
1548 def visit_STANDALONE_COMMENT(self, leaf: Leaf) -> Iterator[Line]:
1549 if not self.current_line.bracket_tracker.any_open_brackets():
1550 yield from self.line()
1551 yield from self.visit_default(leaf)
1553 def __attrs_post_init__(self) -> None:
1554 """You are in a twisty little maze of passages."""
1557 self.visit_assert_stmt = partial(v, keywords={"assert"}, parens={"assert", ","})
1558 self.visit_if_stmt = partial(
1559 v, keywords={"if", "else", "elif"}, parens={"if", "elif"}
1561 self.visit_while_stmt = partial(v, keywords={"while", "else"}, parens={"while"})
1562 self.visit_for_stmt = partial(v, keywords={"for", "else"}, parens={"for", "in"})
1563 self.visit_try_stmt = partial(
1564 v, keywords={"try", "except", "else", "finally"}, parens=Ø
1566 self.visit_except_clause = partial(v, keywords={"except"}, parens=Ø)
1567 self.visit_with_stmt = partial(v, keywords={"with"}, parens=Ø)
1568 self.visit_funcdef = partial(v, keywords={"def"}, parens=Ø)
1569 self.visit_classdef = partial(v, keywords={"class"}, parens=Ø)
1570 self.visit_expr_stmt = partial(v, keywords=Ø, parens=ASSIGNMENTS)
1571 self.visit_return_stmt = partial(v, keywords={"return"}, parens={"return"})
1572 self.visit_import_from = partial(v, keywords=Ø, parens={"import"})
1573 self.visit_async_funcdef = self.visit_async_stmt
1574 self.visit_decorated = self.visit_decorators
1577 IMPLICIT_TUPLE = {syms.testlist, syms.testlist_star_expr, syms.exprlist}
1578 BRACKET = {token.LPAR: token.RPAR, token.LSQB: token.RSQB, token.LBRACE: token.RBRACE}
1579 OPENING_BRACKETS = set(BRACKET.keys())
1580 CLOSING_BRACKETS = set(BRACKET.values())
1581 BRACKETS = OPENING_BRACKETS | CLOSING_BRACKETS
1582 ALWAYS_NO_SPACE = CLOSING_BRACKETS | {token.COMMA, STANDALONE_COMMENT}
1585 def whitespace(leaf: Leaf, *, complex_subscript: bool) -> str: # noqa C901
1586 """Return whitespace prefix if needed for the given `leaf`.
1588 `complex_subscript` signals whether the given leaf is part of a subscription
1589 which has non-trivial arguments, like arithmetic expressions or function calls.
1597 if t in ALWAYS_NO_SPACE:
1600 if t == token.COMMENT:
1603 assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
1604 if t == token.COLON and p.type not in {
1611 prev = leaf.prev_sibling
1613 prevp = preceding_leaf(p)
1614 if not prevp or prevp.type in OPENING_BRACKETS:
1617 if t == token.COLON:
1618 if prevp.type == token.COLON:
1621 elif prevp.type != token.COMMA and not complex_subscript:
1626 if prevp.type == token.EQUAL:
1628 if prevp.parent.type in {
1636 elif prevp.parent.type == syms.typedargslist:
1637 # A bit hacky: if the equal sign has whitespace, it means we
1638 # previously found it's a typed argument. So, we're using
1642 elif prevp.type in STARS:
1643 if is_vararg(prevp, within=VARARGS_PARENTS | UNPACKING_PARENTS):
1646 elif prevp.type == token.COLON:
1647 if prevp.parent and prevp.parent.type in {syms.subscript, syms.sliceop}:
1648 return SPACE if complex_subscript else NO
1652 and prevp.parent.type == syms.factor
1653 and prevp.type in MATH_OPERATORS
1658 prevp.type == token.RIGHTSHIFT
1660 and prevp.parent.type == syms.shift_expr
1661 and prevp.prev_sibling
1662 and prevp.prev_sibling.type == token.NAME
1663 and prevp.prev_sibling.value == "print" # type: ignore
1665 # Python 2 print chevron
1668 elif prev.type in OPENING_BRACKETS:
1671 if p.type in {syms.parameters, syms.arglist}:
1672 # untyped function signatures or calls
1673 if not prev or prev.type != token.COMMA:
1676 elif p.type == syms.varargslist:
1678 if prev and prev.type != token.COMMA:
1681 elif p.type == syms.typedargslist:
1682 # typed function signatures
1686 if t == token.EQUAL:
1687 if prev.type != syms.tname:
1690 elif prev.type == token.EQUAL:
1691 # A bit hacky: if the equal sign has whitespace, it means we
1692 # previously found it's a typed argument. So, we're using that, too.
1695 elif prev.type != token.COMMA:
1698 elif p.type == syms.tname:
1701 prevp = preceding_leaf(p)
1702 if not prevp or prevp.type != token.COMMA:
1705 elif p.type == syms.trailer:
1706 # attributes and calls
1707 if t == token.LPAR or t == token.RPAR:
1712 prevp = preceding_leaf(p)
1713 if not prevp or prevp.type != token.NUMBER:
1716 elif t == token.LSQB:
1719 elif prev.type != token.COMMA:
1722 elif p.type == syms.argument:
1724 if t == token.EQUAL:
1728 prevp = preceding_leaf(p)
1729 if not prevp or prevp.type == token.LPAR:
1732 elif prev.type in {token.EQUAL} | STARS:
1735 elif p.type == syms.decorator:
1739 elif p.type == syms.dotted_name:
1743 prevp = preceding_leaf(p)
1744 if not prevp or prevp.type == token.AT or prevp.type == token.DOT:
1747 elif p.type == syms.classdef:
1751 if prev and prev.type == token.LPAR:
1754 elif p.type in {syms.subscript, syms.sliceop}:
1757 assert p.parent is not None, "subscripts are always parented"
1758 if p.parent.type == syms.subscriptlist:
1763 elif not complex_subscript:
1766 elif p.type == syms.atom:
1767 if prev and t == token.DOT:
1768 # dots, but not the first one.
1771 elif p.type == syms.dictsetmaker:
1773 if prev and prev.type == token.DOUBLESTAR:
1776 elif p.type in {syms.factor, syms.star_expr}:
1779 prevp = preceding_leaf(p)
1780 if not prevp or prevp.type in OPENING_BRACKETS:
1783 prevp_parent = prevp.parent
1784 assert prevp_parent is not None
1785 if prevp.type == token.COLON and prevp_parent.type in {
1791 elif prevp.type == token.EQUAL and prevp_parent.type == syms.argument:
1794 elif t in {token.NAME, token.NUMBER, token.STRING}:
1797 elif p.type == syms.import_from:
1799 if prev and prev.type == token.DOT:
1802 elif t == token.NAME:
1806 if prev and prev.type == token.DOT:
1809 elif p.type == syms.sliceop:
1815 def preceding_leaf(node: Optional[LN]) -> Optional[Leaf]:
1816 """Return the first leaf that precedes `node`, if any."""
1818 res = node.prev_sibling
1820 if isinstance(res, Leaf):
1824 return list(res.leaves())[-1]
1833 def child_towards(ancestor: Node, descendant: LN) -> Optional[LN]:
1834 """Return the child of `ancestor` that contains `descendant`."""
1835 node: Optional[LN] = descendant
1836 while node and node.parent != ancestor:
1841 def container_of(leaf: Leaf) -> LN:
1842 """Return `leaf` or one of its ancestors that is the topmost container of it.
1844 By "container" we mean a node where `leaf` is the very first child.
1846 same_prefix = leaf.prefix
1847 container: LN = leaf
1849 parent = container.parent
1853 if parent.children[0].prefix != same_prefix:
1856 if parent.type == syms.file_input:
1859 if parent.prev_sibling is not None and parent.prev_sibling.type in BRACKETS:
1866 def is_split_after_delimiter(leaf: Leaf, previous: Leaf = None) -> int:
1867 """Return the priority of the `leaf` delimiter, given a line break after it.
1869 The delimiter priorities returned here are from those delimiters that would
1870 cause a line break after themselves.
1872 Higher numbers are higher priority.
1874 if leaf.type == token.COMMA:
1875 return COMMA_PRIORITY
1880 def is_split_before_delimiter(leaf: Leaf, previous: Leaf = None) -> int:
1881 """Return the priority of the `leaf` delimiter, given a line before after it.
1883 The delimiter priorities returned here are from those delimiters that would
1884 cause a line break before themselves.
1886 Higher numbers are higher priority.
1888 if is_vararg(leaf, within=VARARGS_PARENTS | UNPACKING_PARENTS):
1889 # * and ** might also be MATH_OPERATORS but in this case they are not.
1890 # Don't treat them as a delimiter.
1894 leaf.type == token.DOT
1896 and leaf.parent.type not in {syms.import_from, syms.dotted_name}
1897 and (previous is None or previous.type in CLOSING_BRACKETS)
1902 leaf.type in MATH_OPERATORS
1904 and leaf.parent.type not in {syms.factor, syms.star_expr}
1906 return MATH_PRIORITIES[leaf.type]
1908 if leaf.type in COMPARATORS:
1909 return COMPARATOR_PRIORITY
1912 leaf.type == token.STRING
1913 and previous is not None
1914 and previous.type == token.STRING
1916 return STRING_PRIORITY
1918 if leaf.type != token.NAME:
1924 and leaf.parent.type in {syms.comp_for, syms.old_comp_for}
1926 return COMPREHENSION_PRIORITY
1931 and leaf.parent.type in {syms.comp_if, syms.old_comp_if}
1933 return COMPREHENSION_PRIORITY
1935 if leaf.value in {"if", "else"} and leaf.parent and leaf.parent.type == syms.test:
1936 return TERNARY_PRIORITY
1938 if leaf.value == "is":
1939 return COMPARATOR_PRIORITY
1944 and leaf.parent.type in {syms.comp_op, syms.comparison}
1946 previous is not None
1947 and previous.type == token.NAME
1948 and previous.value == "not"
1951 return COMPARATOR_PRIORITY
1956 and leaf.parent.type == syms.comp_op
1958 previous is not None
1959 and previous.type == token.NAME
1960 and previous.value == "is"
1963 return COMPARATOR_PRIORITY
1965 if leaf.value in LOGIC_OPERATORS and leaf.parent:
1966 return LOGIC_PRIORITY
1971 FMT_OFF = {"# fmt: off", "# fmt:off", "# yapf: disable"}
1972 FMT_ON = {"# fmt: on", "# fmt:on", "# yapf: enable"}
1975 def generate_comments(leaf: LN) -> Iterator[Leaf]:
1976 """Clean the prefix of the `leaf` and generate comments from it, if any.
1978 Comments in lib2to3 are shoved into the whitespace prefix. This happens
1979 in `pgen2/driver.py:Driver.parse_tokens()`. This was a brilliant implementation
1980 move because it does away with modifying the grammar to include all the
1981 possible places in which comments can be placed.
1983 The sad consequence for us though is that comments don't "belong" anywhere.
1984 This is why this function generates simple parentless Leaf objects for
1985 comments. We simply don't know what the correct parent should be.
1987 No matter though, we can live without this. We really only need to
1988 differentiate between inline and standalone comments. The latter don't
1989 share the line with any code.
1991 Inline comments are emitted as regular token.COMMENT leaves. Standalone
1992 are emitted with a fake STANDALONE_COMMENT token identifier.
1994 for pc in list_comments(leaf.prefix, is_endmarker=leaf.type == token.ENDMARKER):
1995 yield Leaf(pc.type, pc.value, prefix="\n" * pc.newlines)
2000 type: int # token.COMMENT or STANDALONE_COMMENT
2001 value: str # content of the comment
2002 newlines: int # how many newlines before the comment
2003 consumed: int # how many characters of the original leaf's prefix did we consume
2006 @lru_cache(maxsize=4096)
2007 def list_comments(prefix: str, *, is_endmarker: bool) -> List[ProtoComment]:
2008 result: List[ProtoComment] = []
2009 if not prefix or "#" not in prefix:
2014 for index, line in enumerate(prefix.split("\n")):
2015 consumed += len(line) + 1 # adding the length of the split '\n'
2016 line = line.lstrip()
2019 if not line.startswith("#"):
2022 if index == 0 and not is_endmarker:
2023 comment_type = token.COMMENT # simple trailing comment
2025 comment_type = STANDALONE_COMMENT
2026 comment = make_comment(line)
2029 type=comment_type, value=comment, newlines=nlines, consumed=consumed
2036 def make_comment(content: str) -> str:
2037 """Return a consistently formatted comment from the given `content` string.
2039 All comments (except for "##", "#!", "#:") should have a single space between
2040 the hash sign and the content.
2042 If `content` didn't start with a hash sign, one is provided.
2044 content = content.rstrip()
2048 if content[0] == "#":
2049 content = content[1:]
2050 if content and content[0] not in " !:#":
2051 content = " " + content
2052 return "#" + content
2056 line: Line, line_length: int, inner: bool = False, py36: bool = False
2057 ) -> Iterator[Line]:
2058 """Split a `line` into potentially many lines.
2060 They should fit in the allotted `line_length` but might not be able to.
2061 `inner` signifies that there were a pair of brackets somewhere around the
2062 current `line`, possibly transitively. This means we can fallback to splitting
2063 by delimiters if the LHS/RHS don't yield any results.
2065 If `py36` is True, splitting may generate syntax that is only compatible
2066 with Python 3.6 and later.
2072 line_str = str(line).strip("\n")
2073 if not line.should_explode and is_line_short_enough(
2074 line, line_length=line_length, line_str=line_str
2079 split_funcs: List[SplitFunc]
2081 split_funcs = [left_hand_split]
2084 def rhs(line: Line, py36: bool = False) -> Iterator[Line]:
2085 for omit in generate_trailers_to_omit(line, line_length):
2086 lines = list(right_hand_split(line, line_length, py36, omit=omit))
2087 if is_line_short_enough(lines[0], line_length=line_length):
2091 # All splits failed, best effort split with no omits.
2092 # This mostly happens to multiline strings that are by definition
2093 # reported as not fitting a single line.
2094 yield from right_hand_split(line, py36)
2096 if line.inside_brackets:
2097 split_funcs = [delimiter_split, standalone_comment_split, rhs]
2100 for split_func in split_funcs:
2101 # We are accumulating lines in `result` because we might want to abort
2102 # mission and return the original line in the end, or attempt a different
2104 result: List[Line] = []
2106 for l in split_func(line, py36):
2107 if str(l).strip("\n") == line_str:
2108 raise CannotSplit("Split function returned an unchanged result")
2111 split_line(l, line_length=line_length, inner=True, py36=py36)
2113 except CannotSplit as cs:
2124 def left_hand_split(line: Line, py36: bool = False) -> Iterator[Line]:
2125 """Split line into many lines, starting with the first matching bracket pair.
2127 Note: this usually looks weird, only use this for function definitions.
2128 Prefer RHS otherwise. This is why this function is not symmetrical with
2129 :func:`right_hand_split` which also handles optional parentheses.
2131 head = Line(depth=line.depth)
2132 body = Line(depth=line.depth + 1, inside_brackets=True)
2133 tail = Line(depth=line.depth)
2134 tail_leaves: List[Leaf] = []
2135 body_leaves: List[Leaf] = []
2136 head_leaves: List[Leaf] = []
2137 current_leaves = head_leaves
2138 matching_bracket = None
2139 for leaf in line.leaves:
2141 current_leaves is body_leaves
2142 and leaf.type in CLOSING_BRACKETS
2143 and leaf.opening_bracket is matching_bracket
2145 current_leaves = tail_leaves if body_leaves else head_leaves
2146 current_leaves.append(leaf)
2147 if current_leaves is head_leaves:
2148 if leaf.type in OPENING_BRACKETS:
2149 matching_bracket = leaf
2150 current_leaves = body_leaves
2151 # Since body is a new indent level, remove spurious leading whitespace.
2153 normalize_prefix(body_leaves[0], inside_brackets=True)
2154 # Build the new lines.
2155 for result, leaves in (head, head_leaves), (body, body_leaves), (tail, tail_leaves):
2157 result.append(leaf, preformatted=True)
2158 for comment_after in line.comments_after(leaf):
2159 result.append(comment_after, preformatted=True)
2160 bracket_split_succeeded_or_raise(head, body, tail)
2161 for result in (head, body, tail):
2166 def right_hand_split(
2167 line: Line, line_length: int, py36: bool = False, omit: Collection[LeafID] = ()
2168 ) -> Iterator[Line]:
2169 """Split line into many lines, starting with the last matching bracket pair.
2171 If the split was by optional parentheses, attempt splitting without them, too.
2172 `omit` is a collection of closing bracket IDs that shouldn't be considered for
2175 Note: running this function modifies `bracket_depth` on the leaves of `line`.
2177 head = Line(depth=line.depth)
2178 body = Line(depth=line.depth + 1, inside_brackets=True)
2179 tail = Line(depth=line.depth)
2180 tail_leaves: List[Leaf] = []
2181 body_leaves: List[Leaf] = []
2182 head_leaves: List[Leaf] = []
2183 current_leaves = tail_leaves
2184 opening_bracket = None
2185 closing_bracket = None
2186 for leaf in reversed(line.leaves):
2187 if current_leaves is body_leaves:
2188 if leaf is opening_bracket:
2189 current_leaves = head_leaves if body_leaves else tail_leaves
2190 current_leaves.append(leaf)
2191 if current_leaves is tail_leaves:
2192 if leaf.type in CLOSING_BRACKETS and id(leaf) not in omit:
2193 opening_bracket = leaf.opening_bracket
2194 closing_bracket = leaf
2195 current_leaves = body_leaves
2196 tail_leaves.reverse()
2197 body_leaves.reverse()
2198 head_leaves.reverse()
2199 # Since body is a new indent level, remove spurious leading whitespace.
2201 normalize_prefix(body_leaves[0], inside_brackets=True)
2203 # No `head` means the split failed. Either `tail` has all content or
2204 # the matching `opening_bracket` wasn't available on `line` anymore.
2205 raise CannotSplit("No brackets found")
2207 # Build the new lines.
2208 for result, leaves in (head, head_leaves), (body, body_leaves), (tail, tail_leaves):
2210 result.append(leaf, preformatted=True)
2211 for comment_after in line.comments_after(leaf):
2212 result.append(comment_after, preformatted=True)
2213 assert opening_bracket and closing_bracket
2214 body.should_explode = should_explode(body, opening_bracket)
2215 bracket_split_succeeded_or_raise(head, body, tail)
2217 # the body shouldn't be exploded
2218 not body.should_explode
2219 # the opening bracket is an optional paren
2220 and opening_bracket.type == token.LPAR
2221 and not opening_bracket.value
2222 # the closing bracket is an optional paren
2223 and closing_bracket.type == token.RPAR
2224 and not closing_bracket.value
2225 # it's not an import (optional parens are the only thing we can split on
2226 # in this case; attempting a split without them is a waste of time)
2227 and not line.is_import
2228 # there are no standalone comments in the body
2229 and not body.contains_standalone_comments(0)
2230 # and we can actually remove the parens
2231 and can_omit_invisible_parens(body, line_length)
2233 omit = {id(closing_bracket), *omit}
2235 yield from right_hand_split(line, line_length, py36=py36, omit=omit)
2241 or is_line_short_enough(body, line_length=line_length)
2244 "Splitting failed, body is still too long and can't be split."
2247 elif head.contains_multiline_strings() or tail.contains_multiline_strings():
2249 "The current optional pair of parentheses is bound to fail to "
2250 "satisfy the splitting algorithm because the head or the tail "
2251 "contains multiline strings which by definition never fit one "
2255 ensure_visible(opening_bracket)
2256 ensure_visible(closing_bracket)
2257 for result in (head, body, tail):
2262 def bracket_split_succeeded_or_raise(head: Line, body: Line, tail: Line) -> None:
2263 """Raise :exc:`CannotSplit` if the last left- or right-hand split failed.
2265 Do nothing otherwise.
2267 A left- or right-hand split is based on a pair of brackets. Content before
2268 (and including) the opening bracket is left on one line, content inside the
2269 brackets is put on a separate line, and finally content starting with and
2270 following the closing bracket is put on a separate line.
2272 Those are called `head`, `body`, and `tail`, respectively. If the split
2273 produced the same line (all content in `head`) or ended up with an empty `body`
2274 and the `tail` is just the closing bracket, then it's considered failed.
2276 tail_len = len(str(tail).strip())
2279 raise CannotSplit("Splitting brackets produced the same line")
2283 f"Splitting brackets on an empty body to save "
2284 f"{tail_len} characters is not worth it"
2288 def dont_increase_indentation(split_func: SplitFunc) -> SplitFunc:
2289 """Normalize prefix of the first leaf in every line returned by `split_func`.
2291 This is a decorator over relevant split functions.
2295 def split_wrapper(line: Line, py36: bool = False) -> Iterator[Line]:
2296 for l in split_func(line, py36):
2297 normalize_prefix(l.leaves[0], inside_brackets=True)
2300 return split_wrapper
2303 @dont_increase_indentation
2304 def delimiter_split(line: Line, py36: bool = False) -> Iterator[Line]:
2305 """Split according to delimiters of the highest priority.
2307 If `py36` is True, the split will add trailing commas also in function
2308 signatures that contain `*` and `**`.
2311 last_leaf = line.leaves[-1]
2313 raise CannotSplit("Line empty")
2315 bt = line.bracket_tracker
2317 delimiter_priority = bt.max_delimiter_priority(exclude={id(last_leaf)})
2319 raise CannotSplit("No delimiters found")
2321 if delimiter_priority == DOT_PRIORITY:
2322 if bt.delimiter_count_with_priority(delimiter_priority) == 1:
2323 raise CannotSplit("Splitting a single attribute from its owner looks wrong")
2325 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
2326 lowest_depth = sys.maxsize
2327 trailing_comma_safe = True
2329 def append_to_line(leaf: Leaf) -> Iterator[Line]:
2330 """Append `leaf` to current line or to new line if appending impossible."""
2331 nonlocal current_line
2333 current_line.append_safe(leaf, preformatted=True)
2334 except ValueError as ve:
2337 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
2338 current_line.append(leaf)
2340 for index, leaf in enumerate(line.leaves):
2341 yield from append_to_line(leaf)
2343 for comment_after in line.comments_after(leaf, index):
2344 yield from append_to_line(comment_after)
2346 lowest_depth = min(lowest_depth, leaf.bracket_depth)
2347 if leaf.bracket_depth == lowest_depth and is_vararg(
2348 leaf, within=VARARGS_PARENTS
2350 trailing_comma_safe = trailing_comma_safe and py36
2351 leaf_priority = bt.delimiters.get(id(leaf))
2352 if leaf_priority == delimiter_priority:
2355 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
2359 and delimiter_priority == COMMA_PRIORITY
2360 and current_line.leaves[-1].type != token.COMMA
2361 and current_line.leaves[-1].type != STANDALONE_COMMENT
2363 current_line.append(Leaf(token.COMMA, ","))
2367 @dont_increase_indentation
2368 def standalone_comment_split(line: Line, py36: bool = False) -> Iterator[Line]:
2369 """Split standalone comments from the rest of the line."""
2370 if not line.contains_standalone_comments(0):
2371 raise CannotSplit("Line does not have any standalone comments")
2373 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
2375 def append_to_line(leaf: Leaf) -> Iterator[Line]:
2376 """Append `leaf` to current line or to new line if appending impossible."""
2377 nonlocal current_line
2379 current_line.append_safe(leaf, preformatted=True)
2380 except ValueError as ve:
2383 current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
2384 current_line.append(leaf)
2386 for index, leaf in enumerate(line.leaves):
2387 yield from append_to_line(leaf)
2389 for comment_after in line.comments_after(leaf, index):
2390 yield from append_to_line(comment_after)
2396 def is_import(leaf: Leaf) -> bool:
2397 """Return True if the given leaf starts an import statement."""
2404 (v == "import" and p and p.type == syms.import_name)
2405 or (v == "from" and p and p.type == syms.import_from)
2410 def normalize_prefix(leaf: Leaf, *, inside_brackets: bool) -> None:
2411 """Leave existing extra newlines if not `inside_brackets`. Remove everything
2414 Note: don't use backslashes for formatting or you'll lose your voting rights.
2416 if not inside_brackets:
2417 spl = leaf.prefix.split("#")
2418 if "\\" not in spl[0]:
2419 nl_count = spl[-1].count("\n")
2422 leaf.prefix = "\n" * nl_count
2428 def normalize_string_prefix(leaf: Leaf, remove_u_prefix: bool = False) -> None:
2429 """Make all string prefixes lowercase.
2431 If remove_u_prefix is given, also removes any u prefix from the string.
2433 Note: Mutates its argument.
2435 match = re.match(r"^([furbFURB]*)(.*)$", leaf.value, re.DOTALL)
2436 assert match is not None, f"failed to match string {leaf.value!r}"
2437 orig_prefix = match.group(1)
2438 new_prefix = orig_prefix.lower()
2440 new_prefix = new_prefix.replace("u", "")
2441 leaf.value = f"{new_prefix}{match.group(2)}"
2444 def normalize_string_quotes(leaf: Leaf) -> None:
2445 """Prefer double quotes but only if it doesn't cause more escaping.
2447 Adds or removes backslashes as appropriate. Doesn't parse and fix
2448 strings nested in f-strings (yet).
2450 Note: Mutates its argument.
2452 value = leaf.value.lstrip("furbFURB")
2453 if value[:3] == '"""':
2456 elif value[:3] == "'''":
2459 elif value[0] == '"':
2465 first_quote_pos = leaf.value.find(orig_quote)
2466 if first_quote_pos == -1:
2467 return # There's an internal error
2469 prefix = leaf.value[:first_quote_pos]
2470 unescaped_new_quote = re.compile(rf"(([^\\]|^)(\\\\)*){new_quote}")
2471 escaped_new_quote = re.compile(rf"([^\\]|^)\\((?:\\\\)*){new_quote}")
2472 escaped_orig_quote = re.compile(rf"([^\\]|^)\\((?:\\\\)*){orig_quote}")
2473 body = leaf.value[first_quote_pos + len(orig_quote) : -len(orig_quote)]
2474 if "r" in prefix.casefold():
2475 if unescaped_new_quote.search(body):
2476 # There's at least one unescaped new_quote in this raw string
2477 # so converting is impossible
2480 # Do not introduce or remove backslashes in raw strings
2483 # remove unnecessary escapes
2484 new_body = sub_twice(escaped_new_quote, rf"\1\2{new_quote}", body)
2485 if body != new_body:
2486 # Consider the string without unnecessary escapes as the original
2488 leaf.value = f"{prefix}{orig_quote}{body}{orig_quote}"
2489 new_body = sub_twice(escaped_orig_quote, rf"\1\2{orig_quote}", new_body)
2490 new_body = sub_twice(unescaped_new_quote, rf"\1\\{new_quote}", new_body)
2491 if "f" in prefix.casefold():
2492 matches = re.findall(r"[^{]\{(.*?)\}[^}]", new_body)
2495 # Do not introduce backslashes in interpolated expressions
2497 if new_quote == '"""' and new_body[-1:] == '"':
2499 new_body = new_body[:-1] + '\\"'
2500 orig_escape_count = body.count("\\")
2501 new_escape_count = new_body.count("\\")
2502 if new_escape_count > orig_escape_count:
2503 return # Do not introduce more escaping
2505 if new_escape_count == orig_escape_count and orig_quote == '"':
2506 return # Prefer double quotes
2508 leaf.value = f"{prefix}{new_quote}{new_body}{new_quote}"
2511 def normalize_numeric_literal(leaf: Leaf, allow_underscores: bool) -> None:
2512 """Normalizes numeric (float, int, and complex) literals."""
2513 # We want all letters (e in exponents, j in complex literals, a-f
2514 # in hex literals) to be lowercase.
2515 text = leaf.value.lower()
2516 if text.startswith(("0o", "0x", "0b")):
2517 # Leave octal, hex, and binary literals alone for now.
2520 before, after = text.split("e")
2521 if after.startswith("-"):
2524 elif after.startswith("+"):
2529 before = format_float_or_int_string(before, allow_underscores)
2530 after = format_int_string(after, allow_underscores)
2531 text = f"{before}e{sign}{after}"
2532 # Complex numbers and Python 2 longs
2533 elif "j" in text or "l" in text:
2536 text = f"{format_float_or_int_string(number, allow_underscores)}{suffix}"
2538 text = format_float_or_int_string(text, allow_underscores)
2542 def format_float_or_int_string(text: str, allow_underscores: bool) -> str:
2543 """Formats a float string like "1.0"."""
2545 return format_int_string(text, allow_underscores)
2546 before, after = text.split(".")
2547 before = format_int_string(before, allow_underscores) if before else "0"
2548 after = format_int_string(after, allow_underscores) if after else "0"
2549 return f"{before}.{after}"
2552 def format_int_string(text: str, allow_underscores: bool) -> str:
2553 """Normalizes underscores in a string to e.g. 1_000_000.
2555 Input must be a string consisting only of digits and underscores.
2557 if not allow_underscores:
2559 text = text.replace("_", "")
2561 # No underscores for numbers <= 6 digits long.
2563 return format(int(text), "3_")
2566 def normalize_invisible_parens(node: Node, parens_after: Set[str]) -> None:
2567 """Make existing optional parentheses invisible or create new ones.
2569 `parens_after` is a set of string leaf values immeditely after which parens
2572 Standardizes on visible parentheses for single-element tuples, and keeps
2573 existing visible parentheses for other tuples and generator expressions.
2575 for pc in list_comments(node.prefix, is_endmarker=False):
2576 if pc.value in FMT_OFF:
2577 # This `node` has a prefix with `# fmt: off`, don't mess with parens.
2581 for index, child in enumerate(list(node.children)):
2583 if child.type == syms.atom:
2584 maybe_make_parens_invisible_in_atom(child)
2585 elif is_one_tuple(child):
2586 # wrap child in visible parentheses
2587 lpar = Leaf(token.LPAR, "(")
2588 rpar = Leaf(token.RPAR, ")")
2590 node.insert_child(index, Node(syms.atom, [lpar, child, rpar]))
2591 elif node.type == syms.import_from:
2592 # "import from" nodes store parentheses directly as part of
2594 if child.type == token.LPAR:
2595 # make parentheses invisible
2596 child.value = "" # type: ignore
2597 node.children[-1].value = "" # type: ignore
2598 elif child.type != token.STAR:
2599 # insert invisible parentheses
2600 node.insert_child(index, Leaf(token.LPAR, ""))
2601 node.append_child(Leaf(token.RPAR, ""))
2604 elif not (isinstance(child, Leaf) and is_multiline_string(child)):
2605 # wrap child in invisible parentheses
2606 lpar = Leaf(token.LPAR, "")
2607 rpar = Leaf(token.RPAR, "")
2608 index = child.remove() or 0
2609 node.insert_child(index, Node(syms.atom, [lpar, child, rpar]))
2611 check_lpar = isinstance(child, Leaf) and child.value in parens_after
2614 def normalize_fmt_off(node: Node) -> None:
2615 """Convert content between `# fmt: off`/`# fmt: on` into standalone comments."""
2618 try_again = convert_one_fmt_off_pair(node)
2621 def convert_one_fmt_off_pair(node: Node) -> bool:
2622 """Convert content of a single `# fmt: off`/`# fmt: on` into a standalone comment.
2624 Returns True if a pair was converted.
2626 for leaf in node.leaves():
2627 previous_consumed = 0
2628 for comment in list_comments(leaf.prefix, is_endmarker=False):
2629 if comment.value in FMT_OFF:
2630 # We only want standalone comments. If there's no previous leaf or
2631 # the previous leaf is indentation, it's a standalone comment in
2633 if comment.type != STANDALONE_COMMENT:
2634 prev = preceding_leaf(leaf)
2635 if prev and prev.type not in WHITESPACE:
2638 ignored_nodes = list(generate_ignored_nodes(leaf))
2639 if not ignored_nodes:
2642 first = ignored_nodes[0] # Can be a container node with the `leaf`.
2643 parent = first.parent
2644 prefix = first.prefix
2645 first.prefix = prefix[comment.consumed :]
2647 comment.value + "\n" + "".join(str(n) for n in ignored_nodes)
2649 if hidden_value.endswith("\n"):
2650 # That happens when one of the `ignored_nodes` ended with a NEWLINE
2651 # leaf (possibly followed by a DEDENT).
2652 hidden_value = hidden_value[:-1]
2654 for ignored in ignored_nodes:
2655 index = ignored.remove()
2656 if first_idx is None:
2658 assert parent is not None, "INTERNAL ERROR: fmt: on/off handling (1)"
2659 assert first_idx is not None, "INTERNAL ERROR: fmt: on/off handling (2)"
2660 parent.insert_child(
2665 prefix=prefix[:previous_consumed] + "\n" * comment.newlines,
2670 previous_consumed = comment.consumed
2675 def generate_ignored_nodes(leaf: Leaf) -> Iterator[LN]:
2676 """Starting from the container of `leaf`, generate all leaves until `# fmt: on`.
2678 Stops at the end of the block.
2680 container: Optional[LN] = container_of(leaf)
2681 while container is not None and container.type != token.ENDMARKER:
2682 for comment in list_comments(container.prefix, is_endmarker=False):
2683 if comment.value in FMT_ON:
2688 container = container.next_sibling
2691 def maybe_make_parens_invisible_in_atom(node: LN) -> bool:
2692 """If it's safe, make the parens in the atom `node` invisible, recursively."""
2694 node.type != syms.atom
2695 or is_empty_tuple(node)
2696 or is_one_tuple(node)
2698 or max_delimiter_priority_in_atom(node) >= COMMA_PRIORITY
2702 first = node.children[0]
2703 last = node.children[-1]
2704 if first.type == token.LPAR and last.type == token.RPAR:
2705 # make parentheses invisible
2706 first.value = "" # type: ignore
2707 last.value = "" # type: ignore
2708 if len(node.children) > 1:
2709 maybe_make_parens_invisible_in_atom(node.children[1])
2715 def is_empty_tuple(node: LN) -> bool:
2716 """Return True if `node` holds an empty tuple."""
2718 node.type == syms.atom
2719 and len(node.children) == 2
2720 and node.children[0].type == token.LPAR
2721 and node.children[1].type == token.RPAR
2725 def is_one_tuple(node: LN) -> bool:
2726 """Return True if `node` holds a tuple with one element, with or without parens."""
2727 if node.type == syms.atom:
2728 if len(node.children) != 3:
2731 lpar, gexp, rpar = node.children
2733 lpar.type == token.LPAR
2734 and gexp.type == syms.testlist_gexp
2735 and rpar.type == token.RPAR
2739 return len(gexp.children) == 2 and gexp.children[1].type == token.COMMA
2742 node.type in IMPLICIT_TUPLE
2743 and len(node.children) == 2
2744 and node.children[1].type == token.COMMA
2748 def is_yield(node: LN) -> bool:
2749 """Return True if `node` holds a `yield` or `yield from` expression."""
2750 if node.type == syms.yield_expr:
2753 if node.type == token.NAME and node.value == "yield": # type: ignore
2756 if node.type != syms.atom:
2759 if len(node.children) != 3:
2762 lpar, expr, rpar = node.children
2763 if lpar.type == token.LPAR and rpar.type == token.RPAR:
2764 return is_yield(expr)
2769 def is_vararg(leaf: Leaf, within: Set[NodeType]) -> bool:
2770 """Return True if `leaf` is a star or double star in a vararg or kwarg.
2772 If `within` includes VARARGS_PARENTS, this applies to function signatures.
2773 If `within` includes UNPACKING_PARENTS, it applies to right hand-side
2774 extended iterable unpacking (PEP 3132) and additional unpacking
2775 generalizations (PEP 448).
2777 if leaf.type not in STARS or not leaf.parent:
2781 if p.type == syms.star_expr:
2782 # Star expressions are also used as assignment targets in extended
2783 # iterable unpacking (PEP 3132). See what its parent is instead.
2789 return p.type in within
2792 def is_multiline_string(leaf: Leaf) -> bool:
2793 """Return True if `leaf` is a multiline string that actually spans many lines."""
2794 value = leaf.value.lstrip("furbFURB")
2795 return value[:3] in {'"""', "'''"} and "\n" in value
2798 def is_stub_suite(node: Node) -> bool:
2799 """Return True if `node` is a suite with a stub body."""
2801 len(node.children) != 4
2802 or node.children[0].type != token.NEWLINE
2803 or node.children[1].type != token.INDENT
2804 or node.children[3].type != token.DEDENT
2808 return is_stub_body(node.children[2])
2811 def is_stub_body(node: LN) -> bool:
2812 """Return True if `node` is a simple statement containing an ellipsis."""
2813 if not isinstance(node, Node) or node.type != syms.simple_stmt:
2816 if len(node.children) != 2:
2819 child = node.children[0]
2821 child.type == syms.atom
2822 and len(child.children) == 3
2823 and all(leaf == Leaf(token.DOT, ".") for leaf in child.children)
2827 def max_delimiter_priority_in_atom(node: LN) -> int:
2828 """Return maximum delimiter priority inside `node`.
2830 This is specific to atoms with contents contained in a pair of parentheses.
2831 If `node` isn't an atom or there are no enclosing parentheses, returns 0.
2833 if node.type != syms.atom:
2836 first = node.children[0]
2837 last = node.children[-1]
2838 if not (first.type == token.LPAR and last.type == token.RPAR):
2841 bt = BracketTracker()
2842 for c in node.children[1:-1]:
2843 if isinstance(c, Leaf):
2846 for leaf in c.leaves():
2849 return bt.max_delimiter_priority()
2855 def ensure_visible(leaf: Leaf) -> None:
2856 """Make sure parentheses are visible.
2858 They could be invisible as part of some statements (see
2859 :func:`normalize_invible_parens` and :func:`visit_import_from`).
2861 if leaf.type == token.LPAR:
2863 elif leaf.type == token.RPAR:
2867 def should_explode(line: Line, opening_bracket: Leaf) -> bool:
2868 """Should `line` immediately be split with `delimiter_split()` after RHS?"""
2870 opening_bracket.parent
2871 and opening_bracket.parent.type in {syms.atom, syms.import_from}
2872 and opening_bracket.value in "[{("
2877 last_leaf = line.leaves[-1]
2878 exclude = {id(last_leaf)} if last_leaf.type == token.COMMA else set()
2879 max_priority = line.bracket_tracker.max_delimiter_priority(exclude=exclude)
2880 except (IndexError, ValueError):
2883 return max_priority == COMMA_PRIORITY
2886 def is_python36(node: Node) -> bool:
2887 """Return True if the current file is using Python 3.6+ features.
2889 Currently looking for:
2891 - trailing commas after * or ** in function signatures and calls.
2893 for n in node.pre_order():
2894 if n.type == token.STRING:
2895 value_head = n.value[:2] # type: ignore
2896 if value_head in {'f"', 'F"', "f'", "F'", "rf", "fr", "RF", "FR"}:
2900 n.type in {syms.typedargslist, syms.arglist}
2902 and n.children[-1].type == token.COMMA
2904 for ch in n.children:
2905 if ch.type in STARS:
2908 if ch.type == syms.argument:
2909 for argch in ch.children:
2910 if argch.type in STARS:
2916 def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[LeafID]]:
2917 """Generate sets of closing bracket IDs that should be omitted in a RHS.
2919 Brackets can be omitted if the entire trailer up to and including
2920 a preceding closing bracket fits in one line.
2922 Yielded sets are cumulative (contain results of previous yields, too). First
2926 omit: Set[LeafID] = set()
2929 length = 4 * line.depth
2930 opening_bracket = None
2931 closing_bracket = None
2932 optional_brackets: Set[LeafID] = set()
2933 inner_brackets: Set[LeafID] = set()
2934 for index, leaf, leaf_length in enumerate_with_length(line, reversed=True):
2935 length += leaf_length
2936 if length > line_length:
2939 has_inline_comment = leaf_length > len(leaf.value) + len(leaf.prefix)
2940 if leaf.type == STANDALONE_COMMENT or has_inline_comment:
2943 optional_brackets.discard(id(leaf))
2945 if leaf is opening_bracket:
2946 opening_bracket = None
2947 elif leaf.type in CLOSING_BRACKETS:
2948 inner_brackets.add(id(leaf))
2949 elif leaf.type in CLOSING_BRACKETS:
2951 optional_brackets.add(id(opening_bracket))
2954 if index > 0 and line.leaves[index - 1].type in OPENING_BRACKETS:
2955 # Empty brackets would fail a split so treat them as "inner"
2956 # brackets (e.g. only add them to the `omit` set if another
2957 # pair of brackets was good enough.
2958 inner_brackets.add(id(leaf))
2961 opening_bracket = leaf.opening_bracket
2963 omit.add(id(closing_bracket))
2964 omit.update(inner_brackets)
2965 inner_brackets.clear()
2967 closing_bracket = leaf
2970 def get_future_imports(node: Node) -> Set[str]:
2971 """Return a set of __future__ imports in the file."""
2972 imports: Set[str] = set()
2974 def get_imports_from_children(children: List[LN]) -> Generator[str, None, None]:
2975 for child in children:
2976 if isinstance(child, Leaf):
2977 if child.type == token.NAME:
2979 elif child.type == syms.import_as_name:
2980 orig_name = child.children[0]
2981 assert isinstance(orig_name, Leaf), "Invalid syntax parsing imports"
2982 assert orig_name.type == token.NAME, "Invalid syntax parsing imports"
2983 yield orig_name.value
2984 elif child.type == syms.import_as_names:
2985 yield from get_imports_from_children(child.children)
2987 assert False, "Invalid syntax parsing imports"
2989 for child in node.children:
2990 if child.type != syms.simple_stmt:
2992 first_child = child.children[0]
2993 if isinstance(first_child, Leaf):
2994 # Continue looking if we see a docstring; otherwise stop.
2996 len(child.children) == 2
2997 and first_child.type == token.STRING
2998 and child.children[1].type == token.NEWLINE
3003 elif first_child.type == syms.import_from:
3004 module_name = first_child.children[1]
3005 if not isinstance(module_name, Leaf) or module_name.value != "__future__":
3007 imports |= set(get_imports_from_children(first_child.children[3:]))
3013 def gen_python_files_in_dir(
3016 include: Pattern[str],
3017 exclude: Pattern[str],
3019 ) -> Iterator[Path]:
3020 """Generate all files under `path` whose paths are not excluded by the
3021 `exclude` regex, but are included by the `include` regex.
3023 Symbolic links pointing outside of the `root` directory are ignored.
3025 `report` is where output about exclusions goes.
3027 assert root.is_absolute(), f"INTERNAL ERROR: `root` must be absolute but is {root}"
3028 for child in path.iterdir():
3030 normalized_path = "/" + child.resolve().relative_to(root).as_posix()
3032 if child.is_symlink():
3033 report.path_ignored(
3034 child, f"is a symbolic link that points outside {root}"
3041 normalized_path += "/"
3042 exclude_match = exclude.search(normalized_path)
3043 if exclude_match and exclude_match.group(0):
3044 report.path_ignored(child, f"matches the --exclude regular expression")
3048 yield from gen_python_files_in_dir(child, root, include, exclude, report)
3050 elif child.is_file():
3051 include_match = include.search(normalized_path)
3057 def find_project_root(srcs: Iterable[str]) -> Path:
3058 """Return a directory containing .git, .hg, or pyproject.toml.
3060 That directory can be one of the directories passed in `srcs` or their
3063 If no directory in the tree contains a marker that would specify it's the
3064 project root, the root of the file system is returned.
3067 return Path("/").resolve()
3069 common_base = min(Path(src).resolve() for src in srcs)
3070 if common_base.is_dir():
3071 # Append a fake file so `parents` below returns `common_base_dir`, too.
3072 common_base /= "fake-file"
3073 for directory in common_base.parents:
3074 if (directory / ".git").is_dir():
3077 if (directory / ".hg").is_dir():
3080 if (directory / "pyproject.toml").is_file():
3088 """Provides a reformatting counter. Can be rendered with `str(report)`."""
3092 verbose: bool = False
3093 change_count: int = 0
3095 failure_count: int = 0
3097 def done(self, src: Path, changed: Changed) -> None:
3098 """Increment the counter for successful reformatting. Write out a message."""
3099 if changed is Changed.YES:
3100 reformatted = "would reformat" if self.check else "reformatted"
3101 if self.verbose or not self.quiet:
3102 out(f"{reformatted} {src}")
3103 self.change_count += 1
3106 if changed is Changed.NO:
3107 msg = f"{src} already well formatted, good job."
3109 msg = f"{src} wasn't modified on disk since last run."
3110 out(msg, bold=False)
3111 self.same_count += 1
3113 def failed(self, src: Path, message: str) -> None:
3114 """Increment the counter for failed reformatting. Write out a message."""
3115 err(f"error: cannot format {src}: {message}")
3116 self.failure_count += 1
3118 def path_ignored(self, path: Path, message: str) -> None:
3120 out(f"{path} ignored: {message}", bold=False)
3123 def return_code(self) -> int:
3124 """Return the exit code that the app should use.
3126 This considers the current state of changed files and failures:
3127 - if there were any failures, return 123;
3128 - if any files were changed and --check is being used, return 1;
3129 - otherwise return 0.
3131 # According to http://tldp.org/LDP/abs/html/exitcodes.html starting with
3132 # 126 we have special returncodes reserved by the shell.
3133 if self.failure_count:
3136 elif self.change_count and self.check:
3141 def __str__(self) -> str:
3142 """Render a color report of the current state.
3144 Use `click.unstyle` to remove colors.
3147 reformatted = "would be reformatted"
3148 unchanged = "would be left unchanged"
3149 failed = "would fail to reformat"
3151 reformatted = "reformatted"
3152 unchanged = "left unchanged"
3153 failed = "failed to reformat"
3155 if self.change_count:
3156 s = "s" if self.change_count > 1 else ""
3158 click.style(f"{self.change_count} file{s} {reformatted}", bold=True)
3161 s = "s" if self.same_count > 1 else ""
3162 report.append(f"{self.same_count} file{s} {unchanged}")
3163 if self.failure_count:
3164 s = "s" if self.failure_count > 1 else ""
3166 click.style(f"{self.failure_count} file{s} {failed}", fg="red")
3168 return ", ".join(report) + "."
3171 def assert_equivalent(src: str, dst: str) -> None:
3172 """Raise AssertionError if `src` and `dst` aren't equivalent."""
3177 def _v(node: ast.AST, depth: int = 0) -> Iterator[str]:
3178 """Simple visitor generating strings to compare ASTs by content."""
3179 yield f"{' ' * depth}{node.__class__.__name__}("
3181 for field in sorted(node._fields):
3183 value = getattr(node, field)
3184 except AttributeError:
3187 yield f"{' ' * (depth+1)}{field}="
3189 if isinstance(value, list):
3191 if isinstance(item, ast.AST):
3192 yield from _v(item, depth + 2)
3194 elif isinstance(value, ast.AST):
3195 yield from _v(value, depth + 2)
3198 yield f"{' ' * (depth+2)}{value!r}, # {value.__class__.__name__}"
3200 yield f"{' ' * depth}) # /{node.__class__.__name__}"
3203 src_ast = ast.parse(src)
3204 except Exception as exc:
3205 major, minor = sys.version_info[:2]
3206 raise AssertionError(
3207 f"cannot use --safe with this file; failed to parse source file "
3208 f"with Python {major}.{minor}'s builtin AST. Re-run with --fast "
3209 f"or stop using deprecated Python 2 syntax. AST error message: {exc}"
3213 dst_ast = ast.parse(dst)
3214 except Exception as exc:
3215 log = dump_to_file("".join(traceback.format_tb(exc.__traceback__)), dst)
3216 raise AssertionError(
3217 f"INTERNAL ERROR: Black produced invalid code: {exc}. "
3218 f"Please report a bug on https://github.com/ambv/black/issues. "
3219 f"This invalid output might be helpful: {log}"
3222 src_ast_str = "\n".join(_v(src_ast))
3223 dst_ast_str = "\n".join(_v(dst_ast))
3224 if src_ast_str != dst_ast_str:
3225 log = dump_to_file(diff(src_ast_str, dst_ast_str, "src", "dst"))
3226 raise AssertionError(
3227 f"INTERNAL ERROR: Black produced code that is not equivalent to "
3229 f"Please report a bug on https://github.com/ambv/black/issues. "
3230 f"This diff might be helpful: {log}"
3235 src: str, dst: str, line_length: int, mode: FileMode = FileMode.AUTO_DETECT
3237 """Raise AssertionError if `dst` reformats differently the second time."""
3238 newdst = format_str(dst, line_length=line_length, mode=mode)
3241 diff(src, dst, "source", "first pass"),
3242 diff(dst, newdst, "first pass", "second pass"),
3244 raise AssertionError(
3245 f"INTERNAL ERROR: Black produced different code on the second pass "
3246 f"of the formatter. "
3247 f"Please report a bug on https://github.com/ambv/black/issues. "
3248 f"This diff might be helpful: {log}"
3252 def dump_to_file(*output: str) -> str:
3253 """Dump `output` to a temporary file. Return path to the file."""
3256 with tempfile.NamedTemporaryFile(
3257 mode="w", prefix="blk_", suffix=".log", delete=False, encoding="utf8"
3259 for lines in output:
3261 if lines and lines[-1] != "\n":
3266 def diff(a: str, b: str, a_name: str, b_name: str) -> str:
3267 """Return a unified diff string between strings `a` and `b`."""
3270 a_lines = [line + "\n" for line in a.split("\n")]
3271 b_lines = [line + "\n" for line in b.split("\n")]
3273 difflib.unified_diff(a_lines, b_lines, fromfile=a_name, tofile=b_name, n=5)
3277 def cancel(tasks: Iterable[asyncio.Task]) -> None:
3278 """asyncio signal handler that cancels all `tasks` and reports to stderr."""
3284 def shutdown(loop: BaseEventLoop) -> None:
3285 """Cancel all pending tasks on `loop`, wait for them, and close the loop."""
3287 # This part is borrowed from asyncio/runners.py in Python 3.7b2.
3288 to_cancel = [task for task in asyncio.Task.all_tasks(loop) if not task.done()]
3292 for task in to_cancel:
3294 loop.run_until_complete(
3295 asyncio.gather(*to_cancel, loop=loop, return_exceptions=True)
3298 # `concurrent.futures.Future` objects cannot be cancelled once they
3299 # are already running. There might be some when the `shutdown()` happened.
3300 # Silence their logger's spew about the event loop being closed.
3301 cf_logger = logging.getLogger("concurrent.futures")
3302 cf_logger.setLevel(logging.CRITICAL)
3306 def sub_twice(regex: Pattern[str], replacement: str, original: str) -> str:
3307 """Replace `regex` with `replacement` twice on `original`.
3309 This is used by string normalization to perform replaces on
3310 overlapping matches.
3312 return regex.sub(replacement, regex.sub(replacement, original))
3315 def re_compile_maybe_verbose(regex: str) -> Pattern[str]:
3316 """Compile a regular expression string in `regex`.
3318 If it contains newlines, use verbose mode.
3321 regex = "(?x)" + regex
3322 return re.compile(regex)
3325 def enumerate_reversed(sequence: Sequence[T]) -> Iterator[Tuple[Index, T]]:
3326 """Like `reversed(enumerate(sequence))` if that were possible."""
3327 index = len(sequence) - 1
3328 for element in reversed(sequence):
3329 yield (index, element)
3333 def enumerate_with_length(
3334 line: Line, reversed: bool = False
3335 ) -> Iterator[Tuple[Index, Leaf, int]]:
3336 """Return an enumeration of leaves with their length.
3338 Stops prematurely on multiline strings and standalone comments.
3341 Callable[[Sequence[Leaf]], Iterator[Tuple[Index, Leaf]]],
3342 enumerate_reversed if reversed else enumerate,
3344 for index, leaf in op(line.leaves):
3345 length = len(leaf.prefix) + len(leaf.value)
3346 if "\n" in leaf.value:
3347 return # Multiline strings, we can't continue.
3349 comment: Optional[Leaf]
3350 for comment in line.comments_after(leaf, index):
3351 length += len(comment.value)
3353 yield index, leaf, length
3356 def is_line_short_enough(line: Line, *, line_length: int, line_str: str = "") -> bool:
3357 """Return True if `line` is no longer than `line_length`.
3359 Uses the provided `line_str` rendering, if any, otherwise computes a new one.
3362 line_str = str(line).strip("\n")
3364 len(line_str) <= line_length
3365 and "\n" not in line_str # multiline strings
3366 and not line.contains_standalone_comments()
3370 def can_be_split(line: Line) -> bool:
3371 """Return False if the line cannot be split *for sure*.
3373 This is not an exhaustive search but a cheap heuristic that we can use to
3374 avoid some unfortunate formattings (mostly around wrapping unsplittable code
3375 in unnecessary parentheses).
3377 leaves = line.leaves
3381 if leaves[0].type == token.STRING and leaves[1].type == token.DOT:
3385 for leaf in leaves[-2::-1]:
3386 if leaf.type in OPENING_BRACKETS:
3387 if next.type not in CLOSING_BRACKETS:
3391 elif leaf.type == token.DOT:
3393 elif leaf.type == token.NAME:
3394 if not (next.type == token.DOT or next.type in OPENING_BRACKETS):
3397 elif leaf.type not in CLOSING_BRACKETS:
3400 if dot_count > 1 and call_count > 1:
3406 def can_omit_invisible_parens(line: Line, line_length: int) -> bool:
3407 """Does `line` have a shape safe to reformat without optional parens around it?
3409 Returns True for only a subset of potentially nice looking formattings but
3410 the point is to not return false positives that end up producing lines that
3413 bt = line.bracket_tracker
3414 if not bt.delimiters:
3415 # Without delimiters the optional parentheses are useless.
3418 max_priority = bt.max_delimiter_priority()
3419 if bt.delimiter_count_with_priority(max_priority) > 1:
3420 # With more than one delimiter of a kind the optional parentheses read better.
3423 if max_priority == DOT_PRIORITY:
3424 # A single stranded method call doesn't require optional parentheses.
3427 assert len(line.leaves) >= 2, "Stranded delimiter"
3429 first = line.leaves[0]
3430 second = line.leaves[1]
3431 penultimate = line.leaves[-2]
3432 last = line.leaves[-1]
3434 # With a single delimiter, omit if the expression starts or ends with
3436 if first.type in OPENING_BRACKETS and second.type not in CLOSING_BRACKETS:
3438 length = 4 * line.depth
3439 for _index, leaf, leaf_length in enumerate_with_length(line):
3440 if leaf.type in CLOSING_BRACKETS and leaf.opening_bracket is first:
3443 length += leaf_length
3444 if length > line_length:
3447 if leaf.type in OPENING_BRACKETS:
3448 # There are brackets we can further split on.
3452 # checked the entire string and line length wasn't exceeded
3453 if len(line.leaves) == _index + 1:
3456 # Note: we are not returning False here because a line might have *both*
3457 # a leading opening bracket and a trailing closing bracket. If the
3458 # opening bracket doesn't match our rule, maybe the closing will.
3461 last.type == token.RPAR
3462 or last.type == token.RBRACE
3464 # don't use indexing for omitting optional parentheses;
3466 last.type == token.RSQB
3468 and last.parent.type != syms.trailer
3471 if penultimate.type in OPENING_BRACKETS:
3472 # Empty brackets don't help.
3475 if is_multiline_string(first):
3476 # Additional wrapping of a multiline string in this situation is
3480 length = 4 * line.depth
3481 seen_other_brackets = False
3482 for _index, leaf, leaf_length in enumerate_with_length(line):
3483 length += leaf_length
3484 if leaf is last.opening_bracket:
3485 if seen_other_brackets or length <= line_length:
3488 elif leaf.type in OPENING_BRACKETS:
3489 # There are brackets we can further split on.
3490 seen_other_brackets = True
3495 def get_cache_file(line_length: int, mode: FileMode) -> Path:
3496 return CACHE_DIR / f"cache.{line_length}.{mode.value}.pickle"
3499 def read_cache(line_length: int, mode: FileMode) -> Cache:
3500 """Read the cache if it exists and is well formed.
3502 If it is not well formed, the call to write_cache later should resolve the issue.
3504 cache_file = get_cache_file(line_length, mode)
3505 if not cache_file.exists():
3508 with cache_file.open("rb") as fobj:
3510 cache: Cache = pickle.load(fobj)
3511 except pickle.UnpicklingError:
3517 def get_cache_info(path: Path) -> CacheInfo:
3518 """Return the information used to check if a file is already formatted or not."""
3520 return stat.st_mtime, stat.st_size
3523 def filter_cached(cache: Cache, sources: Iterable[Path]) -> Tuple[Set[Path], Set[Path]]:
3524 """Split an iterable of paths in `sources` into two sets.
3526 The first contains paths of files that modified on disk or are not in the
3527 cache. The other contains paths to non-modified files.
3529 todo, done = set(), set()
3532 if cache.get(src) != get_cache_info(src):
3540 cache: Cache, sources: Iterable[Path], line_length: int, mode: FileMode
3542 """Update the cache file."""
3543 cache_file = get_cache_file(line_length, mode)
3545 if not CACHE_DIR.exists():
3546 CACHE_DIR.mkdir(parents=True)
3547 new_cache = {**cache, **{src.resolve(): get_cache_info(src) for src in sources}}
3548 with cache_file.open("wb") as fobj:
3549 pickle.dump(new_cache, fobj, protocol=pickle.HIGHEST_PROTOCOL)
3554 def patch_click() -> None:
3555 """Make Click not crash.
3557 On certain misconfigured environments, Python 3 selects the ASCII encoding as the
3558 default which restricts paths that it can access during the lifetime of the
3559 application. Click refuses to work in this scenario by raising a RuntimeError.
3561 In case of Black the likelihood that non-ASCII characters are going to be used in
3562 file paths is minimal since it's Python source code. Moreover, this crash was
3563 spurious on Python 3.7 thanks to PEP 538 and PEP 540.
3566 from click import core
3567 from click import _unicodefun # type: ignore
3568 except ModuleNotFoundError:
3571 for module in (core, _unicodefun):
3572 if hasattr(module, "_verify_python3_env"):
3573 module._verify_python3_env = lambda: None
3576 if __name__ == "__main__":