]> 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:

Move delimiter token skipping to BracketTracker
[etc/vim.git] / black.py
1 #!/usr/bin/env python3
2
3 import asyncio
4 import pickle
5 from asyncio.base_events import BaseEventLoop
6 from concurrent.futures import Executor, ProcessPoolExecutor
7 from enum import Enum
8 from functools import partial, wraps
9 import keyword
10 import logging
11 from multiprocessing import Manager
12 import os
13 from pathlib import Path
14 import re
15 import tokenize
16 import signal
17 import sys
18 from typing import (
19     Any,
20     Callable,
21     Collection,
22     Dict,
23     Generic,
24     Iterable,
25     Iterator,
26     List,
27     Optional,
28     Pattern,
29     Set,
30     Tuple,
31     Type,
32     TypeVar,
33     Union,
34 )
35
36 from appdirs import user_cache_dir
37 from attr import dataclass, Factory
38 import click
39
40 # lib2to3 fork
41 from blib2to3.pytree import Node, Leaf, type_repr
42 from blib2to3 import pygram, pytree
43 from blib2to3.pgen2 import driver, token
44 from blib2to3.pgen2.parse import ParseError
45
46 __version__ = "18.4a2"
47 DEFAULT_LINE_LENGTH = 88
48 # types
49 syms = pygram.python_symbols
50 FileContent = str
51 Encoding = str
52 Depth = int
53 NodeType = int
54 LeafID = int
55 Priority = int
56 Index = int
57 LN = Union[Leaf, Node]
58 SplitFunc = Callable[["Line", bool], Iterator["Line"]]
59 Timestamp = float
60 FileSize = int
61 CacheInfo = Tuple[Timestamp, FileSize]
62 Cache = Dict[Path, CacheInfo]
63 out = partial(click.secho, bold=True, err=True)
64 err = partial(click.secho, fg="red", err=True)
65
66
67 class NothingChanged(UserWarning):
68     """Raised by :func:`format_file` when reformatted code is the same as source."""
69
70
71 class CannotSplit(Exception):
72     """A readable split that fits the allotted line length is impossible.
73
74     Raised by :func:`left_hand_split`, :func:`right_hand_split`, and
75     :func:`delimiter_split`.
76     """
77
78
79 class FormatError(Exception):
80     """Base exception for `# fmt: on` and `# fmt: off` handling.
81
82     It holds the number of bytes of the prefix consumed before the format
83     control comment appeared.
84     """
85
86     def __init__(self, consumed: int) -> None:
87         super().__init__(consumed)
88         self.consumed = consumed
89
90     def trim_prefix(self, leaf: Leaf) -> None:
91         leaf.prefix = leaf.prefix[self.consumed:]
92
93     def leaf_from_consumed(self, leaf: Leaf) -> Leaf:
94         """Returns a new Leaf from the consumed part of the prefix."""
95         unformatted_prefix = leaf.prefix[:self.consumed]
96         return Leaf(token.NEWLINE, unformatted_prefix)
97
98
99 class FormatOn(FormatError):
100     """Found a comment like `# fmt: on` in the file."""
101
102
103 class FormatOff(FormatError):
104     """Found a comment like `# fmt: off` in the file."""
105
106
107 class WriteBack(Enum):
108     NO = 0
109     YES = 1
110     DIFF = 2
111
112
113 class Changed(Enum):
114     NO = 0
115     CACHED = 1
116     YES = 2
117
118
119 @click.command()
120 @click.option(
121     "-l",
122     "--line-length",
123     type=int,
124     default=DEFAULT_LINE_LENGTH,
125     help="How many character per line to allow.",
126     show_default=True,
127 )
128 @click.option(
129     "--check",
130     is_flag=True,
131     help=(
132         "Don't write the files back, just return the status.  Return code 0 "
133         "means nothing would change.  Return code 1 means some files would be "
134         "reformatted.  Return code 123 means there was an internal error."
135     ),
136 )
137 @click.option(
138     "--diff",
139     is_flag=True,
140     help="Don't write the files back, just output a diff for each file on stdout.",
141 )
142 @click.option(
143     "--fast/--safe",
144     is_flag=True,
145     help="If --fast given, skip temporary sanity checks. [default: --safe]",
146 )
147 @click.option(
148     "-q",
149     "--quiet",
150     is_flag=True,
151     help=(
152         "Don't emit non-error messages to stderr. Errors are still emitted, "
153         "silence those with 2>/dev/null."
154     ),
155 )
156 @click.version_option(version=__version__)
157 @click.argument(
158     "src",
159     nargs=-1,
160     type=click.Path(
161         exists=True, file_okay=True, dir_okay=True, readable=True, allow_dash=True
162     ),
163 )
164 @click.pass_context
165 def main(
166     ctx: click.Context,
167     line_length: int,
168     check: bool,
169     diff: bool,
170     fast: bool,
171     quiet: bool,
172     src: List[str],
173 ) -> None:
174     """The uncompromising code formatter."""
175     sources: List[Path] = []
176     for s in src:
177         p = Path(s)
178         if p.is_dir():
179             sources.extend(gen_python_files_in_dir(p))
180         elif p.is_file():
181             # if a file was explicitly given, we don't care about its extension
182             sources.append(p)
183         elif s == "-":
184             sources.append(Path("-"))
185         else:
186             err(f"invalid path: {s}")
187     if check and diff:
188         exc = click.ClickException("Options --check and --diff are mutually exclusive")
189         exc.exit_code = 2
190         raise exc
191
192     if check:
193         write_back = WriteBack.NO
194     elif diff:
195         write_back = WriteBack.DIFF
196     else:
197         write_back = WriteBack.YES
198     if len(sources) == 0:
199         ctx.exit(0)
200         return
201
202     elif len(sources) == 1:
203         return_code = reformat_one(sources[0], line_length, fast, quiet, write_back)
204     else:
205         loop = asyncio.get_event_loop()
206         executor = ProcessPoolExecutor(max_workers=os.cpu_count())
207         return_code = 1
208         try:
209             return_code = loop.run_until_complete(
210                 schedule_formatting(
211                     sources, line_length, write_back, fast, quiet, loop, executor
212                 )
213             )
214         finally:
215             shutdown(loop)
216     ctx.exit(return_code)
217
218
219 def reformat_one(
220     src: Path, line_length: int, fast: bool, quiet: bool, write_back: WriteBack
221 ) -> int:
222     """Reformat a single file under `src` without spawning child processes.
223
224     If `quiet` is True, non-error messages are not output. `line_length`,
225     `write_back`, and `fast` options are passed to :func:`format_file_in_place`.
226     """
227     report = Report(check=write_back is WriteBack.NO, quiet=quiet)
228     try:
229         changed = Changed.NO
230         if not src.is_file() and str(src) == "-":
231             if format_stdin_to_stdout(
232                 line_length=line_length, fast=fast, write_back=write_back
233             ):
234                 changed = Changed.YES
235         else:
236             cache: Cache = {}
237             if write_back != WriteBack.DIFF:
238                 cache = read_cache()
239                 src = src.resolve()
240                 if src in cache and cache[src] == get_cache_info(src):
241                     changed = Changed.CACHED
242             if (
243                 changed is not Changed.CACHED
244                 and format_file_in_place(
245                     src, line_length=line_length, fast=fast, write_back=write_back
246                 )
247             ):
248                 changed = Changed.YES
249             if write_back != WriteBack.DIFF and changed is not Changed.NO:
250                 write_cache(cache, [src])
251         report.done(src, changed)
252     except Exception as exc:
253         report.failed(src, str(exc))
254     return report.return_code
255
256
257 async def schedule_formatting(
258     sources: List[Path],
259     line_length: int,
260     write_back: WriteBack,
261     fast: bool,
262     quiet: bool,
263     loop: BaseEventLoop,
264     executor: Executor,
265 ) -> int:
266     """Run formatting of `sources` in parallel using the provided `executor`.
267
268     (Use ProcessPoolExecutors for actual parallelism.)
269
270     `line_length`, `write_back`, and `fast` options are passed to
271     :func:`format_file_in_place`.
272     """
273     report = Report(check=write_back is WriteBack.NO, quiet=quiet)
274     cache: Cache = {}
275     if write_back != WriteBack.DIFF:
276         cache = read_cache()
277         sources, cached = filter_cached(cache, sources)
278         for src in cached:
279             report.done(src, Changed.CACHED)
280     cancelled = []
281     formatted = []
282     if sources:
283         lock = None
284         if write_back == WriteBack.DIFF:
285             # For diff output, we need locks to ensure we don't interleave output
286             # from different processes.
287             manager = Manager()
288             lock = manager.Lock()
289         tasks = {
290             src: loop.run_in_executor(
291                 executor, format_file_in_place, src, line_length, fast, write_back, lock
292             )
293             for src in sources
294         }
295         _task_values = list(tasks.values())
296         try:
297             loop.add_signal_handler(signal.SIGINT, cancel, _task_values)
298             loop.add_signal_handler(signal.SIGTERM, cancel, _task_values)
299         except NotImplementedError:
300             # There are no good alternatives for these on Windows
301             pass
302         await asyncio.wait(_task_values)
303         for src, task in tasks.items():
304             if not task.done():
305                 report.failed(src, "timed out, cancelling")
306                 task.cancel()
307                 cancelled.append(task)
308             elif task.cancelled():
309                 cancelled.append(task)
310             elif task.exception():
311                 report.failed(src, str(task.exception()))
312             else:
313                 formatted.append(src)
314                 report.done(src, Changed.YES if task.result() else Changed.NO)
315
316     if cancelled:
317         await asyncio.gather(*cancelled, loop=loop, return_exceptions=True)
318     elif not quiet:
319         out("All done! ✨ 🍰 ✨")
320     if not quiet:
321         click.echo(str(report))
322
323     if write_back != WriteBack.DIFF and formatted:
324         write_cache(cache, formatted)
325
326     return report.return_code
327
328
329 def format_file_in_place(
330     src: Path,
331     line_length: int,
332     fast: bool,
333     write_back: WriteBack = WriteBack.NO,
334     lock: Any = None,  # multiprocessing.Manager().Lock() is some crazy proxy
335 ) -> bool:
336     """Format file under `src` path. Return True if changed.
337
338     If `write_back` is True, write reformatted code back to stdout.
339     `line_length` and `fast` options are passed to :func:`format_file_contents`.
340     """
341
342     with tokenize.open(src) as src_buffer:
343         src_contents = src_buffer.read()
344     try:
345         dst_contents = format_file_contents(
346             src_contents, line_length=line_length, fast=fast
347         )
348     except NothingChanged:
349         return False
350
351     if write_back == write_back.YES:
352         with open(src, "w", encoding=src_buffer.encoding) as f:
353             f.write(dst_contents)
354     elif write_back == write_back.DIFF:
355         src_name = f"{src.name}  (original)"
356         dst_name = f"{src.name}  (formatted)"
357         diff_contents = diff(src_contents, dst_contents, src_name, dst_name)
358         if lock:
359             lock.acquire()
360         try:
361             sys.stdout.write(diff_contents)
362         finally:
363             if lock:
364                 lock.release()
365     return True
366
367
368 def format_stdin_to_stdout(
369     line_length: int, fast: bool, write_back: WriteBack = WriteBack.NO
370 ) -> bool:
371     """Format file on stdin. Return True if changed.
372
373     If `write_back` is True, write reformatted code back to stdout.
374     `line_length` and `fast` arguments are passed to :func:`format_file_contents`.
375     """
376     src = sys.stdin.read()
377     dst = src
378     try:
379         dst = format_file_contents(src, line_length=line_length, fast=fast)
380         return True
381
382     except NothingChanged:
383         return False
384
385     finally:
386         if write_back == WriteBack.YES:
387             sys.stdout.write(dst)
388         elif write_back == WriteBack.DIFF:
389             src_name = "<stdin>  (original)"
390             dst_name = "<stdin>  (formatted)"
391             sys.stdout.write(diff(src, dst, src_name, dst_name))
392
393
394 def format_file_contents(
395     src_contents: str, line_length: int, fast: bool
396 ) -> FileContent:
397     """Reformat contents a file and return new contents.
398
399     If `fast` is False, additionally confirm that the reformatted code is
400     valid by calling :func:`assert_equivalent` and :func:`assert_stable` on it.
401     `line_length` is passed to :func:`format_str`.
402     """
403     if src_contents.strip() == "":
404         raise NothingChanged
405
406     dst_contents = format_str(src_contents, line_length=line_length)
407     if src_contents == dst_contents:
408         raise NothingChanged
409
410     if not fast:
411         assert_equivalent(src_contents, dst_contents)
412         assert_stable(src_contents, dst_contents, line_length=line_length)
413     return dst_contents
414
415
416 def format_str(src_contents: str, line_length: int) -> FileContent:
417     """Reformat a string and return new contents.
418
419     `line_length` determines how many characters per line are allowed.
420     """
421     src_node = lib2to3_parse(src_contents)
422     dst_contents = ""
423     lines = LineGenerator()
424     elt = EmptyLineTracker()
425     py36 = is_python36(src_node)
426     empty_line = Line()
427     after = 0
428     for current_line in lines.visit(src_node):
429         for _ in range(after):
430             dst_contents += str(empty_line)
431         before, after = elt.maybe_empty_lines(current_line)
432         for _ in range(before):
433             dst_contents += str(empty_line)
434         for line in split_line(current_line, line_length=line_length, py36=py36):
435             dst_contents += str(line)
436     return dst_contents
437
438
439 GRAMMARS = [
440     pygram.python_grammar_no_print_statement_no_exec_statement,
441     pygram.python_grammar_no_print_statement,
442     pygram.python_grammar_no_exec_statement,
443     pygram.python_grammar,
444 ]
445
446
447 def lib2to3_parse(src_txt: str) -> Node:
448     """Given a string with source, return the lib2to3 Node."""
449     grammar = pygram.python_grammar_no_print_statement
450     if src_txt[-1] != "\n":
451         nl = "\r\n" if "\r\n" in src_txt[:1024] else "\n"
452         src_txt += nl
453     for grammar in GRAMMARS:
454         drv = driver.Driver(grammar, pytree.convert)
455         try:
456             result = drv.parse_string(src_txt, True)
457             break
458
459         except ParseError as pe:
460             lineno, column = pe.context[1]
461             lines = src_txt.splitlines()
462             try:
463                 faulty_line = lines[lineno - 1]
464             except IndexError:
465                 faulty_line = "<line number missing in source>"
466             exc = ValueError(f"Cannot parse: {lineno}:{column}: {faulty_line}")
467     else:
468         raise exc from None
469
470     if isinstance(result, Leaf):
471         result = Node(syms.file_input, [result])
472     return result
473
474
475 def lib2to3_unparse(node: Node) -> str:
476     """Given a lib2to3 node, return its string representation."""
477     code = str(node)
478     return code
479
480
481 T = TypeVar("T")
482
483
484 class Visitor(Generic[T]):
485     """Basic lib2to3 visitor that yields things of type `T` on `visit()`."""
486
487     def visit(self, node: LN) -> Iterator[T]:
488         """Main method to visit `node` and its children.
489
490         It tries to find a `visit_*()` method for the given `node.type`, like
491         `visit_simple_stmt` for Node objects or `visit_INDENT` for Leaf objects.
492         If no dedicated `visit_*()` method is found, chooses `visit_default()`
493         instead.
494
495         Then yields objects of type `T` from the selected visitor.
496         """
497         if node.type < 256:
498             name = token.tok_name[node.type]
499         else:
500             name = type_repr(node.type)
501         yield from getattr(self, f"visit_{name}", self.visit_default)(node)
502
503     def visit_default(self, node: LN) -> Iterator[T]:
504         """Default `visit_*()` implementation. Recurses to children of `node`."""
505         if isinstance(node, Node):
506             for child in node.children:
507                 yield from self.visit(child)
508
509
510 @dataclass
511 class DebugVisitor(Visitor[T]):
512     tree_depth: int = 0
513
514     def visit_default(self, node: LN) -> Iterator[T]:
515         indent = " " * (2 * self.tree_depth)
516         if isinstance(node, Node):
517             _type = type_repr(node.type)
518             out(f"{indent}{_type}", fg="yellow")
519             self.tree_depth += 1
520             for child in node.children:
521                 yield from self.visit(child)
522
523             self.tree_depth -= 1
524             out(f"{indent}/{_type}", fg="yellow", bold=False)
525         else:
526             _type = token.tok_name.get(node.type, str(node.type))
527             out(f"{indent}{_type}", fg="blue", nl=False)
528             if node.prefix:
529                 # We don't have to handle prefixes for `Node` objects since
530                 # that delegates to the first child anyway.
531                 out(f" {node.prefix!r}", fg="green", bold=False, nl=False)
532             out(f" {node.value!r}", fg="blue", bold=False)
533
534     @classmethod
535     def show(cls, code: str) -> None:
536         """Pretty-print the lib2to3 AST of a given string of `code`.
537
538         Convenience method for debugging.
539         """
540         v: DebugVisitor[None] = DebugVisitor()
541         list(v.visit(lib2to3_parse(code)))
542
543
544 KEYWORDS = set(keyword.kwlist)
545 WHITESPACE = {token.DEDENT, token.INDENT, token.NEWLINE}
546 FLOW_CONTROL = {"return", "raise", "break", "continue"}
547 STATEMENT = {
548     syms.if_stmt,
549     syms.while_stmt,
550     syms.for_stmt,
551     syms.try_stmt,
552     syms.except_clause,
553     syms.with_stmt,
554     syms.funcdef,
555     syms.classdef,
556 }
557 STANDALONE_COMMENT = 153
558 LOGIC_OPERATORS = {"and", "or"}
559 COMPARATORS = {
560     token.LESS,
561     token.GREATER,
562     token.EQEQUAL,
563     token.NOTEQUAL,
564     token.LESSEQUAL,
565     token.GREATEREQUAL,
566 }
567 MATH_OPERATORS = {
568     token.PLUS,
569     token.MINUS,
570     token.STAR,
571     token.SLASH,
572     token.VBAR,
573     token.AMPER,
574     token.PERCENT,
575     token.CIRCUMFLEX,
576     token.TILDE,
577     token.LEFTSHIFT,
578     token.RIGHTSHIFT,
579     token.DOUBLESTAR,
580     token.DOUBLESLASH,
581 }
582 STARS = {token.STAR, token.DOUBLESTAR}
583 VARARGS_PARENTS = {
584     syms.arglist,
585     syms.argument,  # double star in arglist
586     syms.trailer,  # single argument to call
587     syms.typedargslist,
588     syms.varargslist,  # lambdas
589 }
590 UNPACKING_PARENTS = {
591     syms.atom,  # single element of a list or set literal
592     syms.dictsetmaker,
593     syms.listmaker,
594     syms.testlist_gexp,
595 }
596 COMPREHENSION_PRIORITY = 20
597 COMMA_PRIORITY = 10
598 LOGIC_PRIORITY = 5
599 STRING_PRIORITY = 4
600 COMPARATOR_PRIORITY = 3
601 MATH_PRIORITY = 1
602
603
604 @dataclass
605 class BracketTracker:
606     """Keeps track of brackets on a line."""
607
608     depth: int = 0
609     bracket_match: Dict[Tuple[Depth, NodeType], Leaf] = Factory(dict)
610     delimiters: Dict[LeafID, Priority] = Factory(dict)
611     previous: Optional[Leaf] = None
612     _for_loop_variable: bool = False
613     _lambda_arguments: bool = False
614
615     def mark(self, leaf: Leaf) -> None:
616         """Mark `leaf` with bracket-related metadata. Keep track of delimiters.
617
618         All leaves receive an int `bracket_depth` field that stores how deep
619         within brackets a given leaf is. 0 means there are no enclosing brackets
620         that started on this line.
621
622         If a leaf is itself a closing bracket, it receives an `opening_bracket`
623         field that it forms a pair with. This is a one-directional link to
624         avoid reference cycles.
625
626         If a leaf is a delimiter (a token on which Black can split the line if
627         needed) and it's on depth 0, its `id()` is stored in the tracker's
628         `delimiters` field.
629         """
630         if leaf.type == token.COMMENT:
631             return
632
633         self.maybe_decrement_after_for_loop_variable(leaf)
634         self.maybe_decrement_after_lambda_arguments(leaf)
635         if leaf.type in CLOSING_BRACKETS:
636             self.depth -= 1
637             opening_bracket = self.bracket_match.pop((self.depth, leaf.type))
638             leaf.opening_bracket = opening_bracket
639         leaf.bracket_depth = self.depth
640         if self.depth == 0:
641             delim = is_split_before_delimiter(leaf, self.previous)
642             if delim and self.previous is not None:
643                 self.delimiters[id(self.previous)] = delim
644             else:
645                 delim = is_split_after_delimiter(leaf, self.previous)
646                 if delim:
647                     self.delimiters[id(leaf)] = delim
648         if leaf.type in OPENING_BRACKETS:
649             self.bracket_match[self.depth, BRACKET[leaf.type]] = leaf
650             self.depth += 1
651         self.previous = leaf
652         self.maybe_increment_lambda_arguments(leaf)
653         self.maybe_increment_for_loop_variable(leaf)
654
655     def any_open_brackets(self) -> bool:
656         """Return True if there is an yet unmatched open bracket on the line."""
657         return bool(self.bracket_match)
658
659     def max_delimiter_priority(self, exclude: Iterable[LeafID] = ()) -> int:
660         """Return the highest priority of a delimiter found on the line.
661
662         Values are consistent with what `is_split_*_delimiter()` return.
663         Raises ValueError on no delimiters.
664         """
665         return max(v for k, v in self.delimiters.items() if k not in exclude)
666
667     def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
668         """In a for loop, or comprehension, the variables are often unpacks.
669
670         To avoid splitting on the comma in this situation, increase the depth of
671         tokens between `for` and `in`.
672         """
673         if leaf.type == token.NAME and leaf.value == "for":
674             self.depth += 1
675             self._for_loop_variable = True
676             return True
677
678         return False
679
680     def maybe_decrement_after_for_loop_variable(self, leaf: Leaf) -> bool:
681         """See `maybe_increment_for_loop_variable` above for explanation."""
682         if self._for_loop_variable and leaf.type == token.NAME and leaf.value == "in":
683             self.depth -= 1
684             self._for_loop_variable = False
685             return True
686
687         return False
688
689     def maybe_increment_lambda_arguments(self, leaf: Leaf) -> bool:
690         """In a lambda expression, there might be more than one argument.
691
692         To avoid splitting on the comma in this situation, increase the depth of
693         tokens between `lambda` and `:`.
694         """
695         if leaf.type == token.NAME and leaf.value == "lambda":
696             self.depth += 1
697             self._lambda_arguments = True
698             return True
699
700         return False
701
702     def maybe_decrement_after_lambda_arguments(self, leaf: Leaf) -> bool:
703         """See `maybe_increment_lambda_arguments` above for explanation."""
704         if self._lambda_arguments and leaf.type == token.COLON:
705             self.depth -= 1
706             self._lambda_arguments = False
707             return True
708
709         return False
710
711
712 @dataclass
713 class Line:
714     """Holds leaves and comments. Can be printed with `str(line)`."""
715
716     depth: int = 0
717     leaves: List[Leaf] = Factory(list)
718     comments: List[Tuple[Index, Leaf]] = Factory(list)
719     bracket_tracker: BracketTracker = Factory(BracketTracker)
720     inside_brackets: bool = False
721
722     def append(self, leaf: Leaf, preformatted: bool = False) -> None:
723         """Add a new `leaf` to the end of the line.
724
725         Unless `preformatted` is True, the `leaf` will receive a new consistent
726         whitespace prefix and metadata applied by :class:`BracketTracker`.
727         Trailing commas are maybe removed, unpacked for loop variables are
728         demoted from being delimiters.
729
730         Inline comments are put aside.
731         """
732         has_value = leaf.type in BRACKETS or bool(leaf.value.strip())
733         if not has_value:
734             return
735
736         if self.leaves and not preformatted:
737             # Note: at this point leaf.prefix should be empty except for
738             # imports, for which we only preserve newlines.
739             leaf.prefix += whitespace(leaf)
740         if self.inside_brackets or not preformatted:
741             self.bracket_tracker.mark(leaf)
742             self.maybe_remove_trailing_comma(leaf)
743
744         if not self.append_comment(leaf):
745             self.leaves.append(leaf)
746
747     def append_safe(self, leaf: Leaf, preformatted: bool = False) -> None:
748         """Like :func:`append()` but disallow invalid standalone comment structure.
749
750         Raises ValueError when any `leaf` is appended after a standalone comment
751         or when a standalone comment is not the first leaf on the line.
752         """
753         if self.bracket_tracker.depth == 0:
754             if self.is_comment:
755                 raise ValueError("cannot append to standalone comments")
756
757             if self.leaves and leaf.type == STANDALONE_COMMENT:
758                 raise ValueError(
759                     "cannot append standalone comments to a populated line"
760                 )
761
762         self.append(leaf, preformatted=preformatted)
763
764     @property
765     def is_comment(self) -> bool:
766         """Is this line a standalone comment?"""
767         return len(self.leaves) == 1 and self.leaves[0].type == STANDALONE_COMMENT
768
769     @property
770     def is_decorator(self) -> bool:
771         """Is this line a decorator?"""
772         return bool(self) and self.leaves[0].type == token.AT
773
774     @property
775     def is_import(self) -> bool:
776         """Is this an import line?"""
777         return bool(self) and is_import(self.leaves[0])
778
779     @property
780     def is_class(self) -> bool:
781         """Is this line a class definition?"""
782         return (
783             bool(self)
784             and self.leaves[0].type == token.NAME
785             and self.leaves[0].value == "class"
786         )
787
788     @property
789     def is_def(self) -> bool:
790         """Is this a function definition? (Also returns True for async defs.)"""
791         try:
792             first_leaf = self.leaves[0]
793         except IndexError:
794             return False
795
796         try:
797             second_leaf: Optional[Leaf] = self.leaves[1]
798         except IndexError:
799             second_leaf = None
800         return (
801             (first_leaf.type == token.NAME and first_leaf.value == "def")
802             or (
803                 first_leaf.type == token.ASYNC
804                 and second_leaf is not None
805                 and second_leaf.type == token.NAME
806                 and second_leaf.value == "def"
807             )
808         )
809
810     @property
811     def is_flow_control(self) -> bool:
812         """Is this line a flow control statement?
813
814         Those are `return`, `raise`, `break`, and `continue`.
815         """
816         return (
817             bool(self)
818             and self.leaves[0].type == token.NAME
819             and self.leaves[0].value in FLOW_CONTROL
820         )
821
822     @property
823     def is_yield(self) -> bool:
824         """Is this line a yield statement?"""
825         return (
826             bool(self)
827             and self.leaves[0].type == token.NAME
828             and self.leaves[0].value == "yield"
829         )
830
831     def contains_standalone_comments(self, depth_limit: int = sys.maxsize) -> bool:
832         """If so, needs to be split before emitting."""
833         for leaf in self.leaves:
834             if leaf.type == STANDALONE_COMMENT:
835                 if leaf.bracket_depth <= depth_limit:
836                     return True
837
838         return False
839
840     def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
841         """Remove trailing comma if there is one and it's safe."""
842         if not (
843             self.leaves
844             and self.leaves[-1].type == token.COMMA
845             and closing.type in CLOSING_BRACKETS
846         ):
847             return False
848
849         if closing.type == token.RBRACE:
850             self.remove_trailing_comma()
851             return True
852
853         if closing.type == token.RSQB:
854             comma = self.leaves[-1]
855             if comma.parent and comma.parent.type == syms.listmaker:
856                 self.remove_trailing_comma()
857                 return True
858
859         # For parens let's check if it's safe to remove the comma.  If the
860         # trailing one is the only one, we might mistakenly change a tuple
861         # into a different type by removing the comma.
862         depth = closing.bracket_depth + 1
863         commas = 0
864         opening = closing.opening_bracket
865         for _opening_index, leaf in enumerate(self.leaves):
866             if leaf is opening:
867                 break
868
869         else:
870             return False
871
872         for leaf in self.leaves[_opening_index + 1:]:
873             if leaf is closing:
874                 break
875
876             bracket_depth = leaf.bracket_depth
877             if bracket_depth == depth and leaf.type == token.COMMA:
878                 commas += 1
879                 if leaf.parent and leaf.parent.type == syms.arglist:
880                     commas += 1
881                     break
882
883         if commas > 1:
884             self.remove_trailing_comma()
885             return True
886
887         return False
888
889     def append_comment(self, comment: Leaf) -> bool:
890         """Add an inline or standalone comment to the line."""
891         if (
892             comment.type == STANDALONE_COMMENT
893             and self.bracket_tracker.any_open_brackets()
894         ):
895             comment.prefix = ""
896             return False
897
898         if comment.type != token.COMMENT:
899             return False
900
901         after = len(self.leaves) - 1
902         if after == -1:
903             comment.type = STANDALONE_COMMENT
904             comment.prefix = ""
905             return False
906
907         else:
908             self.comments.append((after, comment))
909             return True
910
911     def comments_after(self, leaf: Leaf) -> Iterator[Leaf]:
912         """Generate comments that should appear directly after `leaf`."""
913         for _leaf_index, _leaf in enumerate(self.leaves):
914             if leaf is _leaf:
915                 break
916
917         else:
918             return
919
920         for index, comment_after in self.comments:
921             if _leaf_index == index:
922                 yield comment_after
923
924     def remove_trailing_comma(self) -> None:
925         """Remove the trailing comma and moves the comments attached to it."""
926         comma_index = len(self.leaves) - 1
927         for i in range(len(self.comments)):
928             comment_index, comment = self.comments[i]
929             if comment_index == comma_index:
930                 self.comments[i] = (comma_index - 1, comment)
931         self.leaves.pop()
932
933     def __str__(self) -> str:
934         """Render the line."""
935         if not self:
936             return "\n"
937
938         indent = "    " * self.depth
939         leaves = iter(self.leaves)
940         first = next(leaves)
941         res = f"{first.prefix}{indent}{first.value}"
942         for leaf in leaves:
943             res += str(leaf)
944         for _, comment in self.comments:
945             res += str(comment)
946         return res + "\n"
947
948     def __bool__(self) -> bool:
949         """Return True if the line has leaves or comments."""
950         return bool(self.leaves or self.comments)
951
952
953 class UnformattedLines(Line):
954     """Just like :class:`Line` but stores lines which aren't reformatted."""
955
956     def append(self, leaf: Leaf, preformatted: bool = True) -> None:
957         """Just add a new `leaf` to the end of the lines.
958
959         The `preformatted` argument is ignored.
960
961         Keeps track of indentation `depth`, which is useful when the user
962         says `# fmt: on`. Otherwise, doesn't do anything with the `leaf`.
963         """
964         try:
965             list(generate_comments(leaf))
966         except FormatOn as f_on:
967             self.leaves.append(f_on.leaf_from_consumed(leaf))
968             raise
969
970         self.leaves.append(leaf)
971         if leaf.type == token.INDENT:
972             self.depth += 1
973         elif leaf.type == token.DEDENT:
974             self.depth -= 1
975
976     def __str__(self) -> str:
977         """Render unformatted lines from leaves which were added with `append()`.
978
979         `depth` is not used for indentation in this case.
980         """
981         if not self:
982             return "\n"
983
984         res = ""
985         for leaf in self.leaves:
986             res += str(leaf)
987         return res
988
989     def append_comment(self, comment: Leaf) -> bool:
990         """Not implemented in this class. Raises `NotImplementedError`."""
991         raise NotImplementedError("Unformatted lines don't store comments separately.")
992
993     def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
994         """Does nothing and returns False."""
995         return False
996
997     def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
998         """Does nothing and returns False."""
999         return False
1000
1001
1002 @dataclass
1003 class EmptyLineTracker:
1004     """Provides a stateful method that returns the number of potential extra
1005     empty lines needed before and after the currently processed line.
1006
1007     Note: this tracker works on lines that haven't been split yet.  It assumes
1008     the prefix of the first leaf consists of optional newlines.  Those newlines
1009     are consumed by `maybe_empty_lines()` and included in the computation.
1010     """
1011     previous_line: Optional[Line] = None
1012     previous_after: int = 0
1013     previous_defs: List[int] = Factory(list)
1014
1015     def maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
1016         """Return the number of extra empty lines before and after the `current_line`.
1017
1018         This is for separating `def`, `async def` and `class` with extra empty
1019         lines (two on module-level), as well as providing an extra empty line
1020         after flow control keywords to make them more prominent.
1021         """
1022         if isinstance(current_line, UnformattedLines):
1023             return 0, 0
1024
1025         before, after = self._maybe_empty_lines(current_line)
1026         before -= self.previous_after
1027         self.previous_after = after
1028         self.previous_line = current_line
1029         return before, after
1030
1031     def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
1032         max_allowed = 1
1033         if current_line.depth == 0:
1034             max_allowed = 2
1035         if current_line.leaves:
1036             # Consume the first leaf's extra newlines.
1037             first_leaf = current_line.leaves[0]
1038             before = first_leaf.prefix.count("\n")
1039             before = min(before, max_allowed)
1040             first_leaf.prefix = ""
1041         else:
1042             before = 0
1043         depth = current_line.depth
1044         while self.previous_defs and self.previous_defs[-1] >= depth:
1045             self.previous_defs.pop()
1046             before = 1 if depth else 2
1047         is_decorator = current_line.is_decorator
1048         if is_decorator or current_line.is_def or current_line.is_class:
1049             if not is_decorator:
1050                 self.previous_defs.append(depth)
1051             if self.previous_line is None:
1052                 # Don't insert empty lines before the first line in the file.
1053                 return 0, 0
1054
1055             if self.previous_line and self.previous_line.is_decorator:
1056                 # Don't insert empty lines between decorators.
1057                 return 0, 0
1058
1059             newlines = 2
1060             if current_line.depth:
1061                 newlines -= 1
1062             return newlines, 0
1063
1064         if current_line.is_flow_control:
1065             return before, 1
1066
1067         if (
1068             self.previous_line
1069             and self.previous_line.is_import
1070             and not current_line.is_import
1071             and depth == self.previous_line.depth
1072         ):
1073             return (before or 1), 0
1074
1075         if (
1076             self.previous_line
1077             and self.previous_line.is_yield
1078             and (not current_line.is_yield or depth != self.previous_line.depth)
1079         ):
1080             return (before or 1), 0
1081
1082         return before, 0
1083
1084
1085 @dataclass
1086 class LineGenerator(Visitor[Line]):
1087     """Generates reformatted Line objects.  Empty lines are not emitted.
1088
1089     Note: destroys the tree it's visiting by mutating prefixes of its leaves
1090     in ways that will no longer stringify to valid Python code on the tree.
1091     """
1092     current_line: Line = Factory(Line)
1093
1094     def line(self, indent: int = 0, type: Type[Line] = Line) -> Iterator[Line]:
1095         """Generate a line.
1096
1097         If the line is empty, only emit if it makes sense.
1098         If the line is too long, split it first and then generate.
1099
1100         If any lines were generated, set up a new current_line.
1101         """
1102         if not self.current_line:
1103             if self.current_line.__class__ == type:
1104                 self.current_line.depth += indent
1105             else:
1106                 self.current_line = type(depth=self.current_line.depth + indent)
1107             return  # Line is empty, don't emit. Creating a new one unnecessary.
1108
1109         complete_line = self.current_line
1110         self.current_line = type(depth=complete_line.depth + indent)
1111         yield complete_line
1112
1113     def visit(self, node: LN) -> Iterator[Line]:
1114         """Main method to visit `node` and its children.
1115
1116         Yields :class:`Line` objects.
1117         """
1118         if isinstance(self.current_line, UnformattedLines):
1119             # File contained `# fmt: off`
1120             yield from self.visit_unformatted(node)
1121
1122         else:
1123             yield from super().visit(node)
1124
1125     def visit_default(self, node: LN) -> Iterator[Line]:
1126         """Default `visit_*()` implementation. Recurses to children of `node`."""
1127         if isinstance(node, Leaf):
1128             any_open_brackets = self.current_line.bracket_tracker.any_open_brackets()
1129             try:
1130                 for comment in generate_comments(node):
1131                     if any_open_brackets:
1132                         # any comment within brackets is subject to splitting
1133                         self.current_line.append(comment)
1134                     elif comment.type == token.COMMENT:
1135                         # regular trailing comment
1136                         self.current_line.append(comment)
1137                         yield from self.line()
1138
1139                     else:
1140                         # regular standalone comment
1141                         yield from self.line()
1142
1143                         self.current_line.append(comment)
1144                         yield from self.line()
1145
1146             except FormatOff as f_off:
1147                 f_off.trim_prefix(node)
1148                 yield from self.line(type=UnformattedLines)
1149                 yield from self.visit(node)
1150
1151             except FormatOn as f_on:
1152                 # This only happens here if somebody says "fmt: on" multiple
1153                 # times in a row.
1154                 f_on.trim_prefix(node)
1155                 yield from self.visit_default(node)
1156
1157             else:
1158                 normalize_prefix(node, inside_brackets=any_open_brackets)
1159                 if node.type == token.STRING:
1160                     normalize_string_quotes(node)
1161                 if node.type not in WHITESPACE:
1162                     self.current_line.append(node)
1163         yield from super().visit_default(node)
1164
1165     def visit_INDENT(self, node: Node) -> Iterator[Line]:
1166         """Increase indentation level, maybe yield a line."""
1167         # In blib2to3 INDENT never holds comments.
1168         yield from self.line(+1)
1169         yield from self.visit_default(node)
1170
1171     def visit_DEDENT(self, node: Node) -> Iterator[Line]:
1172         """Decrease indentation level, maybe yield a line."""
1173         # DEDENT has no value. Additionally, in blib2to3 it never holds comments.
1174         yield from self.line(-1)
1175
1176     def visit_stmt(
1177         self, node: Node, keywords: Set[str], parens: Set[str]
1178     ) -> Iterator[Line]:
1179         """Visit a statement.
1180
1181         This implementation is shared for `if`, `while`, `for`, `try`, `except`,
1182         `def`, `with`, `class`, and `assert`.
1183
1184         The relevant Python language `keywords` for a given statement will be
1185         NAME leaves within it. This methods puts those on a separate line.
1186
1187         `parens` holds pairs of nodes where invisible parentheses should be put.
1188         Keys hold nodes after which opening parentheses should be put, values
1189         hold nodes before which closing parentheses should be put.
1190         """
1191         normalize_invisible_parens(node, parens_after=parens)
1192         for child in node.children:
1193             if child.type == token.NAME and child.value in keywords:  # type: ignore
1194                 yield from self.line()
1195
1196             yield from self.visit(child)
1197
1198     def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
1199         """Visit a statement without nested statements."""
1200         is_suite_like = node.parent and node.parent.type in STATEMENT
1201         if is_suite_like:
1202             yield from self.line(+1)
1203             yield from self.visit_default(node)
1204             yield from self.line(-1)
1205
1206         else:
1207             yield from self.line()
1208             yield from self.visit_default(node)
1209
1210     def visit_async_stmt(self, node: Node) -> Iterator[Line]:
1211         """Visit `async def`, `async for`, `async with`."""
1212         yield from self.line()
1213
1214         children = iter(node.children)
1215         for child in children:
1216             yield from self.visit(child)
1217
1218             if child.type == token.ASYNC:
1219                 break
1220
1221         internal_stmt = next(children)
1222         for child in internal_stmt.children:
1223             yield from self.visit(child)
1224
1225     def visit_decorators(self, node: Node) -> Iterator[Line]:
1226         """Visit decorators."""
1227         for child in node.children:
1228             yield from self.line()
1229             yield from self.visit(child)
1230
1231     def visit_import_from(self, node: Node) -> Iterator[Line]:
1232         """Visit import_from and maybe put invisible parentheses.
1233
1234         This is separate from `visit_stmt` because import statements don't
1235         support arbitrary atoms and thus handling of parentheses is custom.
1236         """
1237         check_lpar = False
1238         for index, child in enumerate(node.children):
1239             if check_lpar:
1240                 if child.type == token.LPAR:
1241                     # make parentheses invisible
1242                     child.value = ""  # type: ignore
1243                     node.children[-1].value = ""  # type: ignore
1244                 else:
1245                     # insert invisible parentheses
1246                     node.insert_child(index, Leaf(token.LPAR, ""))
1247                     node.append_child(Leaf(token.RPAR, ""))
1248                 break
1249
1250             check_lpar = (
1251                 child.type == token.NAME and child.value == "import"  # type: ignore
1252             )
1253
1254         for child in node.children:
1255             yield from self.visit(child)
1256
1257     def visit_SEMI(self, leaf: Leaf) -> Iterator[Line]:
1258         """Remove a semicolon and put the other statement on a separate line."""
1259         yield from self.line()
1260
1261     def visit_ENDMARKER(self, leaf: Leaf) -> Iterator[Line]:
1262         """End of file. Process outstanding comments and end with a newline."""
1263         yield from self.visit_default(leaf)
1264         yield from self.line()
1265
1266     def visit_unformatted(self, node: LN) -> Iterator[Line]:
1267         """Used when file contained a `# fmt: off`."""
1268         if isinstance(node, Node):
1269             for child in node.children:
1270                 yield from self.visit(child)
1271
1272         else:
1273             try:
1274                 self.current_line.append(node)
1275             except FormatOn as f_on:
1276                 f_on.trim_prefix(node)
1277                 yield from self.line()
1278                 yield from self.visit(node)
1279
1280             if node.type == token.ENDMARKER:
1281                 # somebody decided not to put a final `# fmt: on`
1282                 yield from self.line()
1283
1284     def __attrs_post_init__(self) -> None:
1285         """You are in a twisty little maze of passages."""
1286         v = self.visit_stmt
1287         Ø: Set[str] = set()
1288         self.visit_assert_stmt = partial(v, keywords={"assert"}, parens={"assert", ","})
1289         self.visit_if_stmt = partial(v, keywords={"if", "else", "elif"}, parens={"if"})
1290         self.visit_while_stmt = partial(v, keywords={"while", "else"}, parens={"while"})
1291         self.visit_for_stmt = partial(v, keywords={"for", "else"}, parens={"for", "in"})
1292         self.visit_try_stmt = partial(
1293             v, keywords={"try", "except", "else", "finally"}, parens=Ø
1294         )
1295         self.visit_except_clause = partial(v, keywords={"except"}, parens=Ø)
1296         self.visit_with_stmt = partial(v, keywords={"with"}, parens=Ø)
1297         self.visit_funcdef = partial(v, keywords={"def"}, parens=Ø)
1298         self.visit_classdef = partial(v, keywords={"class"}, parens=Ø)
1299         self.visit_async_funcdef = self.visit_async_stmt
1300         self.visit_decorated = self.visit_decorators
1301
1302
1303 IMPLICIT_TUPLE = {syms.testlist, syms.testlist_star_expr, syms.exprlist}
1304 BRACKET = {token.LPAR: token.RPAR, token.LSQB: token.RSQB, token.LBRACE: token.RBRACE}
1305 OPENING_BRACKETS = set(BRACKET.keys())
1306 CLOSING_BRACKETS = set(BRACKET.values())
1307 BRACKETS = OPENING_BRACKETS | CLOSING_BRACKETS
1308 ALWAYS_NO_SPACE = CLOSING_BRACKETS | {token.COMMA, STANDALONE_COMMENT}
1309
1310
1311 def whitespace(leaf: Leaf) -> str:  # noqa C901
1312     """Return whitespace prefix if needed for the given `leaf`."""
1313     NO = ""
1314     SPACE = " "
1315     DOUBLESPACE = "  "
1316     t = leaf.type
1317     p = leaf.parent
1318     v = leaf.value
1319     if t in ALWAYS_NO_SPACE:
1320         return NO
1321
1322     if t == token.COMMENT:
1323         return DOUBLESPACE
1324
1325     assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
1326     if t == token.COLON and p.type not in {syms.subscript, syms.subscriptlist}:
1327         return NO
1328
1329     prev = leaf.prev_sibling
1330     if not prev:
1331         prevp = preceding_leaf(p)
1332         if not prevp or prevp.type in OPENING_BRACKETS:
1333             return NO
1334
1335         if t == token.COLON:
1336             return SPACE if prevp.type == token.COMMA else NO
1337
1338         if prevp.type == token.EQUAL:
1339             if prevp.parent:
1340                 if prevp.parent.type in {
1341                     syms.arglist, syms.argument, syms.parameters, syms.varargslist
1342                 }:
1343                     return NO
1344
1345                 elif prevp.parent.type == syms.typedargslist:
1346                     # A bit hacky: if the equal sign has whitespace, it means we
1347                     # previously found it's a typed argument.  So, we're using
1348                     # that, too.
1349                     return prevp.prefix
1350
1351         elif prevp.type in STARS:
1352             if is_vararg(prevp, within=VARARGS_PARENTS | UNPACKING_PARENTS):
1353                 return NO
1354
1355         elif prevp.type == token.COLON:
1356             if prevp.parent and prevp.parent.type in {syms.subscript, syms.sliceop}:
1357                 return NO
1358
1359         elif (
1360             prevp.parent
1361             and prevp.parent.type == syms.factor
1362             and prevp.type in MATH_OPERATORS
1363         ):
1364             return NO
1365
1366         elif (
1367             prevp.type == token.RIGHTSHIFT
1368             and prevp.parent
1369             and prevp.parent.type == syms.shift_expr
1370             and prevp.prev_sibling
1371             and prevp.prev_sibling.type == token.NAME
1372             and prevp.prev_sibling.value == "print"  # type: ignore
1373         ):
1374             # Python 2 print chevron
1375             return NO
1376
1377     elif prev.type in OPENING_BRACKETS:
1378         return NO
1379
1380     if p.type in {syms.parameters, syms.arglist}:
1381         # untyped function signatures or calls
1382         if not prev or prev.type != token.COMMA:
1383             return NO
1384
1385     elif p.type == syms.varargslist:
1386         # lambdas
1387         if prev and prev.type != token.COMMA:
1388             return NO
1389
1390     elif p.type == syms.typedargslist:
1391         # typed function signatures
1392         if not prev:
1393             return NO
1394
1395         if t == token.EQUAL:
1396             if prev.type != syms.tname:
1397                 return NO
1398
1399         elif prev.type == token.EQUAL:
1400             # A bit hacky: if the equal sign has whitespace, it means we
1401             # previously found it's a typed argument.  So, we're using that, too.
1402             return prev.prefix
1403
1404         elif prev.type != token.COMMA:
1405             return NO
1406
1407     elif p.type == syms.tname:
1408         # type names
1409         if not prev:
1410             prevp = preceding_leaf(p)
1411             if not prevp or prevp.type != token.COMMA:
1412                 return NO
1413
1414     elif p.type == syms.trailer:
1415         # attributes and calls
1416         if t == token.LPAR or t == token.RPAR:
1417             return NO
1418
1419         if not prev:
1420             if t == token.DOT:
1421                 prevp = preceding_leaf(p)
1422                 if not prevp or prevp.type != token.NUMBER:
1423                     return NO
1424
1425             elif t == token.LSQB:
1426                 return NO
1427
1428         elif prev.type != token.COMMA:
1429             return NO
1430
1431     elif p.type == syms.argument:
1432         # single argument
1433         if t == token.EQUAL:
1434             return NO
1435
1436         if not prev:
1437             prevp = preceding_leaf(p)
1438             if not prevp or prevp.type == token.LPAR:
1439                 return NO
1440
1441         elif prev.type in {token.EQUAL} | STARS:
1442             return NO
1443
1444     elif p.type == syms.decorator:
1445         # decorators
1446         return NO
1447
1448     elif p.type == syms.dotted_name:
1449         if prev:
1450             return NO
1451
1452         prevp = preceding_leaf(p)
1453         if not prevp or prevp.type == token.AT or prevp.type == token.DOT:
1454             return NO
1455
1456     elif p.type == syms.classdef:
1457         if t == token.LPAR:
1458             return NO
1459
1460         if prev and prev.type == token.LPAR:
1461             return NO
1462
1463     elif p.type == syms.subscript:
1464         # indexing
1465         if not prev:
1466             assert p.parent is not None, "subscripts are always parented"
1467             if p.parent.type == syms.subscriptlist:
1468                 return SPACE
1469
1470             return NO
1471
1472         else:
1473             return NO
1474
1475     elif p.type == syms.atom:
1476         if prev and t == token.DOT:
1477             # dots, but not the first one.
1478             return NO
1479
1480     elif p.type == syms.dictsetmaker:
1481         # dict unpacking
1482         if prev and prev.type == token.DOUBLESTAR:
1483             return NO
1484
1485     elif p.type in {syms.factor, syms.star_expr}:
1486         # unary ops
1487         if not prev:
1488             prevp = preceding_leaf(p)
1489             if not prevp or prevp.type in OPENING_BRACKETS:
1490                 return NO
1491
1492             prevp_parent = prevp.parent
1493             assert prevp_parent is not None
1494             if (
1495                 prevp.type == token.COLON
1496                 and prevp_parent.type in {syms.subscript, syms.sliceop}
1497             ):
1498                 return NO
1499
1500             elif prevp.type == token.EQUAL and prevp_parent.type == syms.argument:
1501                 return NO
1502
1503         elif t == token.NAME or t == token.NUMBER:
1504             return NO
1505
1506     elif p.type == syms.import_from:
1507         if t == token.DOT:
1508             if prev and prev.type == token.DOT:
1509                 return NO
1510
1511         elif t == token.NAME:
1512             if v == "import":
1513                 return SPACE
1514
1515             if prev and prev.type == token.DOT:
1516                 return NO
1517
1518     elif p.type == syms.sliceop:
1519         return NO
1520
1521     return SPACE
1522
1523
1524 def preceding_leaf(node: Optional[LN]) -> Optional[Leaf]:
1525     """Return the first leaf that precedes `node`, if any."""
1526     while node:
1527         res = node.prev_sibling
1528         if res:
1529             if isinstance(res, Leaf):
1530                 return res
1531
1532             try:
1533                 return list(res.leaves())[-1]
1534
1535             except IndexError:
1536                 return None
1537
1538         node = node.parent
1539     return None
1540
1541
1542 def is_split_after_delimiter(leaf: Leaf, previous: Leaf = None) -> int:
1543     """Return the priority of the `leaf` delimiter, given a line break after it.
1544
1545     The delimiter priorities returned here are from those delimiters that would
1546     cause a line break after themselves.
1547
1548     Higher numbers are higher priority.
1549     """
1550     if leaf.type == token.COMMA:
1551         return COMMA_PRIORITY
1552
1553     return 0
1554
1555
1556 def is_split_before_delimiter(leaf: Leaf, previous: Leaf = None) -> int:
1557     """Return the priority of the `leaf` delimiter, given a line before after it.
1558
1559     The delimiter priorities returned here are from those delimiters that would
1560     cause a line break before themselves.
1561
1562     Higher numbers are higher priority.
1563     """
1564     if is_vararg(leaf, within=VARARGS_PARENTS | UNPACKING_PARENTS):
1565         # * and ** might also be MATH_OPERATORS but in this case they are not.
1566         # Don't treat them as a delimiter.
1567         return 0
1568
1569     if (
1570         leaf.type in MATH_OPERATORS
1571         and leaf.parent
1572         and leaf.parent.type not in {syms.factor, syms.star_expr}
1573     ):
1574         return MATH_PRIORITY
1575
1576     if leaf.type in COMPARATORS:
1577         return COMPARATOR_PRIORITY
1578
1579     if (
1580         leaf.type == token.STRING
1581         and previous is not None
1582         and previous.type == token.STRING
1583     ):
1584         return STRING_PRIORITY
1585
1586     if (
1587         leaf.type == token.NAME
1588         and leaf.value == "for"
1589         and leaf.parent
1590         and leaf.parent.type in {syms.comp_for, syms.old_comp_for}
1591     ):
1592         return COMPREHENSION_PRIORITY
1593
1594     if (
1595         leaf.type == token.NAME
1596         and leaf.value == "if"
1597         and leaf.parent
1598         and leaf.parent.type in {syms.comp_if, syms.old_comp_if}
1599     ):
1600         return COMPREHENSION_PRIORITY
1601
1602     if leaf.type == token.NAME and leaf.value in LOGIC_OPERATORS and leaf.parent:
1603         return LOGIC_PRIORITY
1604
1605     return 0
1606
1607
1608 def generate_comments(leaf: Leaf) -> Iterator[Leaf]:
1609     """Clean the prefix of the `leaf` and generate comments from it, if any.
1610
1611     Comments in lib2to3 are shoved into the whitespace prefix.  This happens
1612     in `pgen2/driver.py:Driver.parse_tokens()`.  This was a brilliant implementation
1613     move because it does away with modifying the grammar to include all the
1614     possible places in which comments can be placed.
1615
1616     The sad consequence for us though is that comments don't "belong" anywhere.
1617     This is why this function generates simple parentless Leaf objects for
1618     comments.  We simply don't know what the correct parent should be.
1619
1620     No matter though, we can live without this.  We really only need to
1621     differentiate between inline and standalone comments.  The latter don't
1622     share the line with any code.
1623
1624     Inline comments are emitted as regular token.COMMENT leaves.  Standalone
1625     are emitted with a fake STANDALONE_COMMENT token identifier.
1626     """
1627     p = leaf.prefix
1628     if not p:
1629         return
1630
1631     if "#" not in p:
1632         return
1633
1634     consumed = 0
1635     nlines = 0
1636     for index, line in enumerate(p.split("\n")):
1637         consumed += len(line) + 1  # adding the length of the split '\n'
1638         line = line.lstrip()
1639         if not line:
1640             nlines += 1
1641         if not line.startswith("#"):
1642             continue
1643
1644         if index == 0 and leaf.type != token.ENDMARKER:
1645             comment_type = token.COMMENT  # simple trailing comment
1646         else:
1647             comment_type = STANDALONE_COMMENT
1648         comment = make_comment(line)
1649         yield Leaf(comment_type, comment, prefix="\n" * nlines)
1650
1651         if comment in {"# fmt: on", "# yapf: enable"}:
1652             raise FormatOn(consumed)
1653
1654         if comment in {"# fmt: off", "# yapf: disable"}:
1655             if comment_type == STANDALONE_COMMENT:
1656                 raise FormatOff(consumed)
1657
1658             prev = preceding_leaf(leaf)
1659             if not prev or prev.type in WHITESPACE:  # standalone comment in disguise
1660                 raise FormatOff(consumed)
1661
1662         nlines = 0
1663
1664
1665 def make_comment(content: str) -> str:
1666     """Return a consistently formatted comment from the given `content` string.
1667
1668     All comments (except for "##", "#!", "#:") should have a single space between
1669     the hash sign and the content.
1670
1671     If `content` didn't start with a hash sign, one is provided.
1672     """
1673     content = content.rstrip()
1674     if not content:
1675         return "#"
1676
1677     if content[0] == "#":
1678         content = content[1:]
1679     if content and content[0] not in " !:#":
1680         content = " " + content
1681     return "#" + content
1682
1683
1684 def split_line(
1685     line: Line, line_length: int, inner: bool = False, py36: bool = False
1686 ) -> Iterator[Line]:
1687     """Split a `line` into potentially many lines.
1688
1689     They should fit in the allotted `line_length` but might not be able to.
1690     `inner` signifies that there were a pair of brackets somewhere around the
1691     current `line`, possibly transitively. This means we can fallback to splitting
1692     by delimiters if the LHS/RHS don't yield any results.
1693
1694     If `py36` is True, splitting may generate syntax that is only compatible
1695     with Python 3.6 and later.
1696     """
1697     if isinstance(line, UnformattedLines) or line.is_comment:
1698         yield line
1699         return
1700
1701     line_str = str(line).strip("\n")
1702     if (
1703         len(line_str) <= line_length
1704         and "\n" not in line_str  # multiline strings
1705         and not line.contains_standalone_comments()
1706     ):
1707         yield line
1708         return
1709
1710     split_funcs: List[SplitFunc]
1711     if line.is_def:
1712         split_funcs = [left_hand_split]
1713     elif line.inside_brackets:
1714         split_funcs = [delimiter_split, standalone_comment_split, right_hand_split]
1715     else:
1716         split_funcs = [right_hand_split]
1717     for split_func in split_funcs:
1718         # We are accumulating lines in `result` because we might want to abort
1719         # mission and return the original line in the end, or attempt a different
1720         # split altogether.
1721         result: List[Line] = []
1722         try:
1723             for l in split_func(line, py36):
1724                 if str(l).strip("\n") == line_str:
1725                     raise CannotSplit("Split function returned an unchanged result")
1726
1727                 result.extend(
1728                     split_line(l, line_length=line_length, inner=True, py36=py36)
1729                 )
1730         except CannotSplit as cs:
1731             continue
1732
1733         else:
1734             yield from result
1735             break
1736
1737     else:
1738         yield line
1739
1740
1741 def left_hand_split(line: Line, py36: bool = False) -> Iterator[Line]:
1742     """Split line into many lines, starting with the first matching bracket pair.
1743
1744     Note: this usually looks weird, only use this for function definitions.
1745     Prefer RHS otherwise.
1746     """
1747     head = Line(depth=line.depth)
1748     body = Line(depth=line.depth + 1, inside_brackets=True)
1749     tail = Line(depth=line.depth)
1750     tail_leaves: List[Leaf] = []
1751     body_leaves: List[Leaf] = []
1752     head_leaves: List[Leaf] = []
1753     current_leaves = head_leaves
1754     matching_bracket = None
1755     for leaf in line.leaves:
1756         if (
1757             current_leaves is body_leaves
1758             and leaf.type in CLOSING_BRACKETS
1759             and leaf.opening_bracket is matching_bracket
1760         ):
1761             current_leaves = tail_leaves if body_leaves else head_leaves
1762         current_leaves.append(leaf)
1763         if current_leaves is head_leaves:
1764             if leaf.type in OPENING_BRACKETS:
1765                 matching_bracket = leaf
1766                 current_leaves = body_leaves
1767     # Since body is a new indent level, remove spurious leading whitespace.
1768     if body_leaves:
1769         normalize_prefix(body_leaves[0], inside_brackets=True)
1770     # Build the new lines.
1771     for result, leaves in (head, head_leaves), (body, body_leaves), (tail, tail_leaves):
1772         for leaf in leaves:
1773             result.append(leaf, preformatted=True)
1774             for comment_after in line.comments_after(leaf):
1775                 result.append(comment_after, preformatted=True)
1776     bracket_split_succeeded_or_raise(head, body, tail)
1777     for result in (head, body, tail):
1778         if result:
1779             yield result
1780
1781
1782 def right_hand_split(
1783     line: Line, py36: bool = False, omit: Collection[LeafID] = ()
1784 ) -> Iterator[Line]:
1785     """Split line into many lines, starting with the last matching bracket pair."""
1786     head = Line(depth=line.depth)
1787     body = Line(depth=line.depth + 1, inside_brackets=True)
1788     tail = Line(depth=line.depth)
1789     tail_leaves: List[Leaf] = []
1790     body_leaves: List[Leaf] = []
1791     head_leaves: List[Leaf] = []
1792     current_leaves = tail_leaves
1793     opening_bracket = None
1794     closing_bracket = None
1795     for leaf in reversed(line.leaves):
1796         if current_leaves is body_leaves:
1797             if leaf is opening_bracket:
1798                 current_leaves = head_leaves if body_leaves else tail_leaves
1799         current_leaves.append(leaf)
1800         if current_leaves is tail_leaves:
1801             if leaf.type in CLOSING_BRACKETS and id(leaf) not in omit:
1802                 opening_bracket = leaf.opening_bracket
1803                 closing_bracket = leaf
1804                 current_leaves = body_leaves
1805     tail_leaves.reverse()
1806     body_leaves.reverse()
1807     head_leaves.reverse()
1808     # Since body is a new indent level, remove spurious leading whitespace.
1809     if body_leaves:
1810         normalize_prefix(body_leaves[0], inside_brackets=True)
1811     elif not head_leaves:
1812         # No `head` and no `body` means the split failed. `tail` has all content.
1813         raise CannotSplit("No brackets found")
1814
1815     # Build the new lines.
1816     for result, leaves in (head, head_leaves), (body, body_leaves), (tail, tail_leaves):
1817         for leaf in leaves:
1818             result.append(leaf, preformatted=True)
1819             for comment_after in line.comments_after(leaf):
1820                 result.append(comment_after, preformatted=True)
1821     bracket_split_succeeded_or_raise(head, body, tail)
1822     assert opening_bracket and closing_bracket
1823     if (
1824         opening_bracket.type == token.LPAR
1825         and not opening_bracket.value
1826         and closing_bracket.type == token.RPAR
1827         and not closing_bracket.value
1828     ):
1829         # These parens were optional. If there aren't any delimiters or standalone
1830         # comments in the body, they were unnecessary and another split without
1831         # them should be attempted.
1832         if not (
1833             body.bracket_tracker.delimiters or line.contains_standalone_comments(0)
1834         ):
1835             omit = {id(closing_bracket), *omit}
1836             yield from right_hand_split(line, py36=py36, omit=omit)
1837             return
1838
1839     ensure_visible(opening_bracket)
1840     ensure_visible(closing_bracket)
1841     for result in (head, body, tail):
1842         if result:
1843             yield result
1844
1845
1846 def bracket_split_succeeded_or_raise(head: Line, body: Line, tail: Line) -> None:
1847     """Raise :exc:`CannotSplit` if the last left- or right-hand split failed.
1848
1849     Do nothing otherwise.
1850
1851     A left- or right-hand split is based on a pair of brackets. Content before
1852     (and including) the opening bracket is left on one line, content inside the
1853     brackets is put on a separate line, and finally content starting with and
1854     following the closing bracket is put on a separate line.
1855
1856     Those are called `head`, `body`, and `tail`, respectively. If the split
1857     produced the same line (all content in `head`) or ended up with an empty `body`
1858     and the `tail` is just the closing bracket, then it's considered failed.
1859     """
1860     tail_len = len(str(tail).strip())
1861     if not body:
1862         if tail_len == 0:
1863             raise CannotSplit("Splitting brackets produced the same line")
1864
1865         elif tail_len < 3:
1866             raise CannotSplit(
1867                 f"Splitting brackets on an empty body to save "
1868                 f"{tail_len} characters is not worth it"
1869             )
1870
1871
1872 def dont_increase_indentation(split_func: SplitFunc) -> SplitFunc:
1873     """Normalize prefix of the first leaf in every line returned by `split_func`.
1874
1875     This is a decorator over relevant split functions.
1876     """
1877
1878     @wraps(split_func)
1879     def split_wrapper(line: Line, py36: bool = False) -> Iterator[Line]:
1880         for l in split_func(line, py36):
1881             normalize_prefix(l.leaves[0], inside_brackets=True)
1882             yield l
1883
1884     return split_wrapper
1885
1886
1887 @dont_increase_indentation
1888 def delimiter_split(line: Line, py36: bool = False) -> Iterator[Line]:
1889     """Split according to delimiters of the highest priority.
1890
1891     If `py36` is True, the split will add trailing commas also in function
1892     signatures that contain `*` and `**`.
1893     """
1894     try:
1895         last_leaf = line.leaves[-1]
1896     except IndexError:
1897         raise CannotSplit("Line empty")
1898
1899     delimiters = line.bracket_tracker.delimiters
1900     try:
1901         delimiter_priority = line.bracket_tracker.max_delimiter_priority(
1902             exclude={id(last_leaf)}
1903         )
1904     except ValueError:
1905         raise CannotSplit("No delimiters found")
1906
1907     current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
1908     lowest_depth = sys.maxsize
1909     trailing_comma_safe = True
1910
1911     def append_to_line(leaf: Leaf) -> Iterator[Line]:
1912         """Append `leaf` to current line or to new line if appending impossible."""
1913         nonlocal current_line
1914         try:
1915             current_line.append_safe(leaf, preformatted=True)
1916         except ValueError as ve:
1917             yield current_line
1918
1919             current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
1920             current_line.append(leaf)
1921
1922     for leaf in line.leaves:
1923         yield from append_to_line(leaf)
1924
1925         for comment_after in line.comments_after(leaf):
1926             yield from append_to_line(comment_after)
1927
1928         lowest_depth = min(lowest_depth, leaf.bracket_depth)
1929         if (
1930             leaf.bracket_depth == lowest_depth
1931             and is_vararg(leaf, within=VARARGS_PARENTS)
1932         ):
1933             trailing_comma_safe = trailing_comma_safe and py36
1934         leaf_priority = delimiters.get(id(leaf))
1935         if leaf_priority == delimiter_priority:
1936             yield current_line
1937
1938             current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
1939     if current_line:
1940         if (
1941             trailing_comma_safe
1942             and delimiter_priority == COMMA_PRIORITY
1943             and current_line.leaves[-1].type != token.COMMA
1944             and current_line.leaves[-1].type != STANDALONE_COMMENT
1945         ):
1946             current_line.append(Leaf(token.COMMA, ","))
1947         yield current_line
1948
1949
1950 @dont_increase_indentation
1951 def standalone_comment_split(line: Line, py36: bool = False) -> Iterator[Line]:
1952     """Split standalone comments from the rest of the line."""
1953     if not line.contains_standalone_comments(0):
1954         raise CannotSplit("Line does not have any standalone comments")
1955
1956     current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
1957
1958     def append_to_line(leaf: Leaf) -> Iterator[Line]:
1959         """Append `leaf` to current line or to new line if appending impossible."""
1960         nonlocal current_line
1961         try:
1962             current_line.append_safe(leaf, preformatted=True)
1963         except ValueError as ve:
1964             yield current_line
1965
1966             current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
1967             current_line.append(leaf)
1968
1969     for leaf in line.leaves:
1970         yield from append_to_line(leaf)
1971
1972         for comment_after in line.comments_after(leaf):
1973             yield from append_to_line(comment_after)
1974
1975     if current_line:
1976         yield current_line
1977
1978
1979 def is_import(leaf: Leaf) -> bool:
1980     """Return True if the given leaf starts an import statement."""
1981     p = leaf.parent
1982     t = leaf.type
1983     v = leaf.value
1984     return bool(
1985         t == token.NAME
1986         and (
1987             (v == "import" and p and p.type == syms.import_name)
1988             or (v == "from" and p and p.type == syms.import_from)
1989         )
1990     )
1991
1992
1993 def normalize_prefix(leaf: Leaf, *, inside_brackets: bool) -> None:
1994     """Leave existing extra newlines if not `inside_brackets`. Remove everything
1995     else.
1996
1997     Note: don't use backslashes for formatting or you'll lose your voting rights.
1998     """
1999     if not inside_brackets:
2000         spl = leaf.prefix.split("#")
2001         if "\\" not in spl[0]:
2002             nl_count = spl[-1].count("\n")
2003             if len(spl) > 1:
2004                 nl_count -= 1
2005             leaf.prefix = "\n" * nl_count
2006             return
2007
2008     leaf.prefix = ""
2009
2010
2011 def normalize_string_quotes(leaf: Leaf) -> None:
2012     """Prefer double quotes but only if it doesn't cause more escaping.
2013
2014     Adds or removes backslashes as appropriate. Doesn't parse and fix
2015     strings nested in f-strings (yet).
2016
2017     Note: Mutates its argument.
2018     """
2019     value = leaf.value.lstrip("furbFURB")
2020     if value[:3] == '"""':
2021         return
2022
2023     elif value[:3] == "'''":
2024         orig_quote = "'''"
2025         new_quote = '"""'
2026     elif value[0] == '"':
2027         orig_quote = '"'
2028         new_quote = "'"
2029     else:
2030         orig_quote = "'"
2031         new_quote = '"'
2032     first_quote_pos = leaf.value.find(orig_quote)
2033     if first_quote_pos == -1:
2034         return  # There's an internal error
2035
2036     prefix = leaf.value[:first_quote_pos]
2037     unescaped_new_quote = re.compile(rf"(([^\\]|^)(\\\\)*){new_quote}")
2038     escaped_new_quote = re.compile(rf"([^\\]|^)\\(\\\\)*{new_quote}")
2039     escaped_orig_quote = re.compile(rf"([^\\]|^)\\(\\\\)*{orig_quote}")
2040     body = leaf.value[first_quote_pos + len(orig_quote):-len(orig_quote)]
2041     if "r" in prefix.casefold():
2042         if unescaped_new_quote.search(body):
2043             # There's at least one unescaped new_quote in this raw string
2044             # so converting is impossible
2045             return
2046
2047         # Do not introduce or remove backslashes in raw strings
2048         new_body = body
2049     else:
2050         # remove unnecessary quotes
2051         new_body = sub_twice(escaped_new_quote, rf"\1\2{new_quote}", body)
2052         if body != new_body:
2053             # Consider the string without unnecessary quotes as the original
2054             body = new_body
2055             leaf.value = f"{prefix}{orig_quote}{body}{orig_quote}"
2056         new_body = sub_twice(escaped_orig_quote, rf"\1\2{orig_quote}", new_body)
2057         new_body = sub_twice(unescaped_new_quote, rf"\1\\{new_quote}", new_body)
2058     if new_quote == '"""' and new_body[-1] == '"':
2059         # edge case:
2060         new_body = new_body[:-1] + '\\"'
2061     orig_escape_count = body.count("\\")
2062     new_escape_count = new_body.count("\\")
2063     if new_escape_count > orig_escape_count:
2064         return  # Do not introduce more escaping
2065
2066     if new_escape_count == orig_escape_count and orig_quote == '"':
2067         return  # Prefer double quotes
2068
2069     leaf.value = f"{prefix}{new_quote}{new_body}{new_quote}"
2070
2071
2072 def normalize_invisible_parens(node: Node, parens_after: Set[str]) -> None:
2073     """Make existing optional parentheses invisible or create new ones.
2074
2075     Standardizes on visible parentheses for single-element tuples, and keeps
2076     existing visible parentheses for other tuples and generator expressions.
2077     """
2078     check_lpar = False
2079     for child in list(node.children):
2080         if check_lpar:
2081             if child.type == syms.atom:
2082                 if not (
2083                     is_empty_tuple(child)
2084                     or is_one_tuple(child)
2085                     or max_delimiter_priority_in_atom(child) >= COMMA_PRIORITY
2086                 ):
2087                     first = child.children[0]
2088                     last = child.children[-1]
2089                     if first.type == token.LPAR and last.type == token.RPAR:
2090                         # make parentheses invisible
2091                         first.value = ""  # type: ignore
2092                         last.value = ""  # type: ignore
2093             elif is_one_tuple(child):
2094                 # wrap child in visible parentheses
2095                 lpar = Leaf(token.LPAR, "(")
2096                 rpar = Leaf(token.RPAR, ")")
2097                 index = child.remove() or 0
2098                 node.insert_child(index, Node(syms.atom, [lpar, child, rpar]))
2099             else:
2100                 # wrap child in invisible parentheses
2101                 lpar = Leaf(token.LPAR, "")
2102                 rpar = Leaf(token.RPAR, "")
2103                 index = child.remove() or 0
2104                 node.insert_child(index, Node(syms.atom, [lpar, child, rpar]))
2105
2106         check_lpar = isinstance(child, Leaf) and child.value in parens_after
2107
2108
2109 def is_empty_tuple(node: LN) -> bool:
2110     """Return True if `node` holds an empty tuple."""
2111     return (
2112         node.type == syms.atom
2113         and len(node.children) == 2
2114         and node.children[0].type == token.LPAR
2115         and node.children[1].type == token.RPAR
2116     )
2117
2118
2119 def is_one_tuple(node: LN) -> bool:
2120     """Return True if `node` holds a tuple with one element, with or without parens."""
2121     if node.type == syms.atom:
2122         if len(node.children) != 3:
2123             return False
2124
2125         lpar, gexp, rpar = node.children
2126         if not (
2127             lpar.type == token.LPAR
2128             and gexp.type == syms.testlist_gexp
2129             and rpar.type == token.RPAR
2130         ):
2131             return False
2132
2133         return len(gexp.children) == 2 and gexp.children[1].type == token.COMMA
2134
2135     return (
2136         node.type in IMPLICIT_TUPLE
2137         and len(node.children) == 2
2138         and node.children[1].type == token.COMMA
2139     )
2140
2141
2142 def is_vararg(leaf: Leaf, within: Set[NodeType]) -> bool:
2143     """Return True if `leaf` is a star or double star in a vararg or kwarg.
2144
2145     If `within` includes VARARGS_PARENTS, this applies to function signatures.
2146     If `within` includes COLLECTION_LIBERALS_PARENTS, it applies to right
2147     hand-side extended iterable unpacking (PEP 3132) and additional unpacking
2148     generalizations (PEP 448).
2149     """
2150     if leaf.type not in STARS or not leaf.parent:
2151         return False
2152
2153     p = leaf.parent
2154     if p.type == syms.star_expr:
2155         # Star expressions are also used as assignment targets in extended
2156         # iterable unpacking (PEP 3132).  See what its parent is instead.
2157         if not p.parent:
2158             return False
2159
2160         p = p.parent
2161
2162     return p.type in within
2163
2164
2165 def max_delimiter_priority_in_atom(node: LN) -> int:
2166     """Return maximum delimiter priority inside `node`.
2167
2168     This is specific to atoms with contents contained in a pair of parentheses.
2169     If `node` isn't an atom or there are no enclosing parentheses, returns 0.
2170     """
2171     if node.type != syms.atom:
2172         return 0
2173
2174     first = node.children[0]
2175     last = node.children[-1]
2176     if not (first.type == token.LPAR and last.type == token.RPAR):
2177         return 0
2178
2179     bt = BracketTracker()
2180     for c in node.children[1:-1]:
2181         if isinstance(c, Leaf):
2182             bt.mark(c)
2183         else:
2184             for leaf in c.leaves():
2185                 bt.mark(leaf)
2186     try:
2187         return bt.max_delimiter_priority()
2188
2189     except ValueError:
2190         return 0
2191
2192
2193 def ensure_visible(leaf: Leaf) -> None:
2194     """Make sure parentheses are visible.
2195
2196     They could be invisible as part of some statements (see
2197     :func:`normalize_invible_parens` and :func:`visit_import_from`).
2198     """
2199     if leaf.type == token.LPAR:
2200         leaf.value = "("
2201     elif leaf.type == token.RPAR:
2202         leaf.value = ")"
2203
2204
2205 def is_python36(node: Node) -> bool:
2206     """Return True if the current file is using Python 3.6+ features.
2207
2208     Currently looking for:
2209     - f-strings; and
2210     - trailing commas after * or ** in function signatures.
2211     """
2212     for n in node.pre_order():
2213         if n.type == token.STRING:
2214             value_head = n.value[:2]  # type: ignore
2215             if value_head in {'f"', 'F"', "f'", "F'", "rf", "fr", "RF", "FR"}:
2216                 return True
2217
2218         elif (
2219             n.type == syms.typedargslist
2220             and n.children
2221             and n.children[-1].type == token.COMMA
2222         ):
2223             for ch in n.children:
2224                 if ch.type in STARS:
2225                     return True
2226
2227     return False
2228
2229
2230 PYTHON_EXTENSIONS = {".py"}
2231 BLACKLISTED_DIRECTORIES = {
2232     "build", "buck-out", "dist", "_build", ".git", ".hg", ".mypy_cache", ".tox", ".venv"
2233 }
2234
2235
2236 def gen_python_files_in_dir(path: Path) -> Iterator[Path]:
2237     """Generate all files under `path` which aren't under BLACKLISTED_DIRECTORIES
2238     and have one of the PYTHON_EXTENSIONS.
2239     """
2240     for child in path.iterdir():
2241         if child.is_dir():
2242             if child.name in BLACKLISTED_DIRECTORIES:
2243                 continue
2244
2245             yield from gen_python_files_in_dir(child)
2246
2247         elif child.suffix in PYTHON_EXTENSIONS:
2248             yield child
2249
2250
2251 @dataclass
2252 class Report:
2253     """Provides a reformatting counter. Can be rendered with `str(report)`."""
2254     check: bool = False
2255     quiet: bool = False
2256     change_count: int = 0
2257     same_count: int = 0
2258     failure_count: int = 0
2259
2260     def done(self, src: Path, changed: Changed) -> None:
2261         """Increment the counter for successful reformatting. Write out a message."""
2262         if changed is Changed.YES:
2263             reformatted = "would reformat" if self.check else "reformatted"
2264             if not self.quiet:
2265                 out(f"{reformatted} {src}")
2266             self.change_count += 1
2267         else:
2268             if not self.quiet:
2269                 if changed is Changed.NO:
2270                     msg = f"{src} already well formatted, good job."
2271                 else:
2272                     msg = f"{src} wasn't modified on disk since last run."
2273                 out(msg, bold=False)
2274             self.same_count += 1
2275
2276     def failed(self, src: Path, message: str) -> None:
2277         """Increment the counter for failed reformatting. Write out a message."""
2278         err(f"error: cannot format {src}: {message}")
2279         self.failure_count += 1
2280
2281     @property
2282     def return_code(self) -> int:
2283         """Return the exit code that the app should use.
2284
2285         This considers the current state of changed files and failures:
2286         - if there were any failures, return 123;
2287         - if any files were changed and --check is being used, return 1;
2288         - otherwise return 0.
2289         """
2290         # According to http://tldp.org/LDP/abs/html/exitcodes.html starting with
2291         # 126 we have special returncodes reserved by the shell.
2292         if self.failure_count:
2293             return 123
2294
2295         elif self.change_count and self.check:
2296             return 1
2297
2298         return 0
2299
2300     def __str__(self) -> str:
2301         """Render a color report of the current state.
2302
2303         Use `click.unstyle` to remove colors.
2304         """
2305         if self.check:
2306             reformatted = "would be reformatted"
2307             unchanged = "would be left unchanged"
2308             failed = "would fail to reformat"
2309         else:
2310             reformatted = "reformatted"
2311             unchanged = "left unchanged"
2312             failed = "failed to reformat"
2313         report = []
2314         if self.change_count:
2315             s = "s" if self.change_count > 1 else ""
2316             report.append(
2317                 click.style(f"{self.change_count} file{s} {reformatted}", bold=True)
2318             )
2319         if self.same_count:
2320             s = "s" if self.same_count > 1 else ""
2321             report.append(f"{self.same_count} file{s} {unchanged}")
2322         if self.failure_count:
2323             s = "s" if self.failure_count > 1 else ""
2324             report.append(
2325                 click.style(f"{self.failure_count} file{s} {failed}", fg="red")
2326             )
2327         return ", ".join(report) + "."
2328
2329
2330 def assert_equivalent(src: str, dst: str) -> None:
2331     """Raise AssertionError if `src` and `dst` aren't equivalent."""
2332
2333     import ast
2334     import traceback
2335
2336     def _v(node: ast.AST, depth: int = 0) -> Iterator[str]:
2337         """Simple visitor generating strings to compare ASTs by content."""
2338         yield f"{'  ' * depth}{node.__class__.__name__}("
2339
2340         for field in sorted(node._fields):
2341             try:
2342                 value = getattr(node, field)
2343             except AttributeError:
2344                 continue
2345
2346             yield f"{'  ' * (depth+1)}{field}="
2347
2348             if isinstance(value, list):
2349                 for item in value:
2350                     if isinstance(item, ast.AST):
2351                         yield from _v(item, depth + 2)
2352
2353             elif isinstance(value, ast.AST):
2354                 yield from _v(value, depth + 2)
2355
2356             else:
2357                 yield f"{'  ' * (depth+2)}{value!r},  # {value.__class__.__name__}"
2358
2359         yield f"{'  ' * depth})  # /{node.__class__.__name__}"
2360
2361     try:
2362         src_ast = ast.parse(src)
2363     except Exception as exc:
2364         major, minor = sys.version_info[:2]
2365         raise AssertionError(
2366             f"cannot use --safe with this file; failed to parse source file "
2367             f"with Python {major}.{minor}'s builtin AST. Re-run with --fast "
2368             f"or stop using deprecated Python 2 syntax. AST error message: {exc}"
2369         )
2370
2371     try:
2372         dst_ast = ast.parse(dst)
2373     except Exception as exc:
2374         log = dump_to_file("".join(traceback.format_tb(exc.__traceback__)), dst)
2375         raise AssertionError(
2376             f"INTERNAL ERROR: Black produced invalid code: {exc}. "
2377             f"Please report a bug on https://github.com/ambv/black/issues.  "
2378             f"This invalid output might be helpful: {log}"
2379         ) from None
2380
2381     src_ast_str = "\n".join(_v(src_ast))
2382     dst_ast_str = "\n".join(_v(dst_ast))
2383     if src_ast_str != dst_ast_str:
2384         log = dump_to_file(diff(src_ast_str, dst_ast_str, "src", "dst"))
2385         raise AssertionError(
2386             f"INTERNAL ERROR: Black produced code that is not equivalent to "
2387             f"the source.  "
2388             f"Please report a bug on https://github.com/ambv/black/issues.  "
2389             f"This diff might be helpful: {log}"
2390         ) from None
2391
2392
2393 def assert_stable(src: str, dst: str, line_length: int) -> None:
2394     """Raise AssertionError if `dst` reformats differently the second time."""
2395     newdst = format_str(dst, line_length=line_length)
2396     if dst != newdst:
2397         log = dump_to_file(
2398             diff(src, dst, "source", "first pass"),
2399             diff(dst, newdst, "first pass", "second pass"),
2400         )
2401         raise AssertionError(
2402             f"INTERNAL ERROR: Black produced different code on the second pass "
2403             f"of the formatter.  "
2404             f"Please report a bug on https://github.com/ambv/black/issues.  "
2405             f"This diff might be helpful: {log}"
2406         ) from None
2407
2408
2409 def dump_to_file(*output: str) -> str:
2410     """Dump `output` to a temporary file. Return path to the file."""
2411     import tempfile
2412
2413     with tempfile.NamedTemporaryFile(
2414         mode="w", prefix="blk_", suffix=".log", delete=False, encoding="utf8"
2415     ) as f:
2416         for lines in output:
2417             f.write(lines)
2418             if lines and lines[-1] != "\n":
2419                 f.write("\n")
2420     return f.name
2421
2422
2423 def diff(a: str, b: str, a_name: str, b_name: str) -> str:
2424     """Return a unified diff string between strings `a` and `b`."""
2425     import difflib
2426
2427     a_lines = [line + "\n" for line in a.split("\n")]
2428     b_lines = [line + "\n" for line in b.split("\n")]
2429     return "".join(
2430         difflib.unified_diff(a_lines, b_lines, fromfile=a_name, tofile=b_name, n=5)
2431     )
2432
2433
2434 def cancel(tasks: List[asyncio.Task]) -> None:
2435     """asyncio signal handler that cancels all `tasks` and reports to stderr."""
2436     err("Aborted!")
2437     for task in tasks:
2438         task.cancel()
2439
2440
2441 def shutdown(loop: BaseEventLoop) -> None:
2442     """Cancel all pending tasks on `loop`, wait for them, and close the loop."""
2443     try:
2444         # This part is borrowed from asyncio/runners.py in Python 3.7b2.
2445         to_cancel = [task for task in asyncio.Task.all_tasks(loop) if not task.done()]
2446         if not to_cancel:
2447             return
2448
2449         for task in to_cancel:
2450             task.cancel()
2451         loop.run_until_complete(
2452             asyncio.gather(*to_cancel, loop=loop, return_exceptions=True)
2453         )
2454     finally:
2455         # `concurrent.futures.Future` objects cannot be cancelled once they
2456         # are already running. There might be some when the `shutdown()` happened.
2457         # Silence their logger's spew about the event loop being closed.
2458         cf_logger = logging.getLogger("concurrent.futures")
2459         cf_logger.setLevel(logging.CRITICAL)
2460         loop.close()
2461
2462
2463 def sub_twice(regex: Pattern[str], replacement: str, original: str) -> str:
2464     """Replace `regex` with `replacement` twice on `original`.
2465
2466     This is used by string normalization to perform replaces on
2467     overlapping matches.
2468     """
2469     return regex.sub(replacement, regex.sub(replacement, original))
2470
2471
2472 CACHE_DIR = Path(user_cache_dir("black", version=__version__))
2473 CACHE_FILE = CACHE_DIR / "cache.pickle"
2474
2475
2476 def read_cache() -> Cache:
2477     """Read the cache if it exists and is well formed.
2478
2479     If it is not well formed, the call to write_cache later should resolve the issue.
2480     """
2481     if not CACHE_FILE.exists():
2482         return {}
2483
2484     with CACHE_FILE.open("rb") as fobj:
2485         try:
2486             cache: Cache = pickle.load(fobj)
2487         except pickle.UnpicklingError:
2488             return {}
2489
2490     return cache
2491
2492
2493 def get_cache_info(path: Path) -> CacheInfo:
2494     """Return the information used to check if a file is already formatted or not."""
2495     stat = path.stat()
2496     return stat.st_mtime, stat.st_size
2497
2498
2499 def filter_cached(
2500     cache: Cache, sources: Iterable[Path]
2501 ) -> Tuple[List[Path], List[Path]]:
2502     """Split a list of paths into two.
2503
2504     The first list contains paths of files that modified on disk or are not in the
2505     cache. The other list contains paths to non-modified files.
2506     """
2507     todo, done = [], []
2508     for src in sources:
2509         src = src.resolve()
2510         if cache.get(src) != get_cache_info(src):
2511             todo.append(src)
2512         else:
2513             done.append(src)
2514     return todo, done
2515
2516
2517 def write_cache(cache: Cache, sources: List[Path]) -> None:
2518     """Update the cache file."""
2519     try:
2520         if not CACHE_DIR.exists():
2521             CACHE_DIR.mkdir(parents=True)
2522         new_cache = {**cache, **{src.resolve(): get_cache_info(src) for src in sources}}
2523         with CACHE_FILE.open("wb") as fobj:
2524             pickle.dump(new_cache, fobj, protocol=pickle.HIGHEST_PROTOCOL)
2525     except OSError:
2526         pass
2527
2528
2529 if __name__ == "__main__":
2530     main()