]> git.madduck.net Git - etc/vim.git/blob - black.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Add support for special comments in multiline functions (#642)
[etc/vim.git] / black.py
1 import asyncio
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
7 import io
8 import itertools
9 import keyword
10 import logging
11 from multiprocessing import Manager, freeze_support
12 import os
13 from pathlib import Path
14 import pickle
15 import re
16 import signal
17 import sys
18 import tokenize
19 from typing import (
20     Any,
21     Callable,
22     Collection,
23     Dict,
24     Generator,
25     Generic,
26     Iterable,
27     Iterator,
28     List,
29     Optional,
30     Pattern,
31     Sequence,
32     Set,
33     Tuple,
34     TypeVar,
35     Union,
36     cast,
37 )
38
39 from appdirs import user_cache_dir
40 from attr import dataclass, Factory
41 import click
42 import toml
43
44 # lib2to3 fork
45 from blib2to3.pytree import Node, Leaf, type_repr
46 from blib2to3 import pygram, pytree
47 from blib2to3.pgen2 import driver, token
48 from blib2to3.pgen2.parse import ParseError
49
50
51 __version__ = "18.9b0"
52 DEFAULT_LINE_LENGTH = 88
53 DEFAULT_EXCLUDES = (
54     r"/(\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|_build|buck-out|build|dist)/"
55 )
56 DEFAULT_INCLUDES = r"\.pyi?$"
57 CACHE_DIR = Path(user_cache_dir("black", version=__version__))
58
59
60 # types
61 FileContent = str
62 Encoding = str
63 NewLine = str
64 Depth = int
65 NodeType = int
66 LeafID = int
67 Priority = int
68 Index = int
69 LN = Union[Leaf, Node]
70 SplitFunc = Callable[["Line", bool], Iterator["Line"]]
71 Timestamp = float
72 FileSize = int
73 CacheInfo = Tuple[Timestamp, FileSize]
74 Cache = Dict[Path, CacheInfo]
75 out = partial(click.secho, bold=True, err=True)
76 err = partial(click.secho, fg="red", err=True)
77
78 pygram.initialize(CACHE_DIR)
79 syms = pygram.python_symbols
80
81
82 class NothingChanged(UserWarning):
83     """Raised when reformatted code is the same as source."""
84
85
86 class CannotSplit(Exception):
87     """A readable split that fits the allotted line length is impossible."""
88
89
90 class InvalidInput(ValueError):
91     """Raised when input source code fails all parse attempts."""
92
93
94 class WriteBack(Enum):
95     NO = 0
96     YES = 1
97     DIFF = 2
98     CHECK = 3
99
100     @classmethod
101     def from_configuration(cls, *, check: bool, diff: bool) -> "WriteBack":
102         if check and not diff:
103             return cls.CHECK
104
105         return cls.DIFF if diff else cls.YES
106
107
108 class Changed(Enum):
109     NO = 0
110     CACHED = 1
111     YES = 2
112
113
114 class FileMode(Flag):
115     AUTO_DETECT = 0
116     PYTHON36 = 1
117     PYI = 2
118     NO_STRING_NORMALIZATION = 4
119     NO_NUMERIC_UNDERSCORE_NORMALIZATION = 8
120
121     @classmethod
122     def from_configuration(
123         cls,
124         *,
125         py36: bool,
126         pyi: bool,
127         skip_string_normalization: bool,
128         skip_numeric_underscore_normalization: bool,
129     ) -> "FileMode":
130         mode = cls.AUTO_DETECT
131         if py36:
132             mode |= cls.PYTHON36
133         if pyi:
134             mode |= cls.PYI
135         if skip_string_normalization:
136             mode |= cls.NO_STRING_NORMALIZATION
137         if skip_numeric_underscore_normalization:
138             mode |= cls.NO_NUMERIC_UNDERSCORE_NORMALIZATION
139         return mode
140
141
142 def read_pyproject_toml(
143     ctx: click.Context, param: click.Parameter, value: Union[str, int, bool, None]
144 ) -> Optional[str]:
145     """Inject Black configuration from "pyproject.toml" into defaults in `ctx`.
146
147     Returns the path to a successfully found and read configuration file, None
148     otherwise.
149     """
150     assert not isinstance(value, (int, bool)), "Invalid parameter type passed"
151     if not value:
152         root = find_project_root(ctx.params.get("src", ()))
153         path = root / "pyproject.toml"
154         if path.is_file():
155             value = str(path)
156         else:
157             return None
158
159     try:
160         pyproject_toml = toml.load(value)
161         config = pyproject_toml.get("tool", {}).get("black", {})
162     except (toml.TomlDecodeError, OSError) as e:
163         raise click.FileError(
164             filename=value, hint=f"Error reading configuration file: {e}"
165         )
166
167     if not config:
168         return None
169
170     if ctx.default_map is None:
171         ctx.default_map = {}
172     ctx.default_map.update(  # type: ignore  # bad types in .pyi
173         {k.replace("--", "").replace("-", "_"): v for k, v in config.items()}
174     )
175     return value
176
177
178 @click.command(context_settings=dict(help_option_names=["-h", "--help"]))
179 @click.option(
180     "-l",
181     "--line-length",
182     type=int,
183     default=DEFAULT_LINE_LENGTH,
184     help="How many characters per line to allow.",
185     show_default=True,
186 )
187 @click.option(
188     "--py36",
189     is_flag=True,
190     help=(
191         "Allow using Python 3.6-only syntax on all input files.  This will put "
192         "trailing commas in function signatures and calls also after *args and "
193         "**kwargs.  [default: per-file auto-detection]"
194     ),
195 )
196 @click.option(
197     "--pyi",
198     is_flag=True,
199     help=(
200         "Format all input files like typing stubs regardless of file extension "
201         "(useful when piping source on standard input)."
202     ),
203 )
204 @click.option(
205     "-S",
206     "--skip-string-normalization",
207     is_flag=True,
208     help="Don't normalize string quotes or prefixes.",
209 )
210 @click.option(
211     "-N",
212     "--skip-numeric-underscore-normalization",
213     is_flag=True,
214     help="Don't normalize underscores in numeric literals.",
215 )
216 @click.option(
217     "--check",
218     is_flag=True,
219     help=(
220         "Don't write the files back, just return the status.  Return code 0 "
221         "means nothing would change.  Return code 1 means some files would be "
222         "reformatted.  Return code 123 means there was an internal error."
223     ),
224 )
225 @click.option(
226     "--diff",
227     is_flag=True,
228     help="Don't write the files back, just output a diff for each file on stdout.",
229 )
230 @click.option(
231     "--fast/--safe",
232     is_flag=True,
233     help="If --fast given, skip temporary sanity checks. [default: --safe]",
234 )
235 @click.option(
236     "--include",
237     type=str,
238     default=DEFAULT_INCLUDES,
239     help=(
240         "A regular expression that matches files and directories that should be "
241         "included on recursive searches.  An empty value means all files are "
242         "included regardless of the name.  Use forward slashes for directories on "
243         "all platforms (Windows, too).  Exclusions are calculated first, inclusions "
244         "later."
245     ),
246     show_default=True,
247 )
248 @click.option(
249     "--exclude",
250     type=str,
251     default=DEFAULT_EXCLUDES,
252     help=(
253         "A regular expression that matches files and directories that should be "
254         "excluded on recursive searches.  An empty value means no paths are excluded. "
255         "Use forward slashes for directories on all platforms (Windows, too).  "
256         "Exclusions are calculated first, inclusions later."
257     ),
258     show_default=True,
259 )
260 @click.option(
261     "-q",
262     "--quiet",
263     is_flag=True,
264     help=(
265         "Don't emit non-error messages to stderr. Errors are still emitted, "
266         "silence those with 2>/dev/null."
267     ),
268 )
269 @click.option(
270     "-v",
271     "--verbose",
272     is_flag=True,
273     help=(
274         "Also emit messages to stderr about files that were not changed or were "
275         "ignored due to --exclude=."
276     ),
277 )
278 @click.version_option(version=__version__)
279 @click.argument(
280     "src",
281     nargs=-1,
282     type=click.Path(
283         exists=True, file_okay=True, dir_okay=True, readable=True, allow_dash=True
284     ),
285     is_eager=True,
286 )
287 @click.option(
288     "--config",
289     type=click.Path(
290         exists=False, file_okay=True, dir_okay=False, readable=True, allow_dash=False
291     ),
292     is_eager=True,
293     callback=read_pyproject_toml,
294     help="Read configuration from PATH.",
295 )
296 @click.pass_context
297 def main(
298     ctx: click.Context,
299     line_length: int,
300     check: bool,
301     diff: bool,
302     fast: bool,
303     pyi: bool,
304     py36: bool,
305     skip_string_normalization: bool,
306     skip_numeric_underscore_normalization: bool,
307     quiet: bool,
308     verbose: bool,
309     include: str,
310     exclude: str,
311     src: Tuple[str],
312     config: Optional[str],
313 ) -> None:
314     """The uncompromising code formatter."""
315     write_back = WriteBack.from_configuration(check=check, diff=diff)
316     mode = FileMode.from_configuration(
317         py36=py36,
318         pyi=pyi,
319         skip_string_normalization=skip_string_normalization,
320         skip_numeric_underscore_normalization=skip_numeric_underscore_normalization,
321     )
322     if config and verbose:
323         out(f"Using configuration from {config}.", bold=False, fg="blue")
324     try:
325         include_regex = re_compile_maybe_verbose(include)
326     except re.error:
327         err(f"Invalid regular expression for include given: {include!r}")
328         ctx.exit(2)
329     try:
330         exclude_regex = re_compile_maybe_verbose(exclude)
331     except re.error:
332         err(f"Invalid regular expression for exclude given: {exclude!r}")
333         ctx.exit(2)
334     report = Report(check=check, quiet=quiet, verbose=verbose)
335     root = find_project_root(src)
336     sources: Set[Path] = set()
337     for s in src:
338         p = Path(s)
339         if p.is_dir():
340             sources.update(
341                 gen_python_files_in_dir(p, root, include_regex, exclude_regex, report)
342             )
343         elif p.is_file() or s == "-":
344             # if a file was explicitly given, we don't care about its extension
345             sources.add(p)
346         else:
347             err(f"invalid path: {s}")
348     if len(sources) == 0:
349         if verbose or not quiet:
350             out("No paths given. Nothing to do 😴")
351         ctx.exit(0)
352
353     if len(sources) == 1:
354         reformat_one(
355             src=sources.pop(),
356             line_length=line_length,
357             fast=fast,
358             write_back=write_back,
359             mode=mode,
360             report=report,
361         )
362     else:
363         loop = asyncio.get_event_loop()
364         executor = ProcessPoolExecutor(max_workers=os.cpu_count())
365         try:
366             loop.run_until_complete(
367                 schedule_formatting(
368                     sources=sources,
369                     line_length=line_length,
370                     fast=fast,
371                     write_back=write_back,
372                     mode=mode,
373                     report=report,
374                     loop=loop,
375                     executor=executor,
376                 )
377             )
378         finally:
379             shutdown(loop)
380     if verbose or not quiet:
381         bang = "💥 💔 💥" if report.return_code else "✨ 🍰 ✨"
382         out(f"All done! {bang}")
383         click.secho(str(report), err=True)
384     ctx.exit(report.return_code)
385
386
387 def reformat_one(
388     src: Path,
389     line_length: int,
390     fast: bool,
391     write_back: WriteBack,
392     mode: FileMode,
393     report: "Report",
394 ) -> None:
395     """Reformat a single file under `src` without spawning child processes.
396
397     If `quiet` is True, non-error messages are not output. `line_length`,
398     `write_back`, `fast` and `pyi` options are passed to
399     :func:`format_file_in_place` or :func:`format_stdin_to_stdout`.
400     """
401     try:
402         changed = Changed.NO
403         if not src.is_file() and str(src) == "-":
404             if format_stdin_to_stdout(
405                 line_length=line_length, fast=fast, write_back=write_back, mode=mode
406             ):
407                 changed = Changed.YES
408         else:
409             cache: Cache = {}
410             if write_back != WriteBack.DIFF:
411                 cache = read_cache(line_length, mode)
412                 res_src = src.resolve()
413                 if res_src in cache and cache[res_src] == get_cache_info(res_src):
414                     changed = Changed.CACHED
415             if changed is not Changed.CACHED and format_file_in_place(
416                 src,
417                 line_length=line_length,
418                 fast=fast,
419                 write_back=write_back,
420                 mode=mode,
421             ):
422                 changed = Changed.YES
423             if (write_back is WriteBack.YES and changed is not Changed.CACHED) or (
424                 write_back is WriteBack.CHECK and changed is Changed.NO
425             ):
426                 write_cache(cache, [src], line_length, mode)
427         report.done(src, changed)
428     except Exception as exc:
429         report.failed(src, str(exc))
430
431
432 async def schedule_formatting(
433     sources: Set[Path],
434     line_length: int,
435     fast: bool,
436     write_back: WriteBack,
437     mode: FileMode,
438     report: "Report",
439     loop: BaseEventLoop,
440     executor: Executor,
441 ) -> None:
442     """Run formatting of `sources` in parallel using the provided `executor`.
443
444     (Use ProcessPoolExecutors for actual parallelism.)
445
446     `line_length`, `write_back`, `fast`, and `pyi` options are passed to
447     :func:`format_file_in_place`.
448     """
449     cache: Cache = {}
450     if write_back != WriteBack.DIFF:
451         cache = read_cache(line_length, mode)
452         sources, cached = filter_cached(cache, sources)
453         for src in sorted(cached):
454             report.done(src, Changed.CACHED)
455     if not sources:
456         return
457
458     cancelled = []
459     sources_to_cache = []
460     lock = None
461     if write_back == WriteBack.DIFF:
462         # For diff output, we need locks to ensure we don't interleave output
463         # from different processes.
464         manager = Manager()
465         lock = manager.Lock()
466     tasks = {
467         loop.run_in_executor(
468             executor,
469             format_file_in_place,
470             src,
471             line_length,
472             fast,
473             write_back,
474             mode,
475             lock,
476         ): src
477         for src in sorted(sources)
478     }
479     pending: Iterable[asyncio.Task] = tasks.keys()
480     try:
481         loop.add_signal_handler(signal.SIGINT, cancel, pending)
482         loop.add_signal_handler(signal.SIGTERM, cancel, pending)
483     except NotImplementedError:
484         # There are no good alternatives for these on Windows.
485         pass
486     while pending:
487         done, _ = await asyncio.wait(pending, return_when=asyncio.FIRST_COMPLETED)
488         for task in done:
489             src = tasks.pop(task)
490             if task.cancelled():
491                 cancelled.append(task)
492             elif task.exception():
493                 report.failed(src, str(task.exception()))
494             else:
495                 changed = Changed.YES if task.result() else Changed.NO
496                 # If the file was written back or was successfully checked as
497                 # well-formatted, store this information in the cache.
498                 if write_back is WriteBack.YES or (
499                     write_back is WriteBack.CHECK and changed is Changed.NO
500                 ):
501                     sources_to_cache.append(src)
502                 report.done(src, changed)
503     if cancelled:
504         await asyncio.gather(*cancelled, loop=loop, return_exceptions=True)
505     if sources_to_cache:
506         write_cache(cache, sources_to_cache, line_length, mode)
507
508
509 def format_file_in_place(
510     src: Path,
511     line_length: int,
512     fast: bool,
513     write_back: WriteBack = WriteBack.NO,
514     mode: FileMode = FileMode.AUTO_DETECT,
515     lock: Any = None,  # multiprocessing.Manager().Lock() is some crazy proxy
516 ) -> bool:
517     """Format file under `src` path. Return True if changed.
518
519     If `write_back` is DIFF, write a diff to stdout. If it is YES, write reformatted
520     code to the file.
521     `line_length` and `fast` options are passed to :func:`format_file_contents`.
522     """
523     if src.suffix == ".pyi":
524         mode |= FileMode.PYI
525
526     then = datetime.utcfromtimestamp(src.stat().st_mtime)
527     with open(src, "rb") as buf:
528         src_contents, encoding, newline = decode_bytes(buf.read())
529     try:
530         dst_contents = format_file_contents(
531             src_contents, line_length=line_length, fast=fast, mode=mode
532         )
533     except NothingChanged:
534         return False
535
536     if write_back == write_back.YES:
537         with open(src, "w", encoding=encoding, newline=newline) as f:
538             f.write(dst_contents)
539     elif write_back == write_back.DIFF:
540         now = datetime.utcnow()
541         src_name = f"{src}\t{then} +0000"
542         dst_name = f"{src}\t{now} +0000"
543         diff_contents = diff(src_contents, dst_contents, src_name, dst_name)
544         if lock:
545             lock.acquire()
546         try:
547             f = io.TextIOWrapper(
548                 sys.stdout.buffer,
549                 encoding=encoding,
550                 newline=newline,
551                 write_through=True,
552             )
553             f.write(diff_contents)
554             f.detach()
555         finally:
556             if lock:
557                 lock.release()
558     return True
559
560
561 def format_stdin_to_stdout(
562     line_length: int,
563     fast: bool,
564     write_back: WriteBack = WriteBack.NO,
565     mode: FileMode = FileMode.AUTO_DETECT,
566 ) -> bool:
567     """Format file on stdin. Return True if changed.
568
569     If `write_back` is YES, write reformatted code back to stdout. If it is DIFF,
570     write a diff to stdout.
571     `line_length`, `fast`, `is_pyi`, and `force_py36` arguments are passed to
572     :func:`format_file_contents`.
573     """
574     then = datetime.utcnow()
575     src, encoding, newline = decode_bytes(sys.stdin.buffer.read())
576     dst = src
577     try:
578         dst = format_file_contents(src, line_length=line_length, fast=fast, mode=mode)
579         return True
580
581     except NothingChanged:
582         return False
583
584     finally:
585         f = io.TextIOWrapper(
586             sys.stdout.buffer, encoding=encoding, newline=newline, write_through=True
587         )
588         if write_back == WriteBack.YES:
589             f.write(dst)
590         elif write_back == WriteBack.DIFF:
591             now = datetime.utcnow()
592             src_name = f"STDIN\t{then} +0000"
593             dst_name = f"STDOUT\t{now} +0000"
594             f.write(diff(src, dst, src_name, dst_name))
595         f.detach()
596
597
598 def format_file_contents(
599     src_contents: str,
600     *,
601     line_length: int,
602     fast: bool,
603     mode: FileMode = FileMode.AUTO_DETECT,
604 ) -> FileContent:
605     """Reformat contents a file and return new contents.
606
607     If `fast` is False, additionally confirm that the reformatted code is
608     valid by calling :func:`assert_equivalent` and :func:`assert_stable` on it.
609     `line_length` is passed to :func:`format_str`.
610     """
611     if src_contents.strip() == "":
612         raise NothingChanged
613
614     dst_contents = format_str(src_contents, line_length=line_length, mode=mode)
615     if src_contents == dst_contents:
616         raise NothingChanged
617
618     if not fast:
619         assert_equivalent(src_contents, dst_contents)
620         assert_stable(src_contents, dst_contents, line_length=line_length, mode=mode)
621     return dst_contents
622
623
624 def format_str(
625     src_contents: str, line_length: int, *, mode: FileMode = FileMode.AUTO_DETECT
626 ) -> FileContent:
627     """Reformat a string and return new contents.
628
629     `line_length` determines how many characters per line are allowed.
630     """
631     src_node = lib2to3_parse(src_contents.lstrip())
632     dst_contents = ""
633     future_imports = get_future_imports(src_node)
634     is_pyi = bool(mode & FileMode.PYI)
635     py36 = bool(mode & FileMode.PYTHON36) or is_python36(src_node)
636     normalize_strings = not bool(mode & FileMode.NO_STRING_NORMALIZATION)
637     normalize_fmt_off(src_node)
638     lines = LineGenerator(
639         remove_u_prefix=py36 or "unicode_literals" in future_imports,
640         is_pyi=is_pyi,
641         normalize_strings=normalize_strings,
642         allow_underscores=py36
643         and not bool(mode & FileMode.NO_NUMERIC_UNDERSCORE_NORMALIZATION),
644     )
645     elt = EmptyLineTracker(is_pyi=is_pyi)
646     empty_line = Line()
647     after = 0
648     for current_line in lines.visit(src_node):
649         for _ in range(after):
650             dst_contents += str(empty_line)
651         before, after = elt.maybe_empty_lines(current_line)
652         for _ in range(before):
653             dst_contents += str(empty_line)
654         for line in split_line(current_line, line_length=line_length, py36=py36):
655             dst_contents += str(line)
656     return dst_contents
657
658
659 def decode_bytes(src: bytes) -> Tuple[FileContent, Encoding, NewLine]:
660     """Return a tuple of (decoded_contents, encoding, newline).
661
662     `newline` is either CRLF or LF but `decoded_contents` is decoded with
663     universal newlines (i.e. only contains LF).
664     """
665     srcbuf = io.BytesIO(src)
666     encoding, lines = tokenize.detect_encoding(srcbuf.readline)
667     if not lines:
668         return "", encoding, "\n"
669
670     newline = "\r\n" if b"\r\n" == lines[0][-2:] else "\n"
671     srcbuf.seek(0)
672     with io.TextIOWrapper(srcbuf, encoding) as tiow:
673         return tiow.read(), encoding, newline
674
675
676 GRAMMARS = [
677     pygram.python_grammar_no_print_statement_no_exec_statement,
678     pygram.python_grammar_no_print_statement,
679     pygram.python_grammar,
680 ]
681
682
683 def lib2to3_parse(src_txt: str) -> Node:
684     """Given a string with source, return the lib2to3 Node."""
685     if src_txt[-1:] != "\n":
686         src_txt += "\n"
687     for grammar in GRAMMARS:
688         drv = driver.Driver(grammar, pytree.convert)
689         try:
690             result = drv.parse_string(src_txt, True)
691             break
692
693         except ParseError as pe:
694             lineno, column = pe.context[1]
695             lines = src_txt.splitlines()
696             try:
697                 faulty_line = lines[lineno - 1]
698             except IndexError:
699                 faulty_line = "<line number missing in source>"
700             exc = InvalidInput(f"Cannot parse: {lineno}:{column}: {faulty_line}")
701     else:
702         raise exc from None
703
704     if isinstance(result, Leaf):
705         result = Node(syms.file_input, [result])
706     return result
707
708
709 def lib2to3_unparse(node: Node) -> str:
710     """Given a lib2to3 node, return its string representation."""
711     code = str(node)
712     return code
713
714
715 T = TypeVar("T")
716
717
718 class Visitor(Generic[T]):
719     """Basic lib2to3 visitor that yields things of type `T` on `visit()`."""
720
721     def visit(self, node: LN) -> Iterator[T]:
722         """Main method to visit `node` and its children.
723
724         It tries to find a `visit_*()` method for the given `node.type`, like
725         `visit_simple_stmt` for Node objects or `visit_INDENT` for Leaf objects.
726         If no dedicated `visit_*()` method is found, chooses `visit_default()`
727         instead.
728
729         Then yields objects of type `T` from the selected visitor.
730         """
731         if node.type < 256:
732             name = token.tok_name[node.type]
733         else:
734             name = type_repr(node.type)
735         yield from getattr(self, f"visit_{name}", self.visit_default)(node)
736
737     def visit_default(self, node: LN) -> Iterator[T]:
738         """Default `visit_*()` implementation. Recurses to children of `node`."""
739         if isinstance(node, Node):
740             for child in node.children:
741                 yield from self.visit(child)
742
743
744 @dataclass
745 class DebugVisitor(Visitor[T]):
746     tree_depth: int = 0
747
748     def visit_default(self, node: LN) -> Iterator[T]:
749         indent = " " * (2 * self.tree_depth)
750         if isinstance(node, Node):
751             _type = type_repr(node.type)
752             out(f"{indent}{_type}", fg="yellow")
753             self.tree_depth += 1
754             for child in node.children:
755                 yield from self.visit(child)
756
757             self.tree_depth -= 1
758             out(f"{indent}/{_type}", fg="yellow", bold=False)
759         else:
760             _type = token.tok_name.get(node.type, str(node.type))
761             out(f"{indent}{_type}", fg="blue", nl=False)
762             if node.prefix:
763                 # We don't have to handle prefixes for `Node` objects since
764                 # that delegates to the first child anyway.
765                 out(f" {node.prefix!r}", fg="green", bold=False, nl=False)
766             out(f" {node.value!r}", fg="blue", bold=False)
767
768     @classmethod
769     def show(cls, code: Union[str, Leaf, Node]) -> None:
770         """Pretty-print the lib2to3 AST of a given string of `code`.
771
772         Convenience method for debugging.
773         """
774         v: DebugVisitor[None] = DebugVisitor()
775         if isinstance(code, str):
776             code = lib2to3_parse(code)
777         list(v.visit(code))
778
779
780 KEYWORDS = set(keyword.kwlist)
781 WHITESPACE = {token.DEDENT, token.INDENT, token.NEWLINE}
782 FLOW_CONTROL = {"return", "raise", "break", "continue"}
783 STATEMENT = {
784     syms.if_stmt,
785     syms.while_stmt,
786     syms.for_stmt,
787     syms.try_stmt,
788     syms.except_clause,
789     syms.with_stmt,
790     syms.funcdef,
791     syms.classdef,
792 }
793 STANDALONE_COMMENT = 153
794 token.tok_name[STANDALONE_COMMENT] = "STANDALONE_COMMENT"
795 LOGIC_OPERATORS = {"and", "or"}
796 COMPARATORS = {
797     token.LESS,
798     token.GREATER,
799     token.EQEQUAL,
800     token.NOTEQUAL,
801     token.LESSEQUAL,
802     token.GREATEREQUAL,
803 }
804 MATH_OPERATORS = {
805     token.VBAR,
806     token.CIRCUMFLEX,
807     token.AMPER,
808     token.LEFTSHIFT,
809     token.RIGHTSHIFT,
810     token.PLUS,
811     token.MINUS,
812     token.STAR,
813     token.SLASH,
814     token.DOUBLESLASH,
815     token.PERCENT,
816     token.AT,
817     token.TILDE,
818     token.DOUBLESTAR,
819 }
820 STARS = {token.STAR, token.DOUBLESTAR}
821 VARARGS_PARENTS = {
822     syms.arglist,
823     syms.argument,  # double star in arglist
824     syms.trailer,  # single argument to call
825     syms.typedargslist,
826     syms.varargslist,  # lambdas
827 }
828 UNPACKING_PARENTS = {
829     syms.atom,  # single element of a list or set literal
830     syms.dictsetmaker,
831     syms.listmaker,
832     syms.testlist_gexp,
833     syms.testlist_star_expr,
834 }
835 TEST_DESCENDANTS = {
836     syms.test,
837     syms.lambdef,
838     syms.or_test,
839     syms.and_test,
840     syms.not_test,
841     syms.comparison,
842     syms.star_expr,
843     syms.expr,
844     syms.xor_expr,
845     syms.and_expr,
846     syms.shift_expr,
847     syms.arith_expr,
848     syms.trailer,
849     syms.term,
850     syms.power,
851 }
852 ASSIGNMENTS = {
853     "=",
854     "+=",
855     "-=",
856     "*=",
857     "@=",
858     "/=",
859     "%=",
860     "&=",
861     "|=",
862     "^=",
863     "<<=",
864     ">>=",
865     "**=",
866     "//=",
867 }
868 COMPREHENSION_PRIORITY = 20
869 COMMA_PRIORITY = 18
870 TERNARY_PRIORITY = 16
871 LOGIC_PRIORITY = 14
872 STRING_PRIORITY = 12
873 COMPARATOR_PRIORITY = 10
874 MATH_PRIORITIES = {
875     token.VBAR: 9,
876     token.CIRCUMFLEX: 8,
877     token.AMPER: 7,
878     token.LEFTSHIFT: 6,
879     token.RIGHTSHIFT: 6,
880     token.PLUS: 5,
881     token.MINUS: 5,
882     token.STAR: 4,
883     token.SLASH: 4,
884     token.DOUBLESLASH: 4,
885     token.PERCENT: 4,
886     token.AT: 4,
887     token.TILDE: 3,
888     token.DOUBLESTAR: 2,
889 }
890 DOT_PRIORITY = 1
891
892
893 @dataclass
894 class BracketTracker:
895     """Keeps track of brackets on a line."""
896
897     depth: int = 0
898     bracket_match: Dict[Tuple[Depth, NodeType], Leaf] = Factory(dict)
899     delimiters: Dict[LeafID, Priority] = Factory(dict)
900     previous: Optional[Leaf] = None
901     _for_loop_depths: List[int] = Factory(list)
902     _lambda_argument_depths: List[int] = Factory(list)
903
904     def mark(self, leaf: Leaf) -> None:
905         """Mark `leaf` with bracket-related metadata. Keep track of delimiters.
906
907         All leaves receive an int `bracket_depth` field that stores how deep
908         within brackets a given leaf is. 0 means there are no enclosing brackets
909         that started on this line.
910
911         If a leaf is itself a closing bracket, it receives an `opening_bracket`
912         field that it forms a pair with. This is a one-directional link to
913         avoid reference cycles.
914
915         If a leaf is a delimiter (a token on which Black can split the line if
916         needed) and it's on depth 0, its `id()` is stored in the tracker's
917         `delimiters` field.
918         """
919         if leaf.type == token.COMMENT:
920             return
921
922         self.maybe_decrement_after_for_loop_variable(leaf)
923         self.maybe_decrement_after_lambda_arguments(leaf)
924         if leaf.type in CLOSING_BRACKETS:
925             self.depth -= 1
926             opening_bracket = self.bracket_match.pop((self.depth, leaf.type))
927             leaf.opening_bracket = opening_bracket
928         leaf.bracket_depth = self.depth
929         if self.depth == 0:
930             delim = is_split_before_delimiter(leaf, self.previous)
931             if delim and self.previous is not None:
932                 self.delimiters[id(self.previous)] = delim
933             else:
934                 delim = is_split_after_delimiter(leaf, self.previous)
935                 if delim:
936                     self.delimiters[id(leaf)] = delim
937         if leaf.type in OPENING_BRACKETS:
938             self.bracket_match[self.depth, BRACKET[leaf.type]] = leaf
939             self.depth += 1
940         self.previous = leaf
941         self.maybe_increment_lambda_arguments(leaf)
942         self.maybe_increment_for_loop_variable(leaf)
943
944     def any_open_brackets(self) -> bool:
945         """Return True if there is an yet unmatched open bracket on the line."""
946         return bool(self.bracket_match)
947
948     def max_delimiter_priority(self, exclude: Iterable[LeafID] = ()) -> int:
949         """Return the highest priority of a delimiter found on the line.
950
951         Values are consistent with what `is_split_*_delimiter()` return.
952         Raises ValueError on no delimiters.
953         """
954         return max(v for k, v in self.delimiters.items() if k not in exclude)
955
956     def delimiter_count_with_priority(self, priority: int = 0) -> int:
957         """Return the number of delimiters with the given `priority`.
958
959         If no `priority` is passed, defaults to max priority on the line.
960         """
961         if not self.delimiters:
962             return 0
963
964         priority = priority or self.max_delimiter_priority()
965         return sum(1 for p in self.delimiters.values() if p == priority)
966
967     def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
968         """In a for loop, or comprehension, the variables are often unpacks.
969
970         To avoid splitting on the comma in this situation, increase the depth of
971         tokens between `for` and `in`.
972         """
973         if leaf.type == token.NAME and leaf.value == "for":
974             self.depth += 1
975             self._for_loop_depths.append(self.depth)
976             return True
977
978         return False
979
980     def maybe_decrement_after_for_loop_variable(self, leaf: Leaf) -> bool:
981         """See `maybe_increment_for_loop_variable` above for explanation."""
982         if (
983             self._for_loop_depths
984             and self._for_loop_depths[-1] == self.depth
985             and leaf.type == token.NAME
986             and leaf.value == "in"
987         ):
988             self.depth -= 1
989             self._for_loop_depths.pop()
990             return True
991
992         return False
993
994     def maybe_increment_lambda_arguments(self, leaf: Leaf) -> bool:
995         """In a lambda expression, there might be more than one argument.
996
997         To avoid splitting on the comma in this situation, increase the depth of
998         tokens between `lambda` and `:`.
999         """
1000         if leaf.type == token.NAME and leaf.value == "lambda":
1001             self.depth += 1
1002             self._lambda_argument_depths.append(self.depth)
1003             return True
1004
1005         return False
1006
1007     def maybe_decrement_after_lambda_arguments(self, leaf: Leaf) -> bool:
1008         """See `maybe_increment_lambda_arguments` above for explanation."""
1009         if (
1010             self._lambda_argument_depths
1011             and self._lambda_argument_depths[-1] == self.depth
1012             and leaf.type == token.COLON
1013         ):
1014             self.depth -= 1
1015             self._lambda_argument_depths.pop()
1016             return True
1017
1018         return False
1019
1020     def get_open_lsqb(self) -> Optional[Leaf]:
1021         """Return the most recent opening square bracket (if any)."""
1022         return self.bracket_match.get((self.depth - 1, token.RSQB))
1023
1024
1025 @dataclass
1026 class Line:
1027     """Holds leaves and comments. Can be printed with `str(line)`."""
1028
1029     depth: int = 0
1030     leaves: List[Leaf] = Factory(list)
1031     # The LeafID keys of comments must remain ordered by the corresponding leaf's index
1032     # in leaves
1033     comments: Dict[LeafID, List[Leaf]] = Factory(dict)
1034     bracket_tracker: BracketTracker = Factory(BracketTracker)
1035     inside_brackets: bool = False
1036     should_explode: bool = False
1037
1038     def append(self, leaf: Leaf, preformatted: bool = False) -> None:
1039         """Add a new `leaf` to the end of the line.
1040
1041         Unless `preformatted` is True, the `leaf` will receive a new consistent
1042         whitespace prefix and metadata applied by :class:`BracketTracker`.
1043         Trailing commas are maybe removed, unpacked for loop variables are
1044         demoted from being delimiters.
1045
1046         Inline comments are put aside.
1047         """
1048         has_value = leaf.type in BRACKETS or bool(leaf.value.strip())
1049         if not has_value:
1050             return
1051
1052         if token.COLON == leaf.type and self.is_class_paren_empty:
1053             del self.leaves[-2:]
1054         if self.leaves and not preformatted:
1055             # Note: at this point leaf.prefix should be empty except for
1056             # imports, for which we only preserve newlines.
1057             leaf.prefix += whitespace(
1058                 leaf, complex_subscript=self.is_complex_subscript(leaf)
1059             )
1060         if self.inside_brackets or not preformatted:
1061             self.bracket_tracker.mark(leaf)
1062             self.maybe_remove_trailing_comma(leaf)
1063         if not self.append_comment(leaf):
1064             self.leaves.append(leaf)
1065
1066     def append_safe(self, leaf: Leaf, preformatted: bool = False) -> None:
1067         """Like :func:`append()` but disallow invalid standalone comment structure.
1068
1069         Raises ValueError when any `leaf` is appended after a standalone comment
1070         or when a standalone comment is not the first leaf on the line.
1071         """
1072         if self.bracket_tracker.depth == 0:
1073             if self.is_comment:
1074                 raise ValueError("cannot append to standalone comments")
1075
1076             if self.leaves and leaf.type == STANDALONE_COMMENT:
1077                 raise ValueError(
1078                     "cannot append standalone comments to a populated line"
1079                 )
1080
1081         self.append(leaf, preformatted=preformatted)
1082
1083     @property
1084     def is_comment(self) -> bool:
1085         """Is this line a standalone comment?"""
1086         return len(self.leaves) == 1 and self.leaves[0].type == STANDALONE_COMMENT
1087
1088     @property
1089     def is_decorator(self) -> bool:
1090         """Is this line a decorator?"""
1091         return bool(self) and self.leaves[0].type == token.AT
1092
1093     @property
1094     def is_import(self) -> bool:
1095         """Is this an import line?"""
1096         return bool(self) and is_import(self.leaves[0])
1097
1098     @property
1099     def is_class(self) -> bool:
1100         """Is this line a class definition?"""
1101         return (
1102             bool(self)
1103             and self.leaves[0].type == token.NAME
1104             and self.leaves[0].value == "class"
1105         )
1106
1107     @property
1108     def is_stub_class(self) -> bool:
1109         """Is this line a class definition with a body consisting only of "..."?"""
1110         return self.is_class and self.leaves[-3:] == [
1111             Leaf(token.DOT, ".") for _ in range(3)
1112         ]
1113
1114     @property
1115     def is_def(self) -> bool:
1116         """Is this a function definition? (Also returns True for async defs.)"""
1117         try:
1118             first_leaf = self.leaves[0]
1119         except IndexError:
1120             return False
1121
1122         try:
1123             second_leaf: Optional[Leaf] = self.leaves[1]
1124         except IndexError:
1125             second_leaf = None
1126         return (first_leaf.type == token.NAME and first_leaf.value == "def") or (
1127             first_leaf.type == token.ASYNC
1128             and second_leaf is not None
1129             and second_leaf.type == token.NAME
1130             and second_leaf.value == "def"
1131         )
1132
1133     @property
1134     def is_class_paren_empty(self) -> bool:
1135         """Is this a class with no base classes but using parentheses?
1136
1137         Those are unnecessary and should be removed.
1138         """
1139         return (
1140             bool(self)
1141             and len(self.leaves) == 4
1142             and self.is_class
1143             and self.leaves[2].type == token.LPAR
1144             and self.leaves[2].value == "("
1145             and self.leaves[3].type == token.RPAR
1146             and self.leaves[3].value == ")"
1147         )
1148
1149     @property
1150     def is_triple_quoted_string(self) -> bool:
1151         """Is the line a triple quoted string?"""
1152         return (
1153             bool(self)
1154             and self.leaves[0].type == token.STRING
1155             and self.leaves[0].value.startswith(('"""', "'''"))
1156         )
1157
1158     def contains_standalone_comments(self, depth_limit: int = sys.maxsize) -> bool:
1159         """If so, needs to be split before emitting."""
1160         for leaf in self.leaves:
1161             if leaf.type == STANDALONE_COMMENT:
1162                 if leaf.bracket_depth <= depth_limit:
1163                     return True
1164
1165         return False
1166
1167     def contains_multiline_strings(self) -> bool:
1168         for leaf in self.leaves:
1169             if is_multiline_string(leaf):
1170                 return True
1171
1172         return False
1173
1174     def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
1175         """Remove trailing comma if there is one and it's safe."""
1176         if not (
1177             self.leaves
1178             and self.leaves[-1].type == token.COMMA
1179             and closing.type in CLOSING_BRACKETS
1180         ):
1181             return False
1182
1183         if closing.type == token.RBRACE:
1184             self.remove_trailing_comma()
1185             return True
1186
1187         if closing.type == token.RSQB:
1188             comma = self.leaves[-1]
1189             if comma.parent and comma.parent.type == syms.listmaker:
1190                 self.remove_trailing_comma()
1191                 return True
1192
1193         # For parens let's check if it's safe to remove the comma.
1194         # Imports are always safe.
1195         if self.is_import:
1196             self.remove_trailing_comma()
1197             return True
1198
1199         # Otherwise, if the trailing one is the only one, we might mistakenly
1200         # change a tuple into a different type by removing the comma.
1201         depth = closing.bracket_depth + 1
1202         commas = 0
1203         opening = closing.opening_bracket
1204         for _opening_index, leaf in enumerate(self.leaves):
1205             if leaf is opening:
1206                 break
1207
1208         else:
1209             return False
1210
1211         for leaf in self.leaves[_opening_index + 1 :]:
1212             if leaf is closing:
1213                 break
1214
1215             bracket_depth = leaf.bracket_depth
1216             if bracket_depth == depth and leaf.type == token.COMMA:
1217                 commas += 1
1218                 if leaf.parent and leaf.parent.type == syms.arglist:
1219                     commas += 1
1220                     break
1221
1222         if commas > 1:
1223             self.remove_trailing_comma()
1224             return True
1225
1226         return False
1227
1228     def append_comment(self, comment: Leaf) -> bool:
1229         """Add an inline or standalone comment to the line."""
1230         if (
1231             comment.type == STANDALONE_COMMENT
1232             and self.bracket_tracker.any_open_brackets()
1233         ):
1234             comment.prefix = ""
1235             return False
1236
1237         if comment.type != token.COMMENT:
1238             return False
1239
1240         if not self.leaves:
1241             comment.type = STANDALONE_COMMENT
1242             comment.prefix = ""
1243             return False
1244
1245         else:
1246             leaf_id = id(self.leaves[-1])
1247             if leaf_id not in self.comments:
1248                 self.comments[leaf_id] = [comment]
1249             else:
1250                 self.comments[leaf_id].append(comment)
1251             return True
1252
1253     def comments_after(self, leaf: Leaf) -> List[Leaf]:
1254         """Generate comments that should appear directly after `leaf`."""
1255         return self.comments.get(id(leaf), [])
1256
1257     def remove_trailing_comma(self) -> None:
1258         """Remove the trailing comma and moves the comments attached to it."""
1259         # Remember, the LeafID keys of self.comments are ordered by the
1260         # corresponding leaf's index in self.leaves
1261         # If id(self.leaves[-2]) is in self.comments, the order doesn't change.
1262         # Otherwise, we insert it into self.comments, and it becomes the last entry.
1263         # However, since we delete id(self.leaves[-1]) from self.comments, the invariant
1264         # is maintained
1265         self.comments.setdefault(id(self.leaves[-2]), []).extend(
1266             self.comments.get(id(self.leaves[-1]), [])
1267         )
1268         self.comments.pop(id(self.leaves[-1]), None)
1269         self.leaves.pop()
1270
1271     def is_complex_subscript(self, leaf: Leaf) -> bool:
1272         """Return True iff `leaf` is part of a slice with non-trivial exprs."""
1273         open_lsqb = self.bracket_tracker.get_open_lsqb()
1274         if open_lsqb is None:
1275             return False
1276
1277         subscript_start = open_lsqb.next_sibling
1278
1279         if isinstance(subscript_start, Node):
1280             if subscript_start.type == syms.listmaker:
1281                 return False
1282
1283             if subscript_start.type == syms.subscriptlist:
1284                 subscript_start = child_towards(subscript_start, leaf)
1285         return subscript_start is not None and any(
1286             n.type in TEST_DESCENDANTS for n in subscript_start.pre_order()
1287         )
1288
1289     def __str__(self) -> str:
1290         """Render the line."""
1291         if not self:
1292             return "\n"
1293
1294         indent = "    " * self.depth
1295         leaves = iter(self.leaves)
1296         first = next(leaves)
1297         res = f"{first.prefix}{indent}{first.value}"
1298         for leaf in leaves:
1299             res += str(leaf)
1300         for comment in itertools.chain.from_iterable(self.comments.values()):
1301             res += str(comment)
1302         return res + "\n"
1303
1304     def __bool__(self) -> bool:
1305         """Return True if the line has leaves or comments."""
1306         return bool(self.leaves or self.comments)
1307
1308
1309 @dataclass
1310 class EmptyLineTracker:
1311     """Provides a stateful method that returns the number of potential extra
1312     empty lines needed before and after the currently processed line.
1313
1314     Note: this tracker works on lines that haven't been split yet.  It assumes
1315     the prefix of the first leaf consists of optional newlines.  Those newlines
1316     are consumed by `maybe_empty_lines()` and included in the computation.
1317     """
1318
1319     is_pyi: bool = False
1320     previous_line: Optional[Line] = None
1321     previous_after: int = 0
1322     previous_defs: List[int] = Factory(list)
1323
1324     def maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
1325         """Return the number of extra empty lines before and after the `current_line`.
1326
1327         This is for separating `def`, `async def` and `class` with extra empty
1328         lines (two on module-level).
1329         """
1330         before, after = self._maybe_empty_lines(current_line)
1331         before -= self.previous_after
1332         self.previous_after = after
1333         self.previous_line = current_line
1334         return before, after
1335
1336     def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
1337         max_allowed = 1
1338         if current_line.depth == 0:
1339             max_allowed = 1 if self.is_pyi else 2
1340         if current_line.leaves:
1341             # Consume the first leaf's extra newlines.
1342             first_leaf = current_line.leaves[0]
1343             before = first_leaf.prefix.count("\n")
1344             before = min(before, max_allowed)
1345             first_leaf.prefix = ""
1346         else:
1347             before = 0
1348         depth = current_line.depth
1349         while self.previous_defs and self.previous_defs[-1] >= depth:
1350             self.previous_defs.pop()
1351             if self.is_pyi:
1352                 before = 0 if depth else 1
1353             else:
1354                 before = 1 if depth else 2
1355         if current_line.is_decorator or current_line.is_def or current_line.is_class:
1356             return self._maybe_empty_lines_for_class_or_def(current_line, before)
1357
1358         if (
1359             self.previous_line
1360             and self.previous_line.is_import
1361             and not current_line.is_import
1362             and depth == self.previous_line.depth
1363         ):
1364             return (before or 1), 0
1365
1366         if (
1367             self.previous_line
1368             and self.previous_line.is_class
1369             and current_line.is_triple_quoted_string
1370         ):
1371             return before, 1
1372
1373         return before, 0
1374
1375     def _maybe_empty_lines_for_class_or_def(
1376         self, current_line: Line, before: int
1377     ) -> Tuple[int, int]:
1378         if not current_line.is_decorator:
1379             self.previous_defs.append(current_line.depth)
1380         if self.previous_line is None:
1381             # Don't insert empty lines before the first line in the file.
1382             return 0, 0
1383
1384         if self.previous_line.is_decorator:
1385             return 0, 0
1386
1387         if self.previous_line.depth < current_line.depth and (
1388             self.previous_line.is_class or self.previous_line.is_def
1389         ):
1390             return 0, 0
1391
1392         if (
1393             self.previous_line.is_comment
1394             and self.previous_line.depth == current_line.depth
1395             and before == 0
1396         ):
1397             return 0, 0
1398
1399         if self.is_pyi:
1400             if self.previous_line.depth > current_line.depth:
1401                 newlines = 1
1402             elif current_line.is_class or self.previous_line.is_class:
1403                 if current_line.is_stub_class and self.previous_line.is_stub_class:
1404                     # No blank line between classes with an empty body
1405                     newlines = 0
1406                 else:
1407                     newlines = 1
1408             elif current_line.is_def and not self.previous_line.is_def:
1409                 # Blank line between a block of functions and a block of non-functions
1410                 newlines = 1
1411             else:
1412                 newlines = 0
1413         else:
1414             newlines = 2
1415         if current_line.depth and newlines:
1416             newlines -= 1
1417         return newlines, 0
1418
1419
1420 @dataclass
1421 class LineGenerator(Visitor[Line]):
1422     """Generates reformatted Line objects.  Empty lines are not emitted.
1423
1424     Note: destroys the tree it's visiting by mutating prefixes of its leaves
1425     in ways that will no longer stringify to valid Python code on the tree.
1426     """
1427
1428     is_pyi: bool = False
1429     normalize_strings: bool = True
1430     current_line: Line = Factory(Line)
1431     remove_u_prefix: bool = False
1432     allow_underscores: bool = False
1433
1434     def line(self, indent: int = 0) -> Iterator[Line]:
1435         """Generate a line.
1436
1437         If the line is empty, only emit if it makes sense.
1438         If the line is too long, split it first and then generate.
1439
1440         If any lines were generated, set up a new current_line.
1441         """
1442         if not self.current_line:
1443             self.current_line.depth += indent
1444             return  # Line is empty, don't emit. Creating a new one unnecessary.
1445
1446         complete_line = self.current_line
1447         self.current_line = Line(depth=complete_line.depth + indent)
1448         yield complete_line
1449
1450     def visit_default(self, node: LN) -> Iterator[Line]:
1451         """Default `visit_*()` implementation. Recurses to children of `node`."""
1452         if isinstance(node, Leaf):
1453             any_open_brackets = self.current_line.bracket_tracker.any_open_brackets()
1454             for comment in generate_comments(node):
1455                 if any_open_brackets:
1456                     # any comment within brackets is subject to splitting
1457                     self.current_line.append(comment)
1458                 elif comment.type == token.COMMENT:
1459                     # regular trailing comment
1460                     self.current_line.append(comment)
1461                     yield from self.line()
1462
1463                 else:
1464                     # regular standalone comment
1465                     yield from self.line()
1466
1467                     self.current_line.append(comment)
1468                     yield from self.line()
1469
1470             normalize_prefix(node, inside_brackets=any_open_brackets)
1471             if self.normalize_strings and node.type == token.STRING:
1472                 normalize_string_prefix(node, remove_u_prefix=self.remove_u_prefix)
1473                 normalize_string_quotes(node)
1474             if node.type == token.NUMBER:
1475                 normalize_numeric_literal(node, self.allow_underscores)
1476             if node.type not in WHITESPACE:
1477                 self.current_line.append(node)
1478         yield from super().visit_default(node)
1479
1480     def visit_INDENT(self, node: Node) -> Iterator[Line]:
1481         """Increase indentation level, maybe yield a line."""
1482         # In blib2to3 INDENT never holds comments.
1483         yield from self.line(+1)
1484         yield from self.visit_default(node)
1485
1486     def visit_DEDENT(self, node: Node) -> Iterator[Line]:
1487         """Decrease indentation level, maybe yield a line."""
1488         # The current line might still wait for trailing comments.  At DEDENT time
1489         # there won't be any (they would be prefixes on the preceding NEWLINE).
1490         # Emit the line then.
1491         yield from self.line()
1492
1493         # While DEDENT has no value, its prefix may contain standalone comments
1494         # that belong to the current indentation level.  Get 'em.
1495         yield from self.visit_default(node)
1496
1497         # Finally, emit the dedent.
1498         yield from self.line(-1)
1499
1500     def visit_stmt(
1501         self, node: Node, keywords: Set[str], parens: Set[str]
1502     ) -> Iterator[Line]:
1503         """Visit a statement.
1504
1505         This implementation is shared for `if`, `while`, `for`, `try`, `except`,
1506         `def`, `with`, `class`, `assert` and assignments.
1507
1508         The relevant Python language `keywords` for a given statement will be
1509         NAME leaves within it. This methods puts those on a separate line.
1510
1511         `parens` holds a set of string leaf values immediately after which
1512         invisible parens should be put.
1513         """
1514         normalize_invisible_parens(node, parens_after=parens)
1515         for child in node.children:
1516             if child.type == token.NAME and child.value in keywords:  # type: ignore
1517                 yield from self.line()
1518
1519             yield from self.visit(child)
1520
1521     def visit_suite(self, node: Node) -> Iterator[Line]:
1522         """Visit a suite."""
1523         if self.is_pyi and is_stub_suite(node):
1524             yield from self.visit(node.children[2])
1525         else:
1526             yield from self.visit_default(node)
1527
1528     def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
1529         """Visit a statement without nested statements."""
1530         is_suite_like = node.parent and node.parent.type in STATEMENT
1531         if is_suite_like:
1532             if self.is_pyi and is_stub_body(node):
1533                 yield from self.visit_default(node)
1534             else:
1535                 yield from self.line(+1)
1536                 yield from self.visit_default(node)
1537                 yield from self.line(-1)
1538
1539         else:
1540             if not self.is_pyi or not node.parent or not is_stub_suite(node.parent):
1541                 yield from self.line()
1542             yield from self.visit_default(node)
1543
1544     def visit_async_stmt(self, node: Node) -> Iterator[Line]:
1545         """Visit `async def`, `async for`, `async with`."""
1546         yield from self.line()
1547
1548         children = iter(node.children)
1549         for child in children:
1550             yield from self.visit(child)
1551
1552             if child.type == token.ASYNC:
1553                 break
1554
1555         internal_stmt = next(children)
1556         for child in internal_stmt.children:
1557             yield from self.visit(child)
1558
1559     def visit_decorators(self, node: Node) -> Iterator[Line]:
1560         """Visit decorators."""
1561         for child in node.children:
1562             yield from self.line()
1563             yield from self.visit(child)
1564
1565     def visit_SEMI(self, leaf: Leaf) -> Iterator[Line]:
1566         """Remove a semicolon and put the other statement on a separate line."""
1567         yield from self.line()
1568
1569     def visit_ENDMARKER(self, leaf: Leaf) -> Iterator[Line]:
1570         """End of file. Process outstanding comments and end with a newline."""
1571         yield from self.visit_default(leaf)
1572         yield from self.line()
1573
1574     def visit_STANDALONE_COMMENT(self, leaf: Leaf) -> Iterator[Line]:
1575         if not self.current_line.bracket_tracker.any_open_brackets():
1576             yield from self.line()
1577         yield from self.visit_default(leaf)
1578
1579     def __attrs_post_init__(self) -> None:
1580         """You are in a twisty little maze of passages."""
1581         v = self.visit_stmt
1582         Ø: Set[str] = set()
1583         self.visit_assert_stmt = partial(v, keywords={"assert"}, parens={"assert", ","})
1584         self.visit_if_stmt = partial(
1585             v, keywords={"if", "else", "elif"}, parens={"if", "elif"}
1586         )
1587         self.visit_while_stmt = partial(v, keywords={"while", "else"}, parens={"while"})
1588         self.visit_for_stmt = partial(v, keywords={"for", "else"}, parens={"for", "in"})
1589         self.visit_try_stmt = partial(
1590             v, keywords={"try", "except", "else", "finally"}, parens=Ø
1591         )
1592         self.visit_except_clause = partial(v, keywords={"except"}, parens=Ø)
1593         self.visit_with_stmt = partial(v, keywords={"with"}, parens=Ø)
1594         self.visit_funcdef = partial(v, keywords={"def"}, parens=Ø)
1595         self.visit_classdef = partial(v, keywords={"class"}, parens=Ø)
1596         self.visit_expr_stmt = partial(v, keywords=Ø, parens=ASSIGNMENTS)
1597         self.visit_return_stmt = partial(v, keywords={"return"}, parens={"return"})
1598         self.visit_import_from = partial(v, keywords=Ø, parens={"import"})
1599         self.visit_async_funcdef = self.visit_async_stmt
1600         self.visit_decorated = self.visit_decorators
1601
1602
1603 IMPLICIT_TUPLE = {syms.testlist, syms.testlist_star_expr, syms.exprlist}
1604 BRACKET = {token.LPAR: token.RPAR, token.LSQB: token.RSQB, token.LBRACE: token.RBRACE}
1605 OPENING_BRACKETS = set(BRACKET.keys())
1606 CLOSING_BRACKETS = set(BRACKET.values())
1607 BRACKETS = OPENING_BRACKETS | CLOSING_BRACKETS
1608 ALWAYS_NO_SPACE = CLOSING_BRACKETS | {token.COMMA, STANDALONE_COMMENT}
1609
1610
1611 def whitespace(leaf: Leaf, *, complex_subscript: bool) -> str:  # noqa C901
1612     """Return whitespace prefix if needed for the given `leaf`.
1613
1614     `complex_subscript` signals whether the given leaf is part of a subscription
1615     which has non-trivial arguments, like arithmetic expressions or function calls.
1616     """
1617     NO = ""
1618     SPACE = " "
1619     DOUBLESPACE = "  "
1620     t = leaf.type
1621     p = leaf.parent
1622     v = leaf.value
1623     if t in ALWAYS_NO_SPACE:
1624         return NO
1625
1626     if t == token.COMMENT:
1627         return DOUBLESPACE
1628
1629     assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
1630     if t == token.COLON and p.type not in {
1631         syms.subscript,
1632         syms.subscriptlist,
1633         syms.sliceop,
1634     }:
1635         return NO
1636
1637     prev = leaf.prev_sibling
1638     if not prev:
1639         prevp = preceding_leaf(p)
1640         if not prevp or prevp.type in OPENING_BRACKETS:
1641             return NO
1642
1643         if t == token.COLON:
1644             if prevp.type == token.COLON:
1645                 return NO
1646
1647             elif prevp.type != token.COMMA and not complex_subscript:
1648                 return NO
1649
1650             return SPACE
1651
1652         if prevp.type == token.EQUAL:
1653             if prevp.parent:
1654                 if prevp.parent.type in {
1655                     syms.arglist,
1656                     syms.argument,
1657                     syms.parameters,
1658                     syms.varargslist,
1659                 }:
1660                     return NO
1661
1662                 elif prevp.parent.type == syms.typedargslist:
1663                     # A bit hacky: if the equal sign has whitespace, it means we
1664                     # previously found it's a typed argument.  So, we're using
1665                     # that, too.
1666                     return prevp.prefix
1667
1668         elif prevp.type in STARS:
1669             if is_vararg(prevp, within=VARARGS_PARENTS | UNPACKING_PARENTS):
1670                 return NO
1671
1672         elif prevp.type == token.COLON:
1673             if prevp.parent and prevp.parent.type in {syms.subscript, syms.sliceop}:
1674                 return SPACE if complex_subscript else NO
1675
1676         elif (
1677             prevp.parent
1678             and prevp.parent.type == syms.factor
1679             and prevp.type in MATH_OPERATORS
1680         ):
1681             return NO
1682
1683         elif (
1684             prevp.type == token.RIGHTSHIFT
1685             and prevp.parent
1686             and prevp.parent.type == syms.shift_expr
1687             and prevp.prev_sibling
1688             and prevp.prev_sibling.type == token.NAME
1689             and prevp.prev_sibling.value == "print"  # type: ignore
1690         ):
1691             # Python 2 print chevron
1692             return NO
1693
1694     elif prev.type in OPENING_BRACKETS:
1695         return NO
1696
1697     if p.type in {syms.parameters, syms.arglist}:
1698         # untyped function signatures or calls
1699         if not prev or prev.type != token.COMMA:
1700             return NO
1701
1702     elif p.type == syms.varargslist:
1703         # lambdas
1704         if prev and prev.type != token.COMMA:
1705             return NO
1706
1707     elif p.type == syms.typedargslist:
1708         # typed function signatures
1709         if not prev:
1710             return NO
1711
1712         if t == token.EQUAL:
1713             if prev.type != syms.tname:
1714                 return NO
1715
1716         elif prev.type == token.EQUAL:
1717             # A bit hacky: if the equal sign has whitespace, it means we
1718             # previously found it's a typed argument.  So, we're using that, too.
1719             return prev.prefix
1720
1721         elif prev.type != token.COMMA:
1722             return NO
1723
1724     elif p.type == syms.tname:
1725         # type names
1726         if not prev:
1727             prevp = preceding_leaf(p)
1728             if not prevp or prevp.type != token.COMMA:
1729                 return NO
1730
1731     elif p.type == syms.trailer:
1732         # attributes and calls
1733         if t == token.LPAR or t == token.RPAR:
1734             return NO
1735
1736         if not prev:
1737             if t == token.DOT:
1738                 prevp = preceding_leaf(p)
1739                 if not prevp or prevp.type != token.NUMBER:
1740                     return NO
1741
1742             elif t == token.LSQB:
1743                 return NO
1744
1745         elif prev.type != token.COMMA:
1746             return NO
1747
1748     elif p.type == syms.argument:
1749         # single argument
1750         if t == token.EQUAL:
1751             return NO
1752
1753         if not prev:
1754             prevp = preceding_leaf(p)
1755             if not prevp or prevp.type == token.LPAR:
1756                 return NO
1757
1758         elif prev.type in {token.EQUAL} | STARS:
1759             return NO
1760
1761     elif p.type == syms.decorator:
1762         # decorators
1763         return NO
1764
1765     elif p.type == syms.dotted_name:
1766         if prev:
1767             return NO
1768
1769         prevp = preceding_leaf(p)
1770         if not prevp or prevp.type == token.AT or prevp.type == token.DOT:
1771             return NO
1772
1773     elif p.type == syms.classdef:
1774         if t == token.LPAR:
1775             return NO
1776
1777         if prev and prev.type == token.LPAR:
1778             return NO
1779
1780     elif p.type in {syms.subscript, syms.sliceop}:
1781         # indexing
1782         if not prev:
1783             assert p.parent is not None, "subscripts are always parented"
1784             if p.parent.type == syms.subscriptlist:
1785                 return SPACE
1786
1787             return NO
1788
1789         elif not complex_subscript:
1790             return NO
1791
1792     elif p.type == syms.atom:
1793         if prev and t == token.DOT:
1794             # dots, but not the first one.
1795             return NO
1796
1797     elif p.type == syms.dictsetmaker:
1798         # dict unpacking
1799         if prev and prev.type == token.DOUBLESTAR:
1800             return NO
1801
1802     elif p.type in {syms.factor, syms.star_expr}:
1803         # unary ops
1804         if not prev:
1805             prevp = preceding_leaf(p)
1806             if not prevp or prevp.type in OPENING_BRACKETS:
1807                 return NO
1808
1809             prevp_parent = prevp.parent
1810             assert prevp_parent is not None
1811             if prevp.type == token.COLON and prevp_parent.type in {
1812                 syms.subscript,
1813                 syms.sliceop,
1814             }:
1815                 return NO
1816
1817             elif prevp.type == token.EQUAL and prevp_parent.type == syms.argument:
1818                 return NO
1819
1820         elif t in {token.NAME, token.NUMBER, token.STRING}:
1821             return NO
1822
1823     elif p.type == syms.import_from:
1824         if t == token.DOT:
1825             if prev and prev.type == token.DOT:
1826                 return NO
1827
1828         elif t == token.NAME:
1829             if v == "import":
1830                 return SPACE
1831
1832             if prev and prev.type == token.DOT:
1833                 return NO
1834
1835     elif p.type == syms.sliceop:
1836         return NO
1837
1838     return SPACE
1839
1840
1841 def preceding_leaf(node: Optional[LN]) -> Optional[Leaf]:
1842     """Return the first leaf that precedes `node`, if any."""
1843     while node:
1844         res = node.prev_sibling
1845         if res:
1846             if isinstance(res, Leaf):
1847                 return res
1848
1849             try:
1850                 return list(res.leaves())[-1]
1851
1852             except IndexError:
1853                 return None
1854
1855         node = node.parent
1856     return None
1857
1858
1859 def child_towards(ancestor: Node, descendant: LN) -> Optional[LN]:
1860     """Return the child of `ancestor` that contains `descendant`."""
1861     node: Optional[LN] = descendant
1862     while node and node.parent != ancestor:
1863         node = node.parent
1864     return node
1865
1866
1867 def container_of(leaf: Leaf) -> LN:
1868     """Return `leaf` or one of its ancestors that is the topmost container of it.
1869
1870     By "container" we mean a node where `leaf` is the very first child.
1871     """
1872     same_prefix = leaf.prefix
1873     container: LN = leaf
1874     while container:
1875         parent = container.parent
1876         if parent is None:
1877             break
1878
1879         if parent.children[0].prefix != same_prefix:
1880             break
1881
1882         if parent.type == syms.file_input:
1883             break
1884
1885         if parent.prev_sibling is not None and parent.prev_sibling.type in BRACKETS:
1886             break
1887
1888         container = parent
1889     return container
1890
1891
1892 def is_split_after_delimiter(leaf: Leaf, previous: Optional[Leaf] = None) -> int:
1893     """Return the priority of the `leaf` delimiter, given a line break after it.
1894
1895     The delimiter priorities returned here are from those delimiters that would
1896     cause a line break after themselves.
1897
1898     Higher numbers are higher priority.
1899     """
1900     if leaf.type == token.COMMA:
1901         return COMMA_PRIORITY
1902
1903     return 0
1904
1905
1906 def is_split_before_delimiter(leaf: Leaf, previous: Optional[Leaf] = None) -> int:
1907     """Return the priority of the `leaf` delimiter, given a line break before it.
1908
1909     The delimiter priorities returned here are from those delimiters that would
1910     cause a line break before themselves.
1911
1912     Higher numbers are higher priority.
1913     """
1914     if is_vararg(leaf, within=VARARGS_PARENTS | UNPACKING_PARENTS):
1915         # * and ** might also be MATH_OPERATORS but in this case they are not.
1916         # Don't treat them as a delimiter.
1917         return 0
1918
1919     if (
1920         leaf.type == token.DOT
1921         and leaf.parent
1922         and leaf.parent.type not in {syms.import_from, syms.dotted_name}
1923         and (previous is None or previous.type in CLOSING_BRACKETS)
1924     ):
1925         return DOT_PRIORITY
1926
1927     if (
1928         leaf.type in MATH_OPERATORS
1929         and leaf.parent
1930         and leaf.parent.type not in {syms.factor, syms.star_expr}
1931     ):
1932         return MATH_PRIORITIES[leaf.type]
1933
1934     if leaf.type in COMPARATORS:
1935         return COMPARATOR_PRIORITY
1936
1937     if (
1938         leaf.type == token.STRING
1939         and previous is not None
1940         and previous.type == token.STRING
1941     ):
1942         return STRING_PRIORITY
1943
1944     if leaf.type not in {token.NAME, token.ASYNC}:
1945         return 0
1946
1947     if (
1948         leaf.value == "for"
1949         and leaf.parent
1950         and leaf.parent.type in {syms.comp_for, syms.old_comp_for}
1951         or leaf.type == token.ASYNC
1952     ):
1953         if (
1954             not isinstance(leaf.prev_sibling, Leaf)
1955             or leaf.prev_sibling.value != "async"
1956         ):
1957             return COMPREHENSION_PRIORITY
1958
1959     if (
1960         leaf.value == "if"
1961         and leaf.parent
1962         and leaf.parent.type in {syms.comp_if, syms.old_comp_if}
1963     ):
1964         return COMPREHENSION_PRIORITY
1965
1966     if leaf.value in {"if", "else"} and leaf.parent and leaf.parent.type == syms.test:
1967         return TERNARY_PRIORITY
1968
1969     if leaf.value == "is":
1970         return COMPARATOR_PRIORITY
1971
1972     if (
1973         leaf.value == "in"
1974         and leaf.parent
1975         and leaf.parent.type in {syms.comp_op, syms.comparison}
1976         and not (
1977             previous is not None
1978             and previous.type == token.NAME
1979             and previous.value == "not"
1980         )
1981     ):
1982         return COMPARATOR_PRIORITY
1983
1984     if (
1985         leaf.value == "not"
1986         and leaf.parent
1987         and leaf.parent.type == syms.comp_op
1988         and not (
1989             previous is not None
1990             and previous.type == token.NAME
1991             and previous.value == "is"
1992         )
1993     ):
1994         return COMPARATOR_PRIORITY
1995
1996     if leaf.value in LOGIC_OPERATORS and leaf.parent:
1997         return LOGIC_PRIORITY
1998
1999     return 0
2000
2001
2002 FMT_OFF = {"# fmt: off", "# fmt:off", "# yapf: disable"}
2003 FMT_ON = {"# fmt: on", "# fmt:on", "# yapf: enable"}
2004
2005
2006 def generate_comments(leaf: LN) -> Iterator[Leaf]:
2007     """Clean the prefix of the `leaf` and generate comments from it, if any.
2008
2009     Comments in lib2to3 are shoved into the whitespace prefix.  This happens
2010     in `pgen2/driver.py:Driver.parse_tokens()`.  This was a brilliant implementation
2011     move because it does away with modifying the grammar to include all the
2012     possible places in which comments can be placed.
2013
2014     The sad consequence for us though is that comments don't "belong" anywhere.
2015     This is why this function generates simple parentless Leaf objects for
2016     comments.  We simply don't know what the correct parent should be.
2017
2018     No matter though, we can live without this.  We really only need to
2019     differentiate between inline and standalone comments.  The latter don't
2020     share the line with any code.
2021
2022     Inline comments are emitted as regular token.COMMENT leaves.  Standalone
2023     are emitted with a fake STANDALONE_COMMENT token identifier.
2024     """
2025     for pc in list_comments(leaf.prefix, is_endmarker=leaf.type == token.ENDMARKER):
2026         yield Leaf(pc.type, pc.value, prefix="\n" * pc.newlines)
2027
2028
2029 @dataclass
2030 class ProtoComment:
2031     """Describes a piece of syntax that is a comment.
2032
2033     It's not a :class:`blib2to3.pytree.Leaf` so that:
2034
2035     * it can be cached (`Leaf` objects should not be reused more than once as
2036       they store their lineno, column, prefix, and parent information);
2037     * `newlines` and `consumed` fields are kept separate from the `value`. This
2038       simplifies handling of special marker comments like ``# fmt: off/on``.
2039     """
2040
2041     type: int  # token.COMMENT or STANDALONE_COMMENT
2042     value: str  # content of the comment
2043     newlines: int  # how many newlines before the comment
2044     consumed: int  # how many characters of the original leaf's prefix did we consume
2045
2046
2047 @lru_cache(maxsize=4096)
2048 def list_comments(prefix: str, *, is_endmarker: bool) -> List[ProtoComment]:
2049     """Return a list of :class:`ProtoComment` objects parsed from the given `prefix`."""
2050     result: List[ProtoComment] = []
2051     if not prefix or "#" not in prefix:
2052         return result
2053
2054     consumed = 0
2055     nlines = 0
2056     for index, line in enumerate(prefix.split("\n")):
2057         consumed += len(line) + 1  # adding the length of the split '\n'
2058         line = line.lstrip()
2059         if not line:
2060             nlines += 1
2061         if not line.startswith("#"):
2062             continue
2063
2064         if index == 0 and not is_endmarker:
2065             comment_type = token.COMMENT  # simple trailing comment
2066         else:
2067             comment_type = STANDALONE_COMMENT
2068         comment = make_comment(line)
2069         result.append(
2070             ProtoComment(
2071                 type=comment_type, value=comment, newlines=nlines, consumed=consumed
2072             )
2073         )
2074         nlines = 0
2075     return result
2076
2077
2078 def make_comment(content: str) -> str:
2079     """Return a consistently formatted comment from the given `content` string.
2080
2081     All comments (except for "##", "#!", "#:", '#'", "#%%") should have a single
2082     space between the hash sign and the content.
2083
2084     If `content` didn't start with a hash sign, one is provided.
2085     """
2086     content = content.rstrip()
2087     if not content:
2088         return "#"
2089
2090     if content[0] == "#":
2091         content = content[1:]
2092     if content and content[0] not in " !:#'%":
2093         content = " " + content
2094     return "#" + content
2095
2096
2097 def split_line(
2098     line: Line, line_length: int, inner: bool = False, py36: bool = False
2099 ) -> Iterator[Line]:
2100     """Split a `line` into potentially many lines.
2101
2102     They should fit in the allotted `line_length` but might not be able to.
2103     `inner` signifies that there were a pair of brackets somewhere around the
2104     current `line`, possibly transitively. This means we can fallback to splitting
2105     by delimiters if the LHS/RHS don't yield any results.
2106
2107     If `py36` is True, splitting may generate syntax that is only compatible
2108     with Python 3.6 and later.
2109     """
2110     if line.is_comment:
2111         yield line
2112         return
2113
2114     line_str = str(line).strip("\n")
2115
2116     # we don't want to split special comments like type annotations
2117     # https://github.com/python/typing/issues/186
2118     has_special_comment = False
2119     for leaf in line.leaves:
2120         for comment in line.comments_after(leaf):
2121             if leaf.type == token.COMMA and is_special_comment(comment):
2122                 has_special_comment = True
2123
2124     if (
2125         not has_special_comment
2126         and not line.should_explode
2127         and is_line_short_enough(line, line_length=line_length, line_str=line_str)
2128     ):
2129         yield line
2130         return
2131
2132     split_funcs: List[SplitFunc]
2133     if line.is_def:
2134         split_funcs = [left_hand_split]
2135     else:
2136
2137         def rhs(line: Line, py36: bool = False) -> Iterator[Line]:
2138             for omit in generate_trailers_to_omit(line, line_length):
2139                 lines = list(right_hand_split(line, line_length, py36, omit=omit))
2140                 if is_line_short_enough(lines[0], line_length=line_length):
2141                     yield from lines
2142                     return
2143
2144             # All splits failed, best effort split with no omits.
2145             # This mostly happens to multiline strings that are by definition
2146             # reported as not fitting a single line.
2147             yield from right_hand_split(line, py36)
2148
2149         if line.inside_brackets:
2150             split_funcs = [delimiter_split, standalone_comment_split, rhs]
2151         else:
2152             split_funcs = [rhs]
2153     for split_func in split_funcs:
2154         # We are accumulating lines in `result` because we might want to abort
2155         # mission and return the original line in the end, or attempt a different
2156         # split altogether.
2157         result: List[Line] = []
2158         try:
2159             for l in split_func(line, py36):
2160                 if str(l).strip("\n") == line_str:
2161                     raise CannotSplit("Split function returned an unchanged result")
2162
2163                 result.extend(
2164                     split_line(l, line_length=line_length, inner=True, py36=py36)
2165                 )
2166         except CannotSplit:
2167             continue
2168
2169         else:
2170             yield from result
2171             break
2172
2173     else:
2174         yield line
2175
2176
2177 def left_hand_split(line: Line, py36: bool = False) -> Iterator[Line]:
2178     """Split line into many lines, starting with the first matching bracket pair.
2179
2180     Note: this usually looks weird, only use this for function definitions.
2181     Prefer RHS otherwise.  This is why this function is not symmetrical with
2182     :func:`right_hand_split` which also handles optional parentheses.
2183     """
2184     tail_leaves: List[Leaf] = []
2185     body_leaves: List[Leaf] = []
2186     head_leaves: List[Leaf] = []
2187     current_leaves = head_leaves
2188     matching_bracket = None
2189     for leaf in line.leaves:
2190         if (
2191             current_leaves is body_leaves
2192             and leaf.type in CLOSING_BRACKETS
2193             and leaf.opening_bracket is matching_bracket
2194         ):
2195             current_leaves = tail_leaves if body_leaves else head_leaves
2196         current_leaves.append(leaf)
2197         if current_leaves is head_leaves:
2198             if leaf.type in OPENING_BRACKETS:
2199                 matching_bracket = leaf
2200                 current_leaves = body_leaves
2201     if not matching_bracket:
2202         raise CannotSplit("No brackets found")
2203
2204     head = bracket_split_build_line(head_leaves, line, matching_bracket)
2205     body = bracket_split_build_line(body_leaves, line, matching_bracket, is_body=True)
2206     tail = bracket_split_build_line(tail_leaves, line, matching_bracket)
2207     bracket_split_succeeded_or_raise(head, body, tail)
2208     for result in (head, body, tail):
2209         if result:
2210             yield result
2211
2212
2213 def right_hand_split(
2214     line: Line, line_length: int, py36: bool = False, omit: Collection[LeafID] = ()
2215 ) -> Iterator[Line]:
2216     """Split line into many lines, starting with the last matching bracket pair.
2217
2218     If the split was by optional parentheses, attempt splitting without them, too.
2219     `omit` is a collection of closing bracket IDs that shouldn't be considered for
2220     this split.
2221
2222     Note: running this function modifies `bracket_depth` on the leaves of `line`.
2223     """
2224     tail_leaves: List[Leaf] = []
2225     body_leaves: List[Leaf] = []
2226     head_leaves: List[Leaf] = []
2227     current_leaves = tail_leaves
2228     opening_bracket = None
2229     closing_bracket = None
2230     for leaf in reversed(line.leaves):
2231         if current_leaves is body_leaves:
2232             if leaf is opening_bracket:
2233                 current_leaves = head_leaves if body_leaves else tail_leaves
2234         current_leaves.append(leaf)
2235         if current_leaves is tail_leaves:
2236             if leaf.type in CLOSING_BRACKETS and id(leaf) not in omit:
2237                 opening_bracket = leaf.opening_bracket
2238                 closing_bracket = leaf
2239                 current_leaves = body_leaves
2240     if not (opening_bracket and closing_bracket and head_leaves):
2241         # If there is no opening or closing_bracket that means the split failed and
2242         # all content is in the tail.  Otherwise, if `head_leaves` are empty, it means
2243         # the matching `opening_bracket` wasn't available on `line` anymore.
2244         raise CannotSplit("No brackets found")
2245
2246     tail_leaves.reverse()
2247     body_leaves.reverse()
2248     head_leaves.reverse()
2249     head = bracket_split_build_line(head_leaves, line, opening_bracket)
2250     body = bracket_split_build_line(body_leaves, line, opening_bracket, is_body=True)
2251     tail = bracket_split_build_line(tail_leaves, line, opening_bracket)
2252     bracket_split_succeeded_or_raise(head, body, tail)
2253     if (
2254         # the body shouldn't be exploded
2255         not body.should_explode
2256         # the opening bracket is an optional paren
2257         and opening_bracket.type == token.LPAR
2258         and not opening_bracket.value
2259         # the closing bracket is an optional paren
2260         and closing_bracket.type == token.RPAR
2261         and not closing_bracket.value
2262         # it's not an import (optional parens are the only thing we can split on
2263         # in this case; attempting a split without them is a waste of time)
2264         and not line.is_import
2265         # there are no standalone comments in the body
2266         and not body.contains_standalone_comments(0)
2267         # and we can actually remove the parens
2268         and can_omit_invisible_parens(body, line_length)
2269     ):
2270         omit = {id(closing_bracket), *omit}
2271         try:
2272             yield from right_hand_split(line, line_length, py36=py36, omit=omit)
2273             return
2274
2275         except CannotSplit:
2276             if not (
2277                 can_be_split(body)
2278                 or is_line_short_enough(body, line_length=line_length)
2279             ):
2280                 raise CannotSplit(
2281                     "Splitting failed, body is still too long and can't be split."
2282                 )
2283
2284             elif head.contains_multiline_strings() or tail.contains_multiline_strings():
2285                 raise CannotSplit(
2286                     "The current optional pair of parentheses is bound to fail to "
2287                     "satisfy the splitting algorithm because the head or the tail "
2288                     "contains multiline strings which by definition never fit one "
2289                     "line."
2290                 )
2291
2292     ensure_visible(opening_bracket)
2293     ensure_visible(closing_bracket)
2294     for result in (head, body, tail):
2295         if result:
2296             yield result
2297
2298
2299 def bracket_split_succeeded_or_raise(head: Line, body: Line, tail: Line) -> None:
2300     """Raise :exc:`CannotSplit` if the last left- or right-hand split failed.
2301
2302     Do nothing otherwise.
2303
2304     A left- or right-hand split is based on a pair of brackets. Content before
2305     (and including) the opening bracket is left on one line, content inside the
2306     brackets is put on a separate line, and finally content starting with and
2307     following the closing bracket is put on a separate line.
2308
2309     Those are called `head`, `body`, and `tail`, respectively. If the split
2310     produced the same line (all content in `head`) or ended up with an empty `body`
2311     and the `tail` is just the closing bracket, then it's considered failed.
2312     """
2313     tail_len = len(str(tail).strip())
2314     if not body:
2315         if tail_len == 0:
2316             raise CannotSplit("Splitting brackets produced the same line")
2317
2318         elif tail_len < 3:
2319             raise CannotSplit(
2320                 f"Splitting brackets on an empty body to save "
2321                 f"{tail_len} characters is not worth it"
2322             )
2323
2324
2325 def bracket_split_build_line(
2326     leaves: List[Leaf], original: Line, opening_bracket: Leaf, *, is_body: bool = False
2327 ) -> Line:
2328     """Return a new line with given `leaves` and respective comments from `original`.
2329
2330     If `is_body` is True, the result line is one-indented inside brackets and as such
2331     has its first leaf's prefix normalized and a trailing comma added when expected.
2332     """
2333     result = Line(depth=original.depth)
2334     if is_body:
2335         result.inside_brackets = True
2336         result.depth += 1
2337         if leaves:
2338             # Since body is a new indent level, remove spurious leading whitespace.
2339             normalize_prefix(leaves[0], inside_brackets=True)
2340             # Ensure a trailing comma when expected.
2341             if original.is_import:
2342                 if leaves[-1].type != token.COMMA:
2343                     leaves.append(Leaf(token.COMMA, ","))
2344     # Populate the line
2345     for leaf in leaves:
2346         result.append(leaf, preformatted=True)
2347         for comment_after in original.comments_after(leaf):
2348             result.append(comment_after, preformatted=True)
2349     if is_body:
2350         result.should_explode = should_explode(result, opening_bracket)
2351     return result
2352
2353
2354 def dont_increase_indentation(split_func: SplitFunc) -> SplitFunc:
2355     """Normalize prefix of the first leaf in every line returned by `split_func`.
2356
2357     This is a decorator over relevant split functions.
2358     """
2359
2360     @wraps(split_func)
2361     def split_wrapper(line: Line, py36: bool = False) -> Iterator[Line]:
2362         for l in split_func(line, py36):
2363             normalize_prefix(l.leaves[0], inside_brackets=True)
2364             yield l
2365
2366     return split_wrapper
2367
2368
2369 @dont_increase_indentation
2370 def delimiter_split(line: Line, py36: bool = False) -> Iterator[Line]:
2371     """Split according to delimiters of the highest priority.
2372
2373     If `py36` is True, the split will add trailing commas also in function
2374     signatures that contain `*` and `**`.
2375     """
2376     try:
2377         last_leaf = line.leaves[-1]
2378     except IndexError:
2379         raise CannotSplit("Line empty")
2380
2381     bt = line.bracket_tracker
2382     try:
2383         delimiter_priority = bt.max_delimiter_priority(exclude={id(last_leaf)})
2384     except ValueError:
2385         raise CannotSplit("No delimiters found")
2386
2387     if delimiter_priority == DOT_PRIORITY:
2388         if bt.delimiter_count_with_priority(delimiter_priority) == 1:
2389             raise CannotSplit("Splitting a single attribute from its owner looks wrong")
2390
2391     current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
2392     lowest_depth = sys.maxsize
2393     trailing_comma_safe = True
2394
2395     def append_to_line(leaf: Leaf) -> Iterator[Line]:
2396         """Append `leaf` to current line or to new line if appending impossible."""
2397         nonlocal current_line
2398         try:
2399             current_line.append_safe(leaf, preformatted=True)
2400         except ValueError:
2401             yield current_line
2402
2403             current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
2404             current_line.append(leaf)
2405
2406     for leaf in line.leaves:
2407         yield from append_to_line(leaf)
2408
2409         for comment_after in line.comments_after(leaf):
2410             yield from append_to_line(comment_after)
2411
2412         lowest_depth = min(lowest_depth, leaf.bracket_depth)
2413         if leaf.bracket_depth == lowest_depth and is_vararg(
2414             leaf, within=VARARGS_PARENTS
2415         ):
2416             trailing_comma_safe = trailing_comma_safe and py36
2417         leaf_priority = bt.delimiters.get(id(leaf))
2418         if leaf_priority == delimiter_priority:
2419             yield current_line
2420
2421             current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
2422     if current_line:
2423         if (
2424             trailing_comma_safe
2425             and delimiter_priority == COMMA_PRIORITY
2426             and current_line.leaves[-1].type != token.COMMA
2427             and current_line.leaves[-1].type != STANDALONE_COMMENT
2428         ):
2429             current_line.append(Leaf(token.COMMA, ","))
2430         yield current_line
2431
2432
2433 @dont_increase_indentation
2434 def standalone_comment_split(line: Line, py36: bool = False) -> Iterator[Line]:
2435     """Split standalone comments from the rest of the line."""
2436     if not line.contains_standalone_comments(0):
2437         raise CannotSplit("Line does not have any standalone comments")
2438
2439     current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
2440
2441     def append_to_line(leaf: Leaf) -> Iterator[Line]:
2442         """Append `leaf` to current line or to new line if appending impossible."""
2443         nonlocal current_line
2444         try:
2445             current_line.append_safe(leaf, preformatted=True)
2446         except ValueError:
2447             yield current_line
2448
2449             current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
2450             current_line.append(leaf)
2451
2452     for leaf in line.leaves:
2453         yield from append_to_line(leaf)
2454
2455         for comment_after in line.comments_after(leaf):
2456             yield from append_to_line(comment_after)
2457
2458     if current_line:
2459         yield current_line
2460
2461
2462 def is_import(leaf: Leaf) -> bool:
2463     """Return True if the given leaf starts an import statement."""
2464     p = leaf.parent
2465     t = leaf.type
2466     v = leaf.value
2467     return bool(
2468         t == token.NAME
2469         and (
2470             (v == "import" and p and p.type == syms.import_name)
2471             or (v == "from" and p and p.type == syms.import_from)
2472         )
2473     )
2474
2475
2476 def is_special_comment(leaf: Leaf) -> bool:
2477     """Return True if the given leaf is a special comment.
2478     Only returns true for type comments for now."""
2479     t = leaf.type
2480     v = leaf.value
2481     return bool(
2482         (t == token.COMMENT or t == STANDALONE_COMMENT) and (v.startswith("# type:"))
2483     )
2484
2485
2486 def normalize_prefix(leaf: Leaf, *, inside_brackets: bool) -> None:
2487     """Leave existing extra newlines if not `inside_brackets`. Remove everything
2488     else.
2489
2490     Note: don't use backslashes for formatting or you'll lose your voting rights.
2491     """
2492     if not inside_brackets:
2493         spl = leaf.prefix.split("#")
2494         if "\\" not in spl[0]:
2495             nl_count = spl[-1].count("\n")
2496             if len(spl) > 1:
2497                 nl_count -= 1
2498             leaf.prefix = "\n" * nl_count
2499             return
2500
2501     leaf.prefix = ""
2502
2503
2504 def normalize_string_prefix(leaf: Leaf, remove_u_prefix: bool = False) -> None:
2505     """Make all string prefixes lowercase.
2506
2507     If remove_u_prefix is given, also removes any u prefix from the string.
2508
2509     Note: Mutates its argument.
2510     """
2511     match = re.match(r"^([furbFURB]*)(.*)$", leaf.value, re.DOTALL)
2512     assert match is not None, f"failed to match string {leaf.value!r}"
2513     orig_prefix = match.group(1)
2514     new_prefix = orig_prefix.lower()
2515     if remove_u_prefix:
2516         new_prefix = new_prefix.replace("u", "")
2517     leaf.value = f"{new_prefix}{match.group(2)}"
2518
2519
2520 def normalize_string_quotes(leaf: Leaf) -> None:
2521     """Prefer double quotes but only if it doesn't cause more escaping.
2522
2523     Adds or removes backslashes as appropriate. Doesn't parse and fix
2524     strings nested in f-strings (yet).
2525
2526     Note: Mutates its argument.
2527     """
2528     value = leaf.value.lstrip("furbFURB")
2529     if value[:3] == '"""':
2530         return
2531
2532     elif value[:3] == "'''":
2533         orig_quote = "'''"
2534         new_quote = '"""'
2535     elif value[0] == '"':
2536         orig_quote = '"'
2537         new_quote = "'"
2538     else:
2539         orig_quote = "'"
2540         new_quote = '"'
2541     first_quote_pos = leaf.value.find(orig_quote)
2542     if first_quote_pos == -1:
2543         return  # There's an internal error
2544
2545     prefix = leaf.value[:first_quote_pos]
2546     unescaped_new_quote = re.compile(rf"(([^\\]|^)(\\\\)*){new_quote}")
2547     escaped_new_quote = re.compile(rf"([^\\]|^)\\((?:\\\\)*){new_quote}")
2548     escaped_orig_quote = re.compile(rf"([^\\]|^)\\((?:\\\\)*){orig_quote}")
2549     body = leaf.value[first_quote_pos + len(orig_quote) : -len(orig_quote)]
2550     if "r" in prefix.casefold():
2551         if unescaped_new_quote.search(body):
2552             # There's at least one unescaped new_quote in this raw string
2553             # so converting is impossible
2554             return
2555
2556         # Do not introduce or remove backslashes in raw strings
2557         new_body = body
2558     else:
2559         # remove unnecessary escapes
2560         new_body = sub_twice(escaped_new_quote, rf"\1\2{new_quote}", body)
2561         if body != new_body:
2562             # Consider the string without unnecessary escapes as the original
2563             body = new_body
2564             leaf.value = f"{prefix}{orig_quote}{body}{orig_quote}"
2565         new_body = sub_twice(escaped_orig_quote, rf"\1\2{orig_quote}", new_body)
2566         new_body = sub_twice(unescaped_new_quote, rf"\1\\{new_quote}", new_body)
2567     if "f" in prefix.casefold():
2568         matches = re.findall(r"[^{]\{(.*?)\}[^}]", new_body)
2569         for m in matches:
2570             if "\\" in str(m):
2571                 # Do not introduce backslashes in interpolated expressions
2572                 return
2573     if new_quote == '"""' and new_body[-1:] == '"':
2574         # edge case:
2575         new_body = new_body[:-1] + '\\"'
2576     orig_escape_count = body.count("\\")
2577     new_escape_count = new_body.count("\\")
2578     if new_escape_count > orig_escape_count:
2579         return  # Do not introduce more escaping
2580
2581     if new_escape_count == orig_escape_count and orig_quote == '"':
2582         return  # Prefer double quotes
2583
2584     leaf.value = f"{prefix}{new_quote}{new_body}{new_quote}"
2585
2586
2587 def normalize_numeric_literal(leaf: Leaf, allow_underscores: bool) -> None:
2588     """Normalizes numeric (float, int, and complex) literals.
2589
2590     All letters used in the representation are normalized to lowercase (except
2591     in Python 2 long literals), and long number literals are split using underscores.
2592     """
2593     text = leaf.value.lower()
2594     if text.startswith(("0o", "0b")):
2595         # Leave octal and binary literals alone.
2596         pass
2597     elif text.startswith("0x"):
2598         # Change hex literals to upper case.
2599         before, after = text[:2], text[2:]
2600         text = f"{before}{after.upper()}"
2601     elif "e" in text:
2602         before, after = text.split("e")
2603         sign = ""
2604         if after.startswith("-"):
2605             after = after[1:]
2606             sign = "-"
2607         elif after.startswith("+"):
2608             after = after[1:]
2609         before = format_float_or_int_string(before, allow_underscores)
2610         after = format_int_string(after, allow_underscores)
2611         text = f"{before}e{sign}{after}"
2612     elif text.endswith(("j", "l")):
2613         number = text[:-1]
2614         suffix = text[-1]
2615         # Capitalize in "2L" because "l" looks too similar to "1".
2616         if suffix == "l":
2617             suffix = "L"
2618         text = f"{format_float_or_int_string(number, allow_underscores)}{suffix}"
2619     else:
2620         text = format_float_or_int_string(text, allow_underscores)
2621     leaf.value = text
2622
2623
2624 def format_float_or_int_string(text: str, allow_underscores: bool) -> str:
2625     """Formats a float string like "1.0"."""
2626     if "." not in text:
2627         return format_int_string(text, allow_underscores)
2628
2629     before, after = text.split(".")
2630     before = format_int_string(before, allow_underscores) if before else "0"
2631     if after:
2632         after = format_int_string(after, allow_underscores, count_from_end=False)
2633     else:
2634         after = "0"
2635     return f"{before}.{after}"
2636
2637
2638 def format_int_string(
2639     text: str, allow_underscores: bool, count_from_end: bool = True
2640 ) -> str:
2641     """Normalizes underscores in a string to e.g. 1_000_000.
2642
2643     Input must be a string of digits and optional underscores.
2644     If count_from_end is False, we add underscores after groups of three digits
2645     counting from the beginning instead of the end of the strings. This is used
2646     for the fractional part of float literals.
2647     """
2648     if not allow_underscores:
2649         return text
2650
2651     text = text.replace("_", "")
2652     if len(text) <= 5:
2653         # No underscores for numbers <= 5 digits long.
2654         return text
2655
2656     if count_from_end:
2657         # Avoid removing leading zeros, which are important if we're formatting
2658         # part of a number like "0.001".
2659         return format(int("1" + text), "3_")[1:].lstrip("_")
2660     else:
2661         return "_".join(text[i : i + 3] for i in range(0, len(text), 3))
2662
2663
2664 def normalize_invisible_parens(node: Node, parens_after: Set[str]) -> None:
2665     """Make existing optional parentheses invisible or create new ones.
2666
2667     `parens_after` is a set of string leaf values immeditely after which parens
2668     should be put.
2669
2670     Standardizes on visible parentheses for single-element tuples, and keeps
2671     existing visible parentheses for other tuples and generator expressions.
2672     """
2673     for pc in list_comments(node.prefix, is_endmarker=False):
2674         if pc.value in FMT_OFF:
2675             # This `node` has a prefix with `# fmt: off`, don't mess with parens.
2676             return
2677
2678     check_lpar = False
2679     for index, child in enumerate(list(node.children)):
2680         if check_lpar:
2681             if child.type == syms.atom:
2682                 if maybe_make_parens_invisible_in_atom(child):
2683                     lpar = Leaf(token.LPAR, "")
2684                     rpar = Leaf(token.RPAR, "")
2685                     index = child.remove() or 0
2686                     node.insert_child(index, Node(syms.atom, [lpar, child, rpar]))
2687             elif is_one_tuple(child):
2688                 # wrap child in visible parentheses
2689                 lpar = Leaf(token.LPAR, "(")
2690                 rpar = Leaf(token.RPAR, ")")
2691                 child.remove()
2692                 node.insert_child(index, Node(syms.atom, [lpar, child, rpar]))
2693             elif node.type == syms.import_from:
2694                 # "import from" nodes store parentheses directly as part of
2695                 # the statement
2696                 if child.type == token.LPAR:
2697                     # make parentheses invisible
2698                     child.value = ""  # type: ignore
2699                     node.children[-1].value = ""  # type: ignore
2700                 elif child.type != token.STAR:
2701                     # insert invisible parentheses
2702                     node.insert_child(index, Leaf(token.LPAR, ""))
2703                     node.append_child(Leaf(token.RPAR, ""))
2704                 break
2705
2706             elif not (isinstance(child, Leaf) and is_multiline_string(child)):
2707                 # wrap child in invisible parentheses
2708                 lpar = Leaf(token.LPAR, "")
2709                 rpar = Leaf(token.RPAR, "")
2710                 index = child.remove() or 0
2711                 node.insert_child(index, Node(syms.atom, [lpar, child, rpar]))
2712
2713         check_lpar = isinstance(child, Leaf) and child.value in parens_after
2714
2715
2716 def normalize_fmt_off(node: Node) -> None:
2717     """Convert content between `# fmt: off`/`# fmt: on` into standalone comments."""
2718     try_again = True
2719     while try_again:
2720         try_again = convert_one_fmt_off_pair(node)
2721
2722
2723 def convert_one_fmt_off_pair(node: Node) -> bool:
2724     """Convert content of a single `# fmt: off`/`# fmt: on` into a standalone comment.
2725
2726     Returns True if a pair was converted.
2727     """
2728     for leaf in node.leaves():
2729         previous_consumed = 0
2730         for comment in list_comments(leaf.prefix, is_endmarker=False):
2731             if comment.value in FMT_OFF:
2732                 # We only want standalone comments. If there's no previous leaf or
2733                 # the previous leaf is indentation, it's a standalone comment in
2734                 # disguise.
2735                 if comment.type != STANDALONE_COMMENT:
2736                     prev = preceding_leaf(leaf)
2737                     if prev and prev.type not in WHITESPACE:
2738                         continue
2739
2740                 ignored_nodes = list(generate_ignored_nodes(leaf))
2741                 if not ignored_nodes:
2742                     continue
2743
2744                 first = ignored_nodes[0]  # Can be a container node with the `leaf`.
2745                 parent = first.parent
2746                 prefix = first.prefix
2747                 first.prefix = prefix[comment.consumed :]
2748                 hidden_value = (
2749                     comment.value + "\n" + "".join(str(n) for n in ignored_nodes)
2750                 )
2751                 if hidden_value.endswith("\n"):
2752                     # That happens when one of the `ignored_nodes` ended with a NEWLINE
2753                     # leaf (possibly followed by a DEDENT).
2754                     hidden_value = hidden_value[:-1]
2755                 first_idx = None
2756                 for ignored in ignored_nodes:
2757                     index = ignored.remove()
2758                     if first_idx is None:
2759                         first_idx = index
2760                 assert parent is not None, "INTERNAL ERROR: fmt: on/off handling (1)"
2761                 assert first_idx is not None, "INTERNAL ERROR: fmt: on/off handling (2)"
2762                 parent.insert_child(
2763                     first_idx,
2764                     Leaf(
2765                         STANDALONE_COMMENT,
2766                         hidden_value,
2767                         prefix=prefix[:previous_consumed] + "\n" * comment.newlines,
2768                     ),
2769                 )
2770                 return True
2771
2772             previous_consumed = comment.consumed
2773
2774     return False
2775
2776
2777 def generate_ignored_nodes(leaf: Leaf) -> Iterator[LN]:
2778     """Starting from the container of `leaf`, generate all leaves until `# fmt: on`.
2779
2780     Stops at the end of the block.
2781     """
2782     container: Optional[LN] = container_of(leaf)
2783     while container is not None and container.type != token.ENDMARKER:
2784         for comment in list_comments(container.prefix, is_endmarker=False):
2785             if comment.value in FMT_ON:
2786                 return
2787
2788         yield container
2789
2790         container = container.next_sibling
2791
2792
2793 def maybe_make_parens_invisible_in_atom(node: LN) -> bool:
2794     """If it's safe, make the parens in the atom `node` invisible, recursively.
2795
2796     Returns whether the node should itself be wrapped in invisible parentheses.
2797
2798     """
2799     if (
2800         node.type != syms.atom
2801         or is_empty_tuple(node)
2802         or is_one_tuple(node)
2803         or is_yield(node)
2804         or max_delimiter_priority_in_atom(node) >= COMMA_PRIORITY
2805     ):
2806         return False
2807
2808     first = node.children[0]
2809     last = node.children[-1]
2810     if first.type == token.LPAR and last.type == token.RPAR:
2811         # make parentheses invisible
2812         first.value = ""  # type: ignore
2813         last.value = ""  # type: ignore
2814         if len(node.children) > 1:
2815             maybe_make_parens_invisible_in_atom(node.children[1])
2816         return False
2817
2818     return True
2819
2820
2821 def is_empty_tuple(node: LN) -> bool:
2822     """Return True if `node` holds an empty tuple."""
2823     return (
2824         node.type == syms.atom
2825         and len(node.children) == 2
2826         and node.children[0].type == token.LPAR
2827         and node.children[1].type == token.RPAR
2828     )
2829
2830
2831 def is_one_tuple(node: LN) -> bool:
2832     """Return True if `node` holds a tuple with one element, with or without parens."""
2833     if node.type == syms.atom:
2834         if len(node.children) != 3:
2835             return False
2836
2837         lpar, gexp, rpar = node.children
2838         if not (
2839             lpar.type == token.LPAR
2840             and gexp.type == syms.testlist_gexp
2841             and rpar.type == token.RPAR
2842         ):
2843             return False
2844
2845         return len(gexp.children) == 2 and gexp.children[1].type == token.COMMA
2846
2847     return (
2848         node.type in IMPLICIT_TUPLE
2849         and len(node.children) == 2
2850         and node.children[1].type == token.COMMA
2851     )
2852
2853
2854 def is_yield(node: LN) -> bool:
2855     """Return True if `node` holds a `yield` or `yield from` expression."""
2856     if node.type == syms.yield_expr:
2857         return True
2858
2859     if node.type == token.NAME and node.value == "yield":  # type: ignore
2860         return True
2861
2862     if node.type != syms.atom:
2863         return False
2864
2865     if len(node.children) != 3:
2866         return False
2867
2868     lpar, expr, rpar = node.children
2869     if lpar.type == token.LPAR and rpar.type == token.RPAR:
2870         return is_yield(expr)
2871
2872     return False
2873
2874
2875 def is_vararg(leaf: Leaf, within: Set[NodeType]) -> bool:
2876     """Return True if `leaf` is a star or double star in a vararg or kwarg.
2877
2878     If `within` includes VARARGS_PARENTS, this applies to function signatures.
2879     If `within` includes UNPACKING_PARENTS, it applies to right hand-side
2880     extended iterable unpacking (PEP 3132) and additional unpacking
2881     generalizations (PEP 448).
2882     """
2883     if leaf.type not in STARS or not leaf.parent:
2884         return False
2885
2886     p = leaf.parent
2887     if p.type == syms.star_expr:
2888         # Star expressions are also used as assignment targets in extended
2889         # iterable unpacking (PEP 3132).  See what its parent is instead.
2890         if not p.parent:
2891             return False
2892
2893         p = p.parent
2894
2895     return p.type in within
2896
2897
2898 def is_multiline_string(leaf: Leaf) -> bool:
2899     """Return True if `leaf` is a multiline string that actually spans many lines."""
2900     value = leaf.value.lstrip("furbFURB")
2901     return value[:3] in {'"""', "'''"} and "\n" in value
2902
2903
2904 def is_stub_suite(node: Node) -> bool:
2905     """Return True if `node` is a suite with a stub body."""
2906     if (
2907         len(node.children) != 4
2908         or node.children[0].type != token.NEWLINE
2909         or node.children[1].type != token.INDENT
2910         or node.children[3].type != token.DEDENT
2911     ):
2912         return False
2913
2914     return is_stub_body(node.children[2])
2915
2916
2917 def is_stub_body(node: LN) -> bool:
2918     """Return True if `node` is a simple statement containing an ellipsis."""
2919     if not isinstance(node, Node) or node.type != syms.simple_stmt:
2920         return False
2921
2922     if len(node.children) != 2:
2923         return False
2924
2925     child = node.children[0]
2926     return (
2927         child.type == syms.atom
2928         and len(child.children) == 3
2929         and all(leaf == Leaf(token.DOT, ".") for leaf in child.children)
2930     )
2931
2932
2933 def max_delimiter_priority_in_atom(node: LN) -> int:
2934     """Return maximum delimiter priority inside `node`.
2935
2936     This is specific to atoms with contents contained in a pair of parentheses.
2937     If `node` isn't an atom or there are no enclosing parentheses, returns 0.
2938     """
2939     if node.type != syms.atom:
2940         return 0
2941
2942     first = node.children[0]
2943     last = node.children[-1]
2944     if not (first.type == token.LPAR and last.type == token.RPAR):
2945         return 0
2946
2947     bt = BracketTracker()
2948     for c in node.children[1:-1]:
2949         if isinstance(c, Leaf):
2950             bt.mark(c)
2951         else:
2952             for leaf in c.leaves():
2953                 bt.mark(leaf)
2954     try:
2955         return bt.max_delimiter_priority()
2956
2957     except ValueError:
2958         return 0
2959
2960
2961 def ensure_visible(leaf: Leaf) -> None:
2962     """Make sure parentheses are visible.
2963
2964     They could be invisible as part of some statements (see
2965     :func:`normalize_invible_parens` and :func:`visit_import_from`).
2966     """
2967     if leaf.type == token.LPAR:
2968         leaf.value = "("
2969     elif leaf.type == token.RPAR:
2970         leaf.value = ")"
2971
2972
2973 def should_explode(line: Line, opening_bracket: Leaf) -> bool:
2974     """Should `line` immediately be split with `delimiter_split()` after RHS?"""
2975
2976     if not (
2977         opening_bracket.parent
2978         and opening_bracket.parent.type in {syms.atom, syms.import_from}
2979         and opening_bracket.value in "[{("
2980     ):
2981         return False
2982
2983     try:
2984         last_leaf = line.leaves[-1]
2985         exclude = {id(last_leaf)} if last_leaf.type == token.COMMA else set()
2986         max_priority = line.bracket_tracker.max_delimiter_priority(exclude=exclude)
2987     except (IndexError, ValueError):
2988         return False
2989
2990     return max_priority == COMMA_PRIORITY
2991
2992
2993 def is_python36(node: Node) -> bool:
2994     """Return True if the current file is using Python 3.6+ features.
2995
2996     Currently looking for:
2997     - f-strings;
2998     - underscores in numeric literals; and
2999     - trailing commas after * or ** in function signatures and calls.
3000     """
3001     for n in node.pre_order():
3002         if n.type == token.STRING:
3003             value_head = n.value[:2]  # type: ignore
3004             if value_head in {'f"', 'F"', "f'", "F'", "rf", "fr", "RF", "FR"}:
3005                 return True
3006
3007         elif n.type == token.NUMBER:
3008             if "_" in n.value:  # type: ignore
3009                 return True
3010
3011         elif (
3012             n.type in {syms.typedargslist, syms.arglist}
3013             and n.children
3014             and n.children[-1].type == token.COMMA
3015         ):
3016             for ch in n.children:
3017                 if ch.type in STARS:
3018                     return True
3019
3020                 if ch.type == syms.argument:
3021                     for argch in ch.children:
3022                         if argch.type in STARS:
3023                             return True
3024
3025     return False
3026
3027
3028 def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[LeafID]]:
3029     """Generate sets of closing bracket IDs that should be omitted in a RHS.
3030
3031     Brackets can be omitted if the entire trailer up to and including
3032     a preceding closing bracket fits in one line.
3033
3034     Yielded sets are cumulative (contain results of previous yields, too).  First
3035     set is empty.
3036     """
3037
3038     omit: Set[LeafID] = set()
3039     yield omit
3040
3041     length = 4 * line.depth
3042     opening_bracket = None
3043     closing_bracket = None
3044     inner_brackets: Set[LeafID] = set()
3045     for index, leaf, leaf_length in enumerate_with_length(line, reversed=True):
3046         length += leaf_length
3047         if length > line_length:
3048             break
3049
3050         has_inline_comment = leaf_length > len(leaf.value) + len(leaf.prefix)
3051         if leaf.type == STANDALONE_COMMENT or has_inline_comment:
3052             break
3053
3054         if opening_bracket:
3055             if leaf is opening_bracket:
3056                 opening_bracket = None
3057             elif leaf.type in CLOSING_BRACKETS:
3058                 inner_brackets.add(id(leaf))
3059         elif leaf.type in CLOSING_BRACKETS:
3060             if index > 0 and line.leaves[index - 1].type in OPENING_BRACKETS:
3061                 # Empty brackets would fail a split so treat them as "inner"
3062                 # brackets (e.g. only add them to the `omit` set if another
3063                 # pair of brackets was good enough.
3064                 inner_brackets.add(id(leaf))
3065                 continue
3066
3067             if closing_bracket:
3068                 omit.add(id(closing_bracket))
3069                 omit.update(inner_brackets)
3070                 inner_brackets.clear()
3071                 yield omit
3072
3073             if leaf.value:
3074                 opening_bracket = leaf.opening_bracket
3075                 closing_bracket = leaf
3076
3077
3078 def get_future_imports(node: Node) -> Set[str]:
3079     """Return a set of __future__ imports in the file."""
3080     imports: Set[str] = set()
3081
3082     def get_imports_from_children(children: List[LN]) -> Generator[str, None, None]:
3083         for child in children:
3084             if isinstance(child, Leaf):
3085                 if child.type == token.NAME:
3086                     yield child.value
3087             elif child.type == syms.import_as_name:
3088                 orig_name = child.children[0]
3089                 assert isinstance(orig_name, Leaf), "Invalid syntax parsing imports"
3090                 assert orig_name.type == token.NAME, "Invalid syntax parsing imports"
3091                 yield orig_name.value
3092             elif child.type == syms.import_as_names:
3093                 yield from get_imports_from_children(child.children)
3094             else:
3095                 assert False, "Invalid syntax parsing imports"
3096
3097     for child in node.children:
3098         if child.type != syms.simple_stmt:
3099             break
3100         first_child = child.children[0]
3101         if isinstance(first_child, Leaf):
3102             # Continue looking if we see a docstring; otherwise stop.
3103             if (
3104                 len(child.children) == 2
3105                 and first_child.type == token.STRING
3106                 and child.children[1].type == token.NEWLINE
3107             ):
3108                 continue
3109             else:
3110                 break
3111         elif first_child.type == syms.import_from:
3112             module_name = first_child.children[1]
3113             if not isinstance(module_name, Leaf) or module_name.value != "__future__":
3114                 break
3115             imports |= set(get_imports_from_children(first_child.children[3:]))
3116         else:
3117             break
3118     return imports
3119
3120
3121 def gen_python_files_in_dir(
3122     path: Path,
3123     root: Path,
3124     include: Pattern[str],
3125     exclude: Pattern[str],
3126     report: "Report",
3127 ) -> Iterator[Path]:
3128     """Generate all files under `path` whose paths are not excluded by the
3129     `exclude` regex, but are included by the `include` regex.
3130
3131     Symbolic links pointing outside of the `root` directory are ignored.
3132
3133     `report` is where output about exclusions goes.
3134     """
3135     assert root.is_absolute(), f"INTERNAL ERROR: `root` must be absolute but is {root}"
3136     for child in path.iterdir():
3137         try:
3138             normalized_path = "/" + child.resolve().relative_to(root).as_posix()
3139         except ValueError:
3140             if child.is_symlink():
3141                 report.path_ignored(
3142                     child, f"is a symbolic link that points outside {root}"
3143                 )
3144                 continue
3145
3146             raise
3147
3148         if child.is_dir():
3149             normalized_path += "/"
3150         exclude_match = exclude.search(normalized_path)
3151         if exclude_match and exclude_match.group(0):
3152             report.path_ignored(child, f"matches the --exclude regular expression")
3153             continue
3154
3155         if child.is_dir():
3156             yield from gen_python_files_in_dir(child, root, include, exclude, report)
3157
3158         elif child.is_file():
3159             include_match = include.search(normalized_path)
3160             if include_match:
3161                 yield child
3162
3163
3164 @lru_cache()
3165 def find_project_root(srcs: Iterable[str]) -> Path:
3166     """Return a directory containing .git, .hg, or pyproject.toml.
3167
3168     That directory can be one of the directories passed in `srcs` or their
3169     common parent.
3170
3171     If no directory in the tree contains a marker that would specify it's the
3172     project root, the root of the file system is returned.
3173     """
3174     if not srcs:
3175         return Path("/").resolve()
3176
3177     common_base = min(Path(src).resolve() for src in srcs)
3178     if common_base.is_dir():
3179         # Append a fake file so `parents` below returns `common_base_dir`, too.
3180         common_base /= "fake-file"
3181     for directory in common_base.parents:
3182         if (directory / ".git").is_dir():
3183             return directory
3184
3185         if (directory / ".hg").is_dir():
3186             return directory
3187
3188         if (directory / "pyproject.toml").is_file():
3189             return directory
3190
3191     return directory
3192
3193
3194 @dataclass
3195 class Report:
3196     """Provides a reformatting counter. Can be rendered with `str(report)`."""
3197
3198     check: bool = False
3199     quiet: bool = False
3200     verbose: bool = False
3201     change_count: int = 0
3202     same_count: int = 0
3203     failure_count: int = 0
3204
3205     def done(self, src: Path, changed: Changed) -> None:
3206         """Increment the counter for successful reformatting. Write out a message."""
3207         if changed is Changed.YES:
3208             reformatted = "would reformat" if self.check else "reformatted"
3209             if self.verbose or not self.quiet:
3210                 out(f"{reformatted} {src}")
3211             self.change_count += 1
3212         else:
3213             if self.verbose:
3214                 if changed is Changed.NO:
3215                     msg = f"{src} already well formatted, good job."
3216                 else:
3217                     msg = f"{src} wasn't modified on disk since last run."
3218                 out(msg, bold=False)
3219             self.same_count += 1
3220
3221     def failed(self, src: Path, message: str) -> None:
3222         """Increment the counter for failed reformatting. Write out a message."""
3223         err(f"error: cannot format {src}: {message}")
3224         self.failure_count += 1
3225
3226     def path_ignored(self, path: Path, message: str) -> None:
3227         if self.verbose:
3228             out(f"{path} ignored: {message}", bold=False)
3229
3230     @property
3231     def return_code(self) -> int:
3232         """Return the exit code that the app should use.
3233
3234         This considers the current state of changed files and failures:
3235         - if there were any failures, return 123;
3236         - if any files were changed and --check is being used, return 1;
3237         - otherwise return 0.
3238         """
3239         # According to http://tldp.org/LDP/abs/html/exitcodes.html starting with
3240         # 126 we have special return codes reserved by the shell.
3241         if self.failure_count:
3242             return 123
3243
3244         elif self.change_count and self.check:
3245             return 1
3246
3247         return 0
3248
3249     def __str__(self) -> str:
3250         """Render a color report of the current state.
3251
3252         Use `click.unstyle` to remove colors.
3253         """
3254         if self.check:
3255             reformatted = "would be reformatted"
3256             unchanged = "would be left unchanged"
3257             failed = "would fail to reformat"
3258         else:
3259             reformatted = "reformatted"
3260             unchanged = "left unchanged"
3261             failed = "failed to reformat"
3262         report = []
3263         if self.change_count:
3264             s = "s" if self.change_count > 1 else ""
3265             report.append(
3266                 click.style(f"{self.change_count} file{s} {reformatted}", bold=True)
3267             )
3268         if self.same_count:
3269             s = "s" if self.same_count > 1 else ""
3270             report.append(f"{self.same_count} file{s} {unchanged}")
3271         if self.failure_count:
3272             s = "s" if self.failure_count > 1 else ""
3273             report.append(
3274                 click.style(f"{self.failure_count} file{s} {failed}", fg="red")
3275             )
3276         return ", ".join(report) + "."
3277
3278
3279 def assert_equivalent(src: str, dst: str) -> None:
3280     """Raise AssertionError if `src` and `dst` aren't equivalent."""
3281
3282     import ast
3283     import traceback
3284
3285     def _v(node: ast.AST, depth: int = 0) -> Iterator[str]:
3286         """Simple visitor generating strings to compare ASTs by content."""
3287         yield f"{'  ' * depth}{node.__class__.__name__}("
3288
3289         for field in sorted(node._fields):
3290             try:
3291                 value = getattr(node, field)
3292             except AttributeError:
3293                 continue
3294
3295             yield f"{'  ' * (depth+1)}{field}="
3296
3297             if isinstance(value, list):
3298                 for item in value:
3299                     if isinstance(item, ast.AST):
3300                         yield from _v(item, depth + 2)
3301
3302             elif isinstance(value, ast.AST):
3303                 yield from _v(value, depth + 2)
3304
3305             else:
3306                 yield f"{'  ' * (depth+2)}{value!r},  # {value.__class__.__name__}"
3307
3308         yield f"{'  ' * depth})  # /{node.__class__.__name__}"
3309
3310     try:
3311         src_ast = ast.parse(src)
3312     except Exception as exc:
3313         major, minor = sys.version_info[:2]
3314         raise AssertionError(
3315             f"cannot use --safe with this file; failed to parse source file "
3316             f"with Python {major}.{minor}'s builtin AST. Re-run with --fast "
3317             f"or stop using deprecated Python 2 syntax. AST error message: {exc}"
3318         )
3319
3320     try:
3321         dst_ast = ast.parse(dst)
3322     except Exception as exc:
3323         log = dump_to_file("".join(traceback.format_tb(exc.__traceback__)), dst)
3324         raise AssertionError(
3325             f"INTERNAL ERROR: Black produced invalid code: {exc}. "
3326             f"Please report a bug on https://github.com/ambv/black/issues.  "
3327             f"This invalid output might be helpful: {log}"
3328         ) from None
3329
3330     src_ast_str = "\n".join(_v(src_ast))
3331     dst_ast_str = "\n".join(_v(dst_ast))
3332     if src_ast_str != dst_ast_str:
3333         log = dump_to_file(diff(src_ast_str, dst_ast_str, "src", "dst"))
3334         raise AssertionError(
3335             f"INTERNAL ERROR: Black produced code that is not equivalent to "
3336             f"the source.  "
3337             f"Please report a bug on https://github.com/ambv/black/issues.  "
3338             f"This diff might be helpful: {log}"
3339         ) from None
3340
3341
3342 def assert_stable(
3343     src: str, dst: str, line_length: int, mode: FileMode = FileMode.AUTO_DETECT
3344 ) -> None:
3345     """Raise AssertionError if `dst` reformats differently the second time."""
3346     newdst = format_str(dst, line_length=line_length, mode=mode)
3347     if dst != newdst:
3348         log = dump_to_file(
3349             diff(src, dst, "source", "first pass"),
3350             diff(dst, newdst, "first pass", "second pass"),
3351         )
3352         raise AssertionError(
3353             f"INTERNAL ERROR: Black produced different code on the second pass "
3354             f"of the formatter.  "
3355             f"Please report a bug on https://github.com/ambv/black/issues.  "
3356             f"This diff might be helpful: {log}"
3357         ) from None
3358
3359
3360 def dump_to_file(*output: str) -> str:
3361     """Dump `output` to a temporary file. Return path to the file."""
3362     import tempfile
3363
3364     with tempfile.NamedTemporaryFile(
3365         mode="w", prefix="blk_", suffix=".log", delete=False, encoding="utf8"
3366     ) as f:
3367         for lines in output:
3368             f.write(lines)
3369             if lines and lines[-1] != "\n":
3370                 f.write("\n")
3371     return f.name
3372
3373
3374 def diff(a: str, b: str, a_name: str, b_name: str) -> str:
3375     """Return a unified diff string between strings `a` and `b`."""
3376     import difflib
3377
3378     a_lines = [line + "\n" for line in a.split("\n")]
3379     b_lines = [line + "\n" for line in b.split("\n")]
3380     return "".join(
3381         difflib.unified_diff(a_lines, b_lines, fromfile=a_name, tofile=b_name, n=5)
3382     )
3383
3384
3385 def cancel(tasks: Iterable[asyncio.Task]) -> None:
3386     """asyncio signal handler that cancels all `tasks` and reports to stderr."""
3387     err("Aborted!")
3388     for task in tasks:
3389         task.cancel()
3390
3391
3392 def shutdown(loop: BaseEventLoop) -> None:
3393     """Cancel all pending tasks on `loop`, wait for them, and close the loop."""
3394     try:
3395         # This part is borrowed from asyncio/runners.py in Python 3.7b2.
3396         to_cancel = [task for task in asyncio.Task.all_tasks(loop) if not task.done()]
3397         if not to_cancel:
3398             return
3399
3400         for task in to_cancel:
3401             task.cancel()
3402         loop.run_until_complete(
3403             asyncio.gather(*to_cancel, loop=loop, return_exceptions=True)
3404         )
3405     finally:
3406         # `concurrent.futures.Future` objects cannot be cancelled once they
3407         # are already running. There might be some when the `shutdown()` happened.
3408         # Silence their logger's spew about the event loop being closed.
3409         cf_logger = logging.getLogger("concurrent.futures")
3410         cf_logger.setLevel(logging.CRITICAL)
3411         loop.close()
3412
3413
3414 def sub_twice(regex: Pattern[str], replacement: str, original: str) -> str:
3415     """Replace `regex` with `replacement` twice on `original`.
3416
3417     This is used by string normalization to perform replaces on
3418     overlapping matches.
3419     """
3420     return regex.sub(replacement, regex.sub(replacement, original))
3421
3422
3423 def re_compile_maybe_verbose(regex: str) -> Pattern[str]:
3424     """Compile a regular expression string in `regex`.
3425
3426     If it contains newlines, use verbose mode.
3427     """
3428     if "\n" in regex:
3429         regex = "(?x)" + regex
3430     return re.compile(regex)
3431
3432
3433 def enumerate_reversed(sequence: Sequence[T]) -> Iterator[Tuple[Index, T]]:
3434     """Like `reversed(enumerate(sequence))` if that were possible."""
3435     index = len(sequence) - 1
3436     for element in reversed(sequence):
3437         yield (index, element)
3438         index -= 1
3439
3440
3441 def enumerate_with_length(
3442     line: Line, reversed: bool = False
3443 ) -> Iterator[Tuple[Index, Leaf, int]]:
3444     """Return an enumeration of leaves with their length.
3445
3446     Stops prematurely on multiline strings and standalone comments.
3447     """
3448     op = cast(
3449         Callable[[Sequence[Leaf]], Iterator[Tuple[Index, Leaf]]],
3450         enumerate_reversed if reversed else enumerate,
3451     )
3452     for index, leaf in op(line.leaves):
3453         length = len(leaf.prefix) + len(leaf.value)
3454         if "\n" in leaf.value:
3455             return  # Multiline strings, we can't continue.
3456
3457         comment: Optional[Leaf]
3458         for comment in line.comments_after(leaf):
3459             length += len(comment.value)
3460
3461         yield index, leaf, length
3462
3463
3464 def is_line_short_enough(line: Line, *, line_length: int, line_str: str = "") -> bool:
3465     """Return True if `line` is no longer than `line_length`.
3466
3467     Uses the provided `line_str` rendering, if any, otherwise computes a new one.
3468     """
3469     if not line_str:
3470         line_str = str(line).strip("\n")
3471     return (
3472         len(line_str) <= line_length
3473         and "\n" not in line_str  # multiline strings
3474         and not line.contains_standalone_comments()
3475     )
3476
3477
3478 def can_be_split(line: Line) -> bool:
3479     """Return False if the line cannot be split *for sure*.
3480
3481     This is not an exhaustive search but a cheap heuristic that we can use to
3482     avoid some unfortunate formattings (mostly around wrapping unsplittable code
3483     in unnecessary parentheses).
3484     """
3485     leaves = line.leaves
3486     if len(leaves) < 2:
3487         return False
3488
3489     if leaves[0].type == token.STRING and leaves[1].type == token.DOT:
3490         call_count = 0
3491         dot_count = 0
3492         next = leaves[-1]
3493         for leaf in leaves[-2::-1]:
3494             if leaf.type in OPENING_BRACKETS:
3495                 if next.type not in CLOSING_BRACKETS:
3496                     return False
3497
3498                 call_count += 1
3499             elif leaf.type == token.DOT:
3500                 dot_count += 1
3501             elif leaf.type == token.NAME:
3502                 if not (next.type == token.DOT or next.type in OPENING_BRACKETS):
3503                     return False
3504
3505             elif leaf.type not in CLOSING_BRACKETS:
3506                 return False
3507
3508             if dot_count > 1 and call_count > 1:
3509                 return False
3510
3511     return True
3512
3513
3514 def can_omit_invisible_parens(line: Line, line_length: int) -> bool:
3515     """Does `line` have a shape safe to reformat without optional parens around it?
3516
3517     Returns True for only a subset of potentially nice looking formattings but
3518     the point is to not return false positives that end up producing lines that
3519     are too long.
3520     """
3521     bt = line.bracket_tracker
3522     if not bt.delimiters:
3523         # Without delimiters the optional parentheses are useless.
3524         return True
3525
3526     max_priority = bt.max_delimiter_priority()
3527     if bt.delimiter_count_with_priority(max_priority) > 1:
3528         # With more than one delimiter of a kind the optional parentheses read better.
3529         return False
3530
3531     if max_priority == DOT_PRIORITY:
3532         # A single stranded method call doesn't require optional parentheses.
3533         return True
3534
3535     assert len(line.leaves) >= 2, "Stranded delimiter"
3536
3537     first = line.leaves[0]
3538     second = line.leaves[1]
3539     penultimate = line.leaves[-2]
3540     last = line.leaves[-1]
3541
3542     # With a single delimiter, omit if the expression starts or ends with
3543     # a bracket.
3544     if first.type in OPENING_BRACKETS and second.type not in CLOSING_BRACKETS:
3545         remainder = False
3546         length = 4 * line.depth
3547         for _index, leaf, leaf_length in enumerate_with_length(line):
3548             if leaf.type in CLOSING_BRACKETS and leaf.opening_bracket is first:
3549                 remainder = True
3550             if remainder:
3551                 length += leaf_length
3552                 if length > line_length:
3553                     break
3554
3555                 if leaf.type in OPENING_BRACKETS:
3556                     # There are brackets we can further split on.
3557                     remainder = False
3558
3559         else:
3560             # checked the entire string and line length wasn't exceeded
3561             if len(line.leaves) == _index + 1:
3562                 return True
3563
3564         # Note: we are not returning False here because a line might have *both*
3565         # a leading opening bracket and a trailing closing bracket.  If the
3566         # opening bracket doesn't match our rule, maybe the closing will.
3567
3568     if (
3569         last.type == token.RPAR
3570         or last.type == token.RBRACE
3571         or (
3572             # don't use indexing for omitting optional parentheses;
3573             # it looks weird
3574             last.type == token.RSQB
3575             and last.parent
3576             and last.parent.type != syms.trailer
3577         )
3578     ):
3579         if penultimate.type in OPENING_BRACKETS:
3580             # Empty brackets don't help.
3581             return False
3582
3583         if is_multiline_string(first):
3584             # Additional wrapping of a multiline string in this situation is
3585             # unnecessary.
3586             return True
3587
3588         length = 4 * line.depth
3589         seen_other_brackets = False
3590         for _index, leaf, leaf_length in enumerate_with_length(line):
3591             length += leaf_length
3592             if leaf is last.opening_bracket:
3593                 if seen_other_brackets or length <= line_length:
3594                     return True
3595
3596             elif leaf.type in OPENING_BRACKETS:
3597                 # There are brackets we can further split on.
3598                 seen_other_brackets = True
3599
3600     return False
3601
3602
3603 def get_cache_file(line_length: int, mode: FileMode) -> Path:
3604     return CACHE_DIR / f"cache.{line_length}.{mode.value}.pickle"
3605
3606
3607 def read_cache(line_length: int, mode: FileMode) -> Cache:
3608     """Read the cache if it exists and is well formed.
3609
3610     If it is not well formed, the call to write_cache later should resolve the issue.
3611     """
3612     cache_file = get_cache_file(line_length, mode)
3613     if not cache_file.exists():
3614         return {}
3615
3616     with cache_file.open("rb") as fobj:
3617         try:
3618             cache: Cache = pickle.load(fobj)
3619         except pickle.UnpicklingError:
3620             return {}
3621
3622     return cache
3623
3624
3625 def get_cache_info(path: Path) -> CacheInfo:
3626     """Return the information used to check if a file is already formatted or not."""
3627     stat = path.stat()
3628     return stat.st_mtime, stat.st_size
3629
3630
3631 def filter_cached(cache: Cache, sources: Iterable[Path]) -> Tuple[Set[Path], Set[Path]]:
3632     """Split an iterable of paths in `sources` into two sets.
3633
3634     The first contains paths of files that modified on disk or are not in the
3635     cache. The other contains paths to non-modified files.
3636     """
3637     todo, done = set(), set()
3638     for src in sources:
3639         src = src.resolve()
3640         if cache.get(src) != get_cache_info(src):
3641             todo.add(src)
3642         else:
3643             done.add(src)
3644     return todo, done
3645
3646
3647 def write_cache(
3648     cache: Cache, sources: Iterable[Path], line_length: int, mode: FileMode
3649 ) -> None:
3650     """Update the cache file."""
3651     cache_file = get_cache_file(line_length, mode)
3652     try:
3653         if not CACHE_DIR.exists():
3654             CACHE_DIR.mkdir(parents=True)
3655         new_cache = {**cache, **{src.resolve(): get_cache_info(src) for src in sources}}
3656         with cache_file.open("wb") as fobj:
3657             pickle.dump(new_cache, fobj, protocol=pickle.HIGHEST_PROTOCOL)
3658     except OSError:
3659         pass
3660
3661
3662 def patch_click() -> None:
3663     """Make Click not crash.
3664
3665     On certain misconfigured environments, Python 3 selects the ASCII encoding as the
3666     default which restricts paths that it can access during the lifetime of the
3667     application.  Click refuses to work in this scenario by raising a RuntimeError.
3668
3669     In case of Black the likelihood that non-ASCII characters are going to be used in
3670     file paths is minimal since it's Python source code.  Moreover, this crash was
3671     spurious on Python 3.7 thanks to PEP 538 and PEP 540.
3672     """
3673     try:
3674         from click import core
3675         from click import _unicodefun  # type: ignore
3676     except ModuleNotFoundError:
3677         return
3678
3679     for module in (core, _unicodefun):
3680         if hasattr(module, "_verify_python3_env"):
3681             module._verify_python3_env = lambda: None
3682
3683
3684 def patched_main() -> None:
3685     freeze_support()
3686     patch_click()
3687     main()
3688
3689
3690 if __name__ == "__main__":
3691     patched_main()