Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/black/__init__.py: 19%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import io
2import json
3import platform
4import re
5import sys
6import tokenize
7import traceback
8from collections.abc import (
9 Collection,
10 Generator,
11 Iterator,
12 MutableMapping,
13 Sequence,
14 Sized,
15)
16from contextlib import contextmanager
17from dataclasses import replace
18from datetime import datetime, timezone
19from enum import Enum
20from json.decoder import JSONDecodeError
21from pathlib import Path
22from re import Pattern
23from typing import Any, Optional, Union
25import click
26from click.core import ParameterSource
27from mypy_extensions import mypyc_attr
28from pathspec import PathSpec
29from pathspec.patterns.gitwildmatch import GitWildMatchPatternError
31from _black_version import version as __version__
32from black.cache import Cache
33from black.comments import normalize_fmt_off
34from black.const import (
35 DEFAULT_EXCLUDES,
36 DEFAULT_INCLUDES,
37 DEFAULT_LINE_LENGTH,
38 STDIN_PLACEHOLDER,
39)
40from black.files import (
41 best_effort_relative_path,
42 find_project_root,
43 find_pyproject_toml,
44 find_user_pyproject_toml,
45 gen_python_files,
46 get_gitignore,
47 parse_pyproject_toml,
48 path_is_excluded,
49 resolves_outside_root_or_cannot_stat,
50 wrap_stream_for_windows,
51)
52from black.handle_ipynb_magics import (
53 PYTHON_CELL_MAGICS,
54 jupyter_dependencies_are_installed,
55 mask_cell,
56 put_trailing_semicolon_back,
57 remove_trailing_semicolon,
58 unmask_cell,
59 validate_cell,
60)
61from black.linegen import LN, LineGenerator, transform_line
62from black.lines import EmptyLineTracker, LinesBlock
63from black.mode import FUTURE_FLAG_TO_FEATURE, VERSION_TO_FEATURES, Feature
64from black.mode import Mode as Mode # re-exported
65from black.mode import Preview, TargetVersion, supports_feature
66from black.nodes import STARS, is_number_token, is_simple_decorator_expression, syms
67from black.output import color_diff, diff, dump_to_file, err, ipynb_diff, out
68from black.parsing import ( # noqa F401
69 ASTSafetyError,
70 InvalidInput,
71 lib2to3_parse,
72 parse_ast,
73 stringify_ast,
74)
75from black.ranges import (
76 adjusted_lines,
77 convert_unchanged_lines,
78 parse_line_ranges,
79 sanitized_lines,
80)
81from black.report import Changed, NothingChanged, Report
82from blib2to3.pgen2 import token
83from blib2to3.pytree import Leaf, Node
85COMPILED = Path(__file__).suffix in (".pyd", ".so")
87# types
88FileContent = str
89Encoding = str
90NewLine = str
93class WriteBack(Enum):
94 NO = 0
95 YES = 1
96 DIFF = 2
97 CHECK = 3
98 COLOR_DIFF = 4
100 @classmethod
101 def from_configuration(
102 cls, *, check: bool, diff: bool, color: bool = False
103 ) -> "WriteBack":
104 if check and not diff:
105 return cls.CHECK
107 if diff and color:
108 return cls.COLOR_DIFF
110 return cls.DIFF if diff else cls.YES
113# Legacy name, left for integrations.
114FileMode = Mode
117def read_pyproject_toml(
118 ctx: click.Context, param: click.Parameter, value: Optional[str]
119) -> Optional[str]:
120 """Inject Black configuration from "pyproject.toml" into defaults in `ctx`.
122 Returns the path to a successfully found and read configuration file, None
123 otherwise.
124 """
125 if not value:
126 value = find_pyproject_toml(
127 ctx.params.get("src", ()), ctx.params.get("stdin_filename", None)
128 )
129 if value is None:
130 return None
132 try:
133 config = parse_pyproject_toml(value)
134 except (OSError, ValueError) as e:
135 raise click.FileError(
136 filename=value, hint=f"Error reading configuration file: {e}"
137 ) from None
139 if not config:
140 return None
141 else:
142 spellcheck_pyproject_toml_keys(ctx, list(config), value)
143 # Sanitize the values to be Click friendly. For more information please see:
144 # https://github.com/psf/black/issues/1458
145 # https://github.com/pallets/click/issues/1567
146 config = {
147 k: str(v) if not isinstance(v, (list, dict)) else v
148 for k, v in config.items()
149 }
151 target_version = config.get("target_version")
152 if target_version is not None and not isinstance(target_version, list):
153 raise click.BadOptionUsage(
154 "target-version", "Config key target-version must be a list"
155 )
157 exclude = config.get("exclude")
158 if exclude is not None and not isinstance(exclude, str):
159 raise click.BadOptionUsage("exclude", "Config key exclude must be a string")
161 extend_exclude = config.get("extend_exclude")
162 if extend_exclude is not None and not isinstance(extend_exclude, str):
163 raise click.BadOptionUsage(
164 "extend-exclude", "Config key extend-exclude must be a string"
165 )
167 line_ranges = config.get("line_ranges")
168 if line_ranges is not None:
169 raise click.BadOptionUsage(
170 "line-ranges", "Cannot use line-ranges in the pyproject.toml file."
171 )
173 default_map: dict[str, Any] = {}
174 if ctx.default_map:
175 default_map.update(ctx.default_map)
176 default_map.update(config)
178 ctx.default_map = default_map
179 return value
182def spellcheck_pyproject_toml_keys(
183 ctx: click.Context, config_keys: list[str], config_file_path: str
184) -> None:
185 invalid_keys: list[str] = []
186 available_config_options = {param.name for param in ctx.command.params}
187 invalid_keys = [key for key in config_keys if key not in available_config_options]
188 if invalid_keys:
189 keys_str = ", ".join(map(repr, invalid_keys))
190 out(
191 f"Invalid config keys detected: {keys_str} (in {config_file_path})",
192 fg="red",
193 )
196def target_version_option_callback(
197 c: click.Context, p: Union[click.Option, click.Parameter], v: tuple[str, ...]
198) -> list[TargetVersion]:
199 """Compute the target versions from a --target-version flag.
201 This is its own function because mypy couldn't infer the type correctly
202 when it was a lambda, causing mypyc trouble.
203 """
204 return [TargetVersion[val.upper()] for val in v]
207def enable_unstable_feature_callback(
208 c: click.Context, p: Union[click.Option, click.Parameter], v: tuple[str, ...]
209) -> list[Preview]:
210 """Compute the features from an --enable-unstable-feature flag."""
211 return [Preview[val] for val in v]
214def re_compile_maybe_verbose(regex: str) -> Pattern[str]:
215 """Compile a regular expression string in `regex`.
217 If it contains newlines, use verbose mode.
218 """
219 if "\n" in regex:
220 regex = "(?x)" + regex
221 compiled: Pattern[str] = re.compile(regex)
222 return compiled
225def validate_regex(
226 ctx: click.Context,
227 param: click.Parameter,
228 value: Optional[str],
229) -> Optional[Pattern[str]]:
230 try:
231 return re_compile_maybe_verbose(value) if value is not None else None
232 except re.error as e:
233 raise click.BadParameter(f"Not a valid regular expression: {e}") from None
236@click.command(
237 context_settings={"help_option_names": ["-h", "--help"]},
238 # While Click does set this field automatically using the docstring, mypyc
239 # (annoyingly) strips 'em so we need to set it here too.
240 help="The uncompromising code formatter.",
241)
242@click.option("-c", "--code", type=str, help="Format the code passed in as a string.")
243@click.option(
244 "-l",
245 "--line-length",
246 type=int,
247 default=DEFAULT_LINE_LENGTH,
248 help="How many characters per line to allow.",
249 show_default=True,
250)
251@click.option(
252 "-t",
253 "--target-version",
254 type=click.Choice([v.name.lower() for v in TargetVersion]),
255 callback=target_version_option_callback,
256 multiple=True,
257 help=(
258 "Python versions that should be supported by Black's output. You should"
259 " include all versions that your code supports. By default, Black will infer"
260 " target versions from the project metadata in pyproject.toml. If this does"
261 " not yield conclusive results, Black will use per-file auto-detection."
262 ),
263)
264@click.option(
265 "--pyi",
266 is_flag=True,
267 help=(
268 "Format all input files like typing stubs regardless of file extension. This"
269 " is useful when piping source on standard input."
270 ),
271)
272@click.option(
273 "--ipynb",
274 is_flag=True,
275 help=(
276 "Format all input files like Jupyter Notebooks regardless of file extension."
277 " This is useful when piping source on standard input."
278 ),
279)
280@click.option(
281 "--python-cell-magics",
282 multiple=True,
283 help=(
284 "When processing Jupyter Notebooks, add the given magic to the list"
285 f" of known python-magics ({', '.join(sorted(PYTHON_CELL_MAGICS))})."
286 " Useful for formatting cells with custom python magics."
287 ),
288 default=[],
289)
290@click.option(
291 "-x",
292 "--skip-source-first-line",
293 is_flag=True,
294 help="Skip the first line of the source code.",
295)
296@click.option(
297 "-S",
298 "--skip-string-normalization",
299 is_flag=True,
300 help="Don't normalize string quotes or prefixes.",
301)
302@click.option(
303 "-C",
304 "--skip-magic-trailing-comma",
305 is_flag=True,
306 help="Don't use trailing commas as a reason to split lines.",
307)
308@click.option(
309 "--preview",
310 is_flag=True,
311 help=(
312 "Enable potentially disruptive style changes that may be added to Black's main"
313 " functionality in the next major release."
314 ),
315)
316@click.option(
317 "--unstable",
318 is_flag=True,
319 help=(
320 "Enable potentially disruptive style changes that have known bugs or are not"
321 " currently expected to make it into the stable style Black's next major"
322 " release. Implies --preview."
323 ),
324)
325@click.option(
326 "--enable-unstable-feature",
327 type=click.Choice([v.name for v in Preview]),
328 callback=enable_unstable_feature_callback,
329 multiple=True,
330 help=(
331 "Enable specific features included in the `--unstable` style. Requires"
332 " `--preview`. No compatibility guarantees are provided on the behavior"
333 " or existence of any unstable features."
334 ),
335)
336@click.option(
337 "--check",
338 is_flag=True,
339 help=(
340 "Don't write the files back, just return the status. Return code 0 means"
341 " nothing would change. Return code 1 means some files would be reformatted."
342 " Return code 123 means there was an internal error."
343 ),
344)
345@click.option(
346 "--diff",
347 is_flag=True,
348 help=(
349 "Don't write the files back, just output a diff to indicate what changes"
350 " Black would've made. They are printed to stdout so capturing them is simple."
351 ),
352)
353@click.option(
354 "--color/--no-color",
355 is_flag=True,
356 help="Show (or do not show) colored diff. Only applies when --diff is given.",
357)
358@click.option(
359 "--line-ranges",
360 multiple=True,
361 metavar="START-END",
362 help=(
363 "When specified, Black will try its best to only format these lines. This"
364 " option can be specified multiple times, and a union of the lines will be"
365 " formatted. Each range must be specified as two integers connected by a `-`:"
366 " `<START>-<END>`. The `<START>` and `<END>` integer indices are 1-based and"
367 " inclusive on both ends."
368 ),
369 default=(),
370)
371@click.option(
372 "--fast/--safe",
373 is_flag=True,
374 help=(
375 "By default, Black performs an AST safety check after formatting your code."
376 " The --fast flag turns off this check and the --safe flag explicitly enables"
377 " it. [default: --safe]"
378 ),
379)
380@click.option(
381 "--required-version",
382 type=str,
383 help=(
384 "Require a specific version of Black to be running. This is useful for"
385 " ensuring that all contributors to your project are using the same"
386 " version, because different versions of Black may format code a little"
387 " differently. This option can be set in a configuration file for consistent"
388 " results across environments."
389 ),
390)
391@click.option(
392 "--exclude",
393 type=str,
394 callback=validate_regex,
395 help=(
396 "A regular expression that matches files and directories that should be"
397 " excluded on recursive searches. An empty value means no paths are excluded."
398 " Use forward slashes for directories on all platforms (Windows, too)."
399 " By default, Black also ignores all paths listed in .gitignore. Changing this"
400 f" value will override all default exclusions. [default: {DEFAULT_EXCLUDES}]"
401 ),
402 show_default=False,
403)
404@click.option(
405 "--extend-exclude",
406 type=str,
407 callback=validate_regex,
408 help=(
409 "Like --exclude, but adds additional files and directories on top of the"
410 " default values instead of overriding them."
411 ),
412)
413@click.option(
414 "--force-exclude",
415 type=str,
416 callback=validate_regex,
417 help=(
418 "Like --exclude, but files and directories matching this regex will be excluded"
419 " even when they are passed explicitly as arguments. This is useful when"
420 " invoking Black programmatically on changed files, such as in a pre-commit"
421 " hook or editor plugin."
422 ),
423)
424@click.option(
425 "--stdin-filename",
426 type=str,
427 is_eager=True,
428 help=(
429 "The name of the file when passing it through stdin. Useful to make sure Black"
430 " will respect the --force-exclude option on some editors that rely on using"
431 " stdin."
432 ),
433)
434@click.option(
435 "--include",
436 type=str,
437 default=DEFAULT_INCLUDES,
438 callback=validate_regex,
439 help=(
440 "A regular expression that matches files and directories that should be"
441 " included on recursive searches. An empty value means all files are included"
442 " regardless of the name. Use forward slashes for directories on all platforms"
443 " (Windows, too). Overrides all exclusions, including from .gitignore and"
444 " command line options."
445 ),
446 show_default=True,
447)
448@click.option(
449 "-W",
450 "--workers",
451 type=click.IntRange(min=1),
452 default=None,
453 help=(
454 "When Black formats multiple files, it may use a process pool to speed up"
455 " formatting. This option controls the number of parallel workers. This can"
456 " also be specified via the BLACK_NUM_WORKERS environment variable. Defaults"
457 " to the number of CPUs in the system."
458 ),
459)
460@click.option(
461 "-q",
462 "--quiet",
463 is_flag=True,
464 help=(
465 "Stop emitting all non-critical output. Error messages will still be emitted"
466 " (which can silenced by 2>/dev/null)."
467 ),
468)
469@click.option(
470 "-v",
471 "--verbose",
472 is_flag=True,
473 help=(
474 "Emit messages about files that were not changed or were ignored due to"
475 " exclusion patterns. If Black is using a configuration file, a message"
476 " detailing which one it is using will be emitted."
477 ),
478)
479@click.version_option(
480 version=__version__,
481 message=(
482 f"%(prog)s, %(version)s (compiled: {'yes' if COMPILED else 'no'})\n"
483 f"Python ({platform.python_implementation()}) {platform.python_version()}"
484 ),
485)
486@click.argument(
487 "src",
488 nargs=-1,
489 type=click.Path(
490 exists=True, file_okay=True, dir_okay=True, readable=True, allow_dash=True
491 ),
492 is_eager=True,
493 metavar="SRC ...",
494)
495@click.option(
496 "--config",
497 type=click.Path(
498 exists=True,
499 file_okay=True,
500 dir_okay=False,
501 readable=True,
502 allow_dash=False,
503 path_type=str,
504 ),
505 is_eager=True,
506 callback=read_pyproject_toml,
507 help="Read configuration options from a configuration file.",
508)
509@click.pass_context
510def main( # noqa: C901
511 ctx: click.Context,
512 code: Optional[str],
513 line_length: int,
514 target_version: list[TargetVersion],
515 check: bool,
516 diff: bool,
517 line_ranges: Sequence[str],
518 color: bool,
519 fast: bool,
520 pyi: bool,
521 ipynb: bool,
522 python_cell_magics: Sequence[str],
523 skip_source_first_line: bool,
524 skip_string_normalization: bool,
525 skip_magic_trailing_comma: bool,
526 preview: bool,
527 unstable: bool,
528 enable_unstable_feature: list[Preview],
529 quiet: bool,
530 verbose: bool,
531 required_version: Optional[str],
532 include: Pattern[str],
533 exclude: Optional[Pattern[str]],
534 extend_exclude: Optional[Pattern[str]],
535 force_exclude: Optional[Pattern[str]],
536 stdin_filename: Optional[str],
537 workers: Optional[int],
538 src: tuple[str, ...],
539 config: Optional[str],
540) -> None:
541 """The uncompromising code formatter."""
542 ctx.ensure_object(dict)
544 assert sys.version_info >= (3, 9), "Black requires Python 3.9+"
545 if sys.version_info[:3] == (3, 12, 5):
546 out(
547 "Python 3.12.5 has a memory safety issue that can cause Black's "
548 "AST safety checks to fail. "
549 "Please upgrade to Python 3.12.6 or downgrade to Python 3.12.4"
550 )
551 ctx.exit(1)
553 if src and code is not None:
554 out(
555 main.get_usage(ctx)
556 + "\n\n'SRC' and 'code' cannot be passed simultaneously."
557 )
558 ctx.exit(1)
559 if not src and code is None:
560 out(main.get_usage(ctx) + "\n\nOne of 'SRC' or 'code' is required.")
561 ctx.exit(1)
563 # It doesn't do anything if --unstable is also passed, so just allow it.
564 if enable_unstable_feature and not (preview or unstable):
565 out(
566 main.get_usage(ctx)
567 + "\n\n'--enable-unstable-feature' requires '--preview'."
568 )
569 ctx.exit(1)
571 root, method = (
572 find_project_root(src, stdin_filename) if code is None else (None, None)
573 )
574 ctx.obj["root"] = root
576 if verbose:
577 if root:
578 out(
579 f"Identified `{root}` as project root containing a {method}.",
580 fg="blue",
581 )
583 if config:
584 config_source = ctx.get_parameter_source("config")
585 user_level_config = str(find_user_pyproject_toml())
586 if config == user_level_config:
587 out(
588 "Using configuration from user-level config at "
589 f"'{user_level_config}'.",
590 fg="blue",
591 )
592 elif config_source in (
593 ParameterSource.DEFAULT,
594 ParameterSource.DEFAULT_MAP,
595 ):
596 out("Using configuration from project root.", fg="blue")
597 else:
598 out(f"Using configuration in '{config}'.", fg="blue")
599 if ctx.default_map:
600 for param, value in ctx.default_map.items():
601 out(f"{param}: {value}")
603 error_msg = "Oh no! 💥 💔 💥"
604 if (
605 required_version
606 and required_version != __version__
607 and required_version != __version__.split(".")[0]
608 ):
609 err(
610 f"{error_msg} The required version `{required_version}` does not match"
611 f" the running version `{__version__}`!"
612 )
613 ctx.exit(1)
614 if ipynb and pyi:
615 err("Cannot pass both `pyi` and `ipynb` flags!")
616 ctx.exit(1)
618 write_back = WriteBack.from_configuration(check=check, diff=diff, color=color)
619 if target_version:
620 versions = set(target_version)
621 else:
622 # We'll autodetect later.
623 versions = set()
624 mode = Mode(
625 target_versions=versions,
626 line_length=line_length,
627 is_pyi=pyi,
628 is_ipynb=ipynb,
629 skip_source_first_line=skip_source_first_line,
630 string_normalization=not skip_string_normalization,
631 magic_trailing_comma=not skip_magic_trailing_comma,
632 preview=preview,
633 unstable=unstable,
634 python_cell_magics=set(python_cell_magics),
635 enabled_features=set(enable_unstable_feature),
636 )
638 lines: list[tuple[int, int]] = []
639 if line_ranges:
640 if ipynb:
641 err("Cannot use --line-ranges with ipynb files.")
642 ctx.exit(1)
644 try:
645 lines = parse_line_ranges(line_ranges)
646 except ValueError as e:
647 err(str(e))
648 ctx.exit(1)
650 if code is not None:
651 # Run in quiet mode by default with -c; the extra output isn't useful.
652 # You can still pass -v to get verbose output.
653 quiet = True
655 report = Report(check=check, diff=diff, quiet=quiet, verbose=verbose)
657 if code is not None:
658 reformat_code(
659 content=code,
660 fast=fast,
661 write_back=write_back,
662 mode=mode,
663 report=report,
664 lines=lines,
665 )
666 else:
667 assert root is not None # root is only None if code is not None
668 try:
669 sources = get_sources(
670 root=root,
671 src=src,
672 quiet=quiet,
673 verbose=verbose,
674 include=include,
675 exclude=exclude,
676 extend_exclude=extend_exclude,
677 force_exclude=force_exclude,
678 report=report,
679 stdin_filename=stdin_filename,
680 )
681 except GitWildMatchPatternError:
682 ctx.exit(1)
684 path_empty(
685 sources,
686 "No Python files are present to be formatted. Nothing to do 😴",
687 quiet,
688 verbose,
689 ctx,
690 )
692 if len(sources) == 1:
693 reformat_one(
694 src=sources.pop(),
695 fast=fast,
696 write_back=write_back,
697 mode=mode,
698 report=report,
699 lines=lines,
700 )
701 else:
702 from black.concurrency import reformat_many
704 if lines:
705 err("Cannot use --line-ranges to format multiple files.")
706 ctx.exit(1)
707 reformat_many(
708 sources=sources,
709 fast=fast,
710 write_back=write_back,
711 mode=mode,
712 report=report,
713 workers=workers,
714 )
716 if verbose or not quiet:
717 if code is None and (verbose or report.change_count or report.failure_count):
718 out()
719 out(error_msg if report.return_code else "All done! ✨ 🍰 ✨")
720 if code is None:
721 click.echo(str(report), err=True)
722 ctx.exit(report.return_code)
725def get_sources(
726 *,
727 root: Path,
728 src: tuple[str, ...],
729 quiet: bool,
730 verbose: bool,
731 include: Pattern[str],
732 exclude: Optional[Pattern[str]],
733 extend_exclude: Optional[Pattern[str]],
734 force_exclude: Optional[Pattern[str]],
735 report: "Report",
736 stdin_filename: Optional[str],
737) -> set[Path]:
738 """Compute the set of files to be formatted."""
739 sources: set[Path] = set()
741 assert root.is_absolute(), f"INTERNAL ERROR: `root` must be absolute but is {root}"
742 using_default_exclude = exclude is None
743 exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES) if exclude is None else exclude
744 gitignore: Optional[dict[Path, PathSpec]] = None
745 root_gitignore = get_gitignore(root)
747 for s in src:
748 if s == "-" and stdin_filename:
749 path = Path(stdin_filename)
750 if path_is_excluded(stdin_filename, force_exclude):
751 report.path_ignored(
752 path,
753 "--stdin-filename matches the --force-exclude regular expression",
754 )
755 continue
756 is_stdin = True
757 else:
758 path = Path(s)
759 is_stdin = False
761 # Compare the logic here to the logic in `gen_python_files`.
762 if is_stdin or path.is_file():
763 if resolves_outside_root_or_cannot_stat(path, root, report):
764 if verbose:
765 out(f'Skipping invalid source: "{path}"', fg="red")
766 continue
768 root_relative_path = best_effort_relative_path(path, root).as_posix()
769 root_relative_path = "/" + root_relative_path
771 # Hard-exclude any files that matches the `--force-exclude` regex.
772 if path_is_excluded(root_relative_path, force_exclude):
773 report.path_ignored(
774 path, "matches the --force-exclude regular expression"
775 )
776 continue
778 if is_stdin:
779 path = Path(f"{STDIN_PLACEHOLDER}{path}")
781 if path.suffix == ".ipynb" and not jupyter_dependencies_are_installed(
782 warn=verbose or not quiet
783 ):
784 continue
786 if verbose:
787 out(f'Found input source: "{path}"', fg="blue")
788 sources.add(path)
789 elif path.is_dir():
790 path = root / (path.resolve().relative_to(root))
791 if verbose:
792 out(f'Found input source directory: "{path}"', fg="blue")
794 if using_default_exclude:
795 gitignore = {
796 root: root_gitignore,
797 path: get_gitignore(path),
798 }
799 sources.update(
800 gen_python_files(
801 path.iterdir(),
802 root,
803 include,
804 exclude,
805 extend_exclude,
806 force_exclude,
807 report,
808 gitignore,
809 verbose=verbose,
810 quiet=quiet,
811 )
812 )
813 elif s == "-":
814 if verbose:
815 out("Found input source stdin", fg="blue")
816 sources.add(path)
817 else:
818 err(f"invalid path: {s}")
820 return sources
823def path_empty(
824 src: Sized, msg: str, quiet: bool, verbose: bool, ctx: click.Context
825) -> None:
826 """
827 Exit if there is no `src` provided for formatting
828 """
829 if not src:
830 if verbose or not quiet:
831 out(msg)
832 ctx.exit(0)
835def reformat_code(
836 content: str,
837 fast: bool,
838 write_back: WriteBack,
839 mode: Mode,
840 report: Report,
841 *,
842 lines: Collection[tuple[int, int]] = (),
843) -> None:
844 """
845 Reformat and print out `content` without spawning child processes.
846 Similar to `reformat_one`, but for string content.
848 `fast`, `write_back`, and `mode` options are passed to
849 :func:`format_file_in_place` or :func:`format_stdin_to_stdout`.
850 """
851 path = Path("<string>")
852 try:
853 changed = Changed.NO
854 if format_stdin_to_stdout(
855 content=content, fast=fast, write_back=write_back, mode=mode, lines=lines
856 ):
857 changed = Changed.YES
858 report.done(path, changed)
859 except Exception as exc:
860 if report.verbose:
861 traceback.print_exc()
862 report.failed(path, str(exc))
865# diff-shades depends on being to monkeypatch this function to operate. I know it's
866# not ideal, but this shouldn't cause any issues ... hopefully. ~ichard26
867@mypyc_attr(patchable=True)
868def reformat_one(
869 src: Path,
870 fast: bool,
871 write_back: WriteBack,
872 mode: Mode,
873 report: "Report",
874 *,
875 lines: Collection[tuple[int, int]] = (),
876) -> None:
877 """Reformat a single file under `src` without spawning child processes.
879 `fast`, `write_back`, and `mode` options are passed to
880 :func:`format_file_in_place` or :func:`format_stdin_to_stdout`.
881 """
882 try:
883 changed = Changed.NO
885 if str(src) == "-":
886 is_stdin = True
887 elif str(src).startswith(STDIN_PLACEHOLDER):
888 is_stdin = True
889 # Use the original name again in case we want to print something
890 # to the user
891 src = Path(str(src)[len(STDIN_PLACEHOLDER) :])
892 else:
893 is_stdin = False
895 if is_stdin:
896 if src.suffix == ".pyi":
897 mode = replace(mode, is_pyi=True)
898 elif src.suffix == ".ipynb":
899 mode = replace(mode, is_ipynb=True)
900 if format_stdin_to_stdout(
901 fast=fast, write_back=write_back, mode=mode, lines=lines
902 ):
903 changed = Changed.YES
904 else:
905 cache = Cache.read(mode)
906 if write_back not in (WriteBack.DIFF, WriteBack.COLOR_DIFF):
907 if not cache.is_changed(src):
908 changed = Changed.CACHED
909 if changed is not Changed.CACHED and format_file_in_place(
910 src, fast=fast, write_back=write_back, mode=mode, lines=lines
911 ):
912 changed = Changed.YES
913 if (write_back is WriteBack.YES and changed is not Changed.CACHED) or (
914 write_back is WriteBack.CHECK and changed is Changed.NO
915 ):
916 cache.write([src])
917 report.done(src, changed)
918 except Exception as exc:
919 if report.verbose:
920 traceback.print_exc()
921 report.failed(src, str(exc))
924def format_file_in_place(
925 src: Path,
926 fast: bool,
927 mode: Mode,
928 write_back: WriteBack = WriteBack.NO,
929 lock: Any = None, # multiprocessing.Manager().Lock() is some crazy proxy
930 *,
931 lines: Collection[tuple[int, int]] = (),
932) -> bool:
933 """Format file under `src` path. Return True if changed.
935 If `write_back` is DIFF, write a diff to stdout. If it is YES, write reformatted
936 code to the file.
937 `mode` and `fast` options are passed to :func:`format_file_contents`.
938 """
939 if src.suffix == ".pyi":
940 mode = replace(mode, is_pyi=True)
941 elif src.suffix == ".ipynb":
942 mode = replace(mode, is_ipynb=True)
944 then = datetime.fromtimestamp(src.stat().st_mtime, timezone.utc)
945 header = b""
946 with open(src, "rb") as buf:
947 if mode.skip_source_first_line:
948 header = buf.readline()
949 src_contents, encoding, newline = decode_bytes(buf.read())
950 try:
951 dst_contents = format_file_contents(
952 src_contents, fast=fast, mode=mode, lines=lines
953 )
954 except NothingChanged:
955 return False
956 except JSONDecodeError:
957 raise ValueError(
958 f"File '{src}' cannot be parsed as valid Jupyter notebook."
959 ) from None
960 src_contents = header.decode(encoding) + src_contents
961 dst_contents = header.decode(encoding) + dst_contents
963 if write_back == WriteBack.YES:
964 with open(src, "w", encoding=encoding, newline=newline) as f:
965 f.write(dst_contents)
966 elif write_back in (WriteBack.DIFF, WriteBack.COLOR_DIFF):
967 now = datetime.now(timezone.utc)
968 src_name = f"{src}\t{then}"
969 dst_name = f"{src}\t{now}"
970 if mode.is_ipynb:
971 diff_contents = ipynb_diff(src_contents, dst_contents, src_name, dst_name)
972 else:
973 diff_contents = diff(src_contents, dst_contents, src_name, dst_name)
975 if write_back == WriteBack.COLOR_DIFF:
976 diff_contents = color_diff(diff_contents)
978 with lock or nullcontext():
979 f = io.TextIOWrapper(
980 sys.stdout.buffer,
981 encoding=encoding,
982 newline=newline,
983 write_through=True,
984 )
985 f = wrap_stream_for_windows(f)
986 f.write(diff_contents)
987 f.detach()
989 return True
992def format_stdin_to_stdout(
993 fast: bool,
994 *,
995 content: Optional[str] = None,
996 write_back: WriteBack = WriteBack.NO,
997 mode: Mode,
998 lines: Collection[tuple[int, int]] = (),
999) -> bool:
1000 """Format file on stdin. Return True if changed.
1002 If content is None, it's read from sys.stdin.
1004 If `write_back` is YES, write reformatted code back to stdout. If it is DIFF,
1005 write a diff to stdout. The `mode` argument is passed to
1006 :func:`format_file_contents`.
1007 """
1008 then = datetime.now(timezone.utc)
1010 if content is None:
1011 src, encoding, newline = decode_bytes(sys.stdin.buffer.read())
1012 else:
1013 src, encoding, newline = content, "utf-8", ""
1015 dst = src
1016 try:
1017 dst = format_file_contents(src, fast=fast, mode=mode, lines=lines)
1018 return True
1020 except NothingChanged:
1021 return False
1023 finally:
1024 f = io.TextIOWrapper(
1025 sys.stdout.buffer, encoding=encoding, newline=newline, write_through=True
1026 )
1027 if write_back == WriteBack.YES:
1028 # Make sure there's a newline after the content
1029 if dst and dst[-1] != "\n":
1030 dst += "\n"
1031 f.write(dst)
1032 elif write_back in (WriteBack.DIFF, WriteBack.COLOR_DIFF):
1033 now = datetime.now(timezone.utc)
1034 src_name = f"STDIN\t{then}"
1035 dst_name = f"STDOUT\t{now}"
1036 d = diff(src, dst, src_name, dst_name)
1037 if write_back == WriteBack.COLOR_DIFF:
1038 d = color_diff(d)
1039 f = wrap_stream_for_windows(f)
1040 f.write(d)
1041 f.detach()
1044def check_stability_and_equivalence(
1045 src_contents: str,
1046 dst_contents: str,
1047 *,
1048 mode: Mode,
1049 lines: Collection[tuple[int, int]] = (),
1050) -> None:
1051 """Perform stability and equivalence checks.
1053 Raise AssertionError if source and destination contents are not
1054 equivalent, or if a second pass of the formatter would format the
1055 content differently.
1056 """
1057 assert_equivalent(src_contents, dst_contents)
1058 assert_stable(src_contents, dst_contents, mode=mode, lines=lines)
1061def format_file_contents(
1062 src_contents: str,
1063 *,
1064 fast: bool,
1065 mode: Mode,
1066 lines: Collection[tuple[int, int]] = (),
1067) -> FileContent:
1068 """Reformat contents of a file and return new contents.
1070 If `fast` is False, additionally confirm that the reformatted code is
1071 valid by calling :func:`assert_equivalent` and :func:`assert_stable` on it.
1072 `mode` is passed to :func:`format_str`.
1073 """
1074 if mode.is_ipynb:
1075 dst_contents = format_ipynb_string(src_contents, fast=fast, mode=mode)
1076 else:
1077 dst_contents = format_str(src_contents, mode=mode, lines=lines)
1078 if src_contents == dst_contents:
1079 raise NothingChanged
1081 if not fast and not mode.is_ipynb:
1082 # Jupyter notebooks will already have been checked above.
1083 check_stability_and_equivalence(
1084 src_contents, dst_contents, mode=mode, lines=lines
1085 )
1086 return dst_contents
1089def format_cell(src: str, *, fast: bool, mode: Mode) -> str:
1090 """Format code in given cell of Jupyter notebook.
1092 General idea is:
1094 - if cell has trailing semicolon, remove it;
1095 - if cell has IPython magics, mask them;
1096 - format cell;
1097 - reinstate IPython magics;
1098 - reinstate trailing semicolon (if originally present);
1099 - strip trailing newlines.
1101 Cells with syntax errors will not be processed, as they
1102 could potentially be automagics or multi-line magics, which
1103 are currently not supported.
1104 """
1105 validate_cell(src, mode)
1106 src_without_trailing_semicolon, has_trailing_semicolon = remove_trailing_semicolon(
1107 src
1108 )
1109 try:
1110 masked_src, replacements = mask_cell(src_without_trailing_semicolon)
1111 except SyntaxError:
1112 raise NothingChanged from None
1113 masked_dst = format_str(masked_src, mode=mode)
1114 if not fast:
1115 check_stability_and_equivalence(masked_src, masked_dst, mode=mode)
1116 dst_without_trailing_semicolon = unmask_cell(masked_dst, replacements)
1117 dst = put_trailing_semicolon_back(
1118 dst_without_trailing_semicolon, has_trailing_semicolon
1119 )
1120 dst = dst.rstrip("\n")
1121 if dst == src:
1122 raise NothingChanged from None
1123 return dst
1126def validate_metadata(nb: MutableMapping[str, Any]) -> None:
1127 """If notebook is marked as non-Python, don't format it.
1129 All notebook metadata fields are optional, see
1130 https://nbformat.readthedocs.io/en/latest/format_description.html. So
1131 if a notebook has empty metadata, we will try to parse it anyway.
1132 """
1133 language = nb.get("metadata", {}).get("language_info", {}).get("name", None)
1134 if language is not None and language != "python":
1135 raise NothingChanged from None
1138def format_ipynb_string(src_contents: str, *, fast: bool, mode: Mode) -> FileContent:
1139 """Format Jupyter notebook.
1141 Operate cell-by-cell, only on code cells, only for Python notebooks.
1142 If the ``.ipynb`` originally had a trailing newline, it'll be preserved.
1143 """
1144 if not src_contents:
1145 raise NothingChanged
1147 trailing_newline = src_contents[-1] == "\n"
1148 modified = False
1149 nb = json.loads(src_contents)
1150 validate_metadata(nb)
1151 for cell in nb["cells"]:
1152 if cell.get("cell_type", None) == "code":
1153 try:
1154 src = "".join(cell["source"])
1155 dst = format_cell(src, fast=fast, mode=mode)
1156 except NothingChanged:
1157 pass
1158 else:
1159 cell["source"] = dst.splitlines(keepends=True)
1160 modified = True
1161 if modified:
1162 dst_contents = json.dumps(nb, indent=1, ensure_ascii=False)
1163 if trailing_newline:
1164 dst_contents = dst_contents + "\n"
1165 return dst_contents
1166 else:
1167 raise NothingChanged
1170def format_str(
1171 src_contents: str, *, mode: Mode, lines: Collection[tuple[int, int]] = ()
1172) -> str:
1173 """Reformat a string and return new contents.
1175 `mode` determines formatting options, such as how many characters per line are
1176 allowed. Example:
1178 >>> import black
1179 >>> print(black.format_str("def f(arg:str='')->None:...", mode=black.Mode()))
1180 def f(arg: str = "") -> None:
1181 ...
1183 A more complex example:
1185 >>> print(
1186 ... black.format_str(
1187 ... "def f(arg:str='')->None: hey",
1188 ... mode=black.Mode(
1189 ... target_versions={black.TargetVersion.PY36},
1190 ... line_length=10,
1191 ... string_normalization=False,
1192 ... is_pyi=False,
1193 ... ),
1194 ... ),
1195 ... )
1196 def f(
1197 arg: str = '',
1198 ) -> None:
1199 hey
1201 """
1202 if lines:
1203 lines = sanitized_lines(lines, src_contents)
1204 if not lines:
1205 return src_contents # Nothing to format
1206 dst_contents = _format_str_once(src_contents, mode=mode, lines=lines)
1207 # Forced second pass to work around optional trailing commas (becoming
1208 # forced trailing commas on pass 2) interacting differently with optional
1209 # parentheses. Admittedly ugly.
1210 if src_contents != dst_contents:
1211 if lines:
1212 lines = adjusted_lines(lines, src_contents, dst_contents)
1213 return _format_str_once(dst_contents, mode=mode, lines=lines)
1214 return dst_contents
1217def _format_str_once(
1218 src_contents: str, *, mode: Mode, lines: Collection[tuple[int, int]] = ()
1219) -> str:
1220 src_node = lib2to3_parse(src_contents.lstrip(), mode.target_versions)
1221 dst_blocks: list[LinesBlock] = []
1222 if mode.target_versions:
1223 versions = mode.target_versions
1224 else:
1225 future_imports = get_future_imports(src_node)
1226 versions = detect_target_versions(src_node, future_imports=future_imports)
1228 context_manager_features = {
1229 feature
1230 for feature in {Feature.PARENTHESIZED_CONTEXT_MANAGERS}
1231 if supports_feature(versions, feature)
1232 }
1233 normalize_fmt_off(src_node, mode, lines)
1234 if lines:
1235 # This should be called after normalize_fmt_off.
1236 convert_unchanged_lines(src_node, lines)
1238 line_generator = LineGenerator(mode=mode, features=context_manager_features)
1239 elt = EmptyLineTracker(mode=mode)
1240 split_line_features = {
1241 feature
1242 for feature in {
1243 Feature.TRAILING_COMMA_IN_CALL,
1244 Feature.TRAILING_COMMA_IN_DEF,
1245 }
1246 if supports_feature(versions, feature)
1247 }
1248 block: Optional[LinesBlock] = None
1249 for current_line in line_generator.visit(src_node):
1250 block = elt.maybe_empty_lines(current_line)
1251 dst_blocks.append(block)
1252 for line in transform_line(
1253 current_line, mode=mode, features=split_line_features
1254 ):
1255 block.content_lines.append(str(line))
1256 if dst_blocks:
1257 dst_blocks[-1].after = 0
1258 dst_contents = []
1259 for block in dst_blocks:
1260 dst_contents.extend(block.all_lines())
1261 if not dst_contents:
1262 # Use decode_bytes to retrieve the correct source newline (CRLF or LF),
1263 # and check if normalized_content has more than one line
1264 normalized_content, _, newline = decode_bytes(src_contents.encode("utf-8"))
1265 if "\n" in normalized_content:
1266 return newline
1267 return ""
1268 return "".join(dst_contents)
1271def decode_bytes(src: bytes) -> tuple[FileContent, Encoding, NewLine]:
1272 """Return a tuple of (decoded_contents, encoding, newline).
1274 `newline` is either CRLF or LF but `decoded_contents` is decoded with
1275 universal newlines (i.e. only contains LF).
1276 """
1277 srcbuf = io.BytesIO(src)
1278 encoding, lines = tokenize.detect_encoding(srcbuf.readline)
1279 if not lines:
1280 return "", encoding, "\n"
1282 newline = "\r\n" if lines[0][-2:] == b"\r\n" else "\n"
1283 srcbuf.seek(0)
1284 with io.TextIOWrapper(srcbuf, encoding) as tiow:
1285 return tiow.read(), encoding, newline
1288def get_features_used( # noqa: C901
1289 node: Node, *, future_imports: Optional[set[str]] = None
1290) -> set[Feature]:
1291 """Return a set of (relatively) new Python features used in this file.
1293 Currently looking for:
1294 - f-strings;
1295 - self-documenting expressions in f-strings (f"{x=}");
1296 - underscores in numeric literals;
1297 - trailing commas after * or ** in function signatures and calls;
1298 - positional only arguments in function signatures and lambdas;
1299 - assignment expression;
1300 - relaxed decorator syntax;
1301 - usage of __future__ flags (annotations);
1302 - print / exec statements;
1303 - parenthesized context managers;
1304 - match statements;
1305 - except* clause;
1306 - variadic generics;
1307 """
1308 features: set[Feature] = set()
1309 if future_imports:
1310 features |= {
1311 FUTURE_FLAG_TO_FEATURE[future_import]
1312 for future_import in future_imports
1313 if future_import in FUTURE_FLAG_TO_FEATURE
1314 }
1316 for n in node.pre_order():
1317 if n.type == token.FSTRING_START:
1318 features.add(Feature.F_STRINGS)
1319 elif (
1320 n.type == token.RBRACE
1321 and n.parent is not None
1322 and any(child.type == token.EQUAL for child in n.parent.children)
1323 ):
1324 features.add(Feature.DEBUG_F_STRINGS)
1326 elif is_number_token(n):
1327 if "_" in n.value:
1328 features.add(Feature.NUMERIC_UNDERSCORES)
1330 elif n.type == token.SLASH:
1331 if n.parent and n.parent.type in {
1332 syms.typedargslist,
1333 syms.arglist,
1334 syms.varargslist,
1335 }:
1336 features.add(Feature.POS_ONLY_ARGUMENTS)
1338 elif n.type == token.COLONEQUAL:
1339 features.add(Feature.ASSIGNMENT_EXPRESSIONS)
1341 elif n.type == syms.decorator:
1342 if len(n.children) > 1 and not is_simple_decorator_expression(
1343 n.children[1]
1344 ):
1345 features.add(Feature.RELAXED_DECORATORS)
1347 elif (
1348 n.type in {syms.typedargslist, syms.arglist}
1349 and n.children
1350 and n.children[-1].type == token.COMMA
1351 ):
1352 if n.type == syms.typedargslist:
1353 feature = Feature.TRAILING_COMMA_IN_DEF
1354 else:
1355 feature = Feature.TRAILING_COMMA_IN_CALL
1357 for ch in n.children:
1358 if ch.type in STARS:
1359 features.add(feature)
1361 if ch.type == syms.argument:
1362 for argch in ch.children:
1363 if argch.type in STARS:
1364 features.add(feature)
1366 elif (
1367 n.type in {syms.return_stmt, syms.yield_expr}
1368 and len(n.children) >= 2
1369 and n.children[1].type == syms.testlist_star_expr
1370 and any(child.type == syms.star_expr for child in n.children[1].children)
1371 ):
1372 features.add(Feature.UNPACKING_ON_FLOW)
1374 elif (
1375 n.type == syms.annassign
1376 and len(n.children) >= 4
1377 and n.children[3].type == syms.testlist_star_expr
1378 ):
1379 features.add(Feature.ANN_ASSIGN_EXTENDED_RHS)
1381 elif (
1382 n.type == syms.with_stmt
1383 and len(n.children) > 2
1384 and n.children[1].type == syms.atom
1385 ):
1386 atom_children = n.children[1].children
1387 if (
1388 len(atom_children) == 3
1389 and atom_children[0].type == token.LPAR
1390 and _contains_asexpr(atom_children[1])
1391 and atom_children[2].type == token.RPAR
1392 ):
1393 features.add(Feature.PARENTHESIZED_CONTEXT_MANAGERS)
1395 elif n.type == syms.match_stmt:
1396 features.add(Feature.PATTERN_MATCHING)
1398 elif (
1399 n.type == syms.except_clause
1400 and len(n.children) >= 2
1401 and n.children[1].type == token.STAR
1402 ):
1403 features.add(Feature.EXCEPT_STAR)
1405 elif n.type in {syms.subscriptlist, syms.trailer} and any(
1406 child.type == syms.star_expr for child in n.children
1407 ):
1408 features.add(Feature.VARIADIC_GENERICS)
1410 elif (
1411 n.type == syms.tname_star
1412 and len(n.children) == 3
1413 and n.children[2].type == syms.star_expr
1414 ):
1415 features.add(Feature.VARIADIC_GENERICS)
1417 elif n.type in (syms.type_stmt, syms.typeparams):
1418 features.add(Feature.TYPE_PARAMS)
1420 elif (
1421 n.type in (syms.typevartuple, syms.paramspec, syms.typevar)
1422 and n.children[-2].type == token.EQUAL
1423 ):
1424 features.add(Feature.TYPE_PARAM_DEFAULTS)
1426 return features
1429def _contains_asexpr(node: Union[Node, Leaf]) -> bool:
1430 """Return True if `node` contains an as-pattern."""
1431 if node.type == syms.asexpr_test:
1432 return True
1433 elif node.type == syms.atom:
1434 if (
1435 len(node.children) == 3
1436 and node.children[0].type == token.LPAR
1437 and node.children[2].type == token.RPAR
1438 ):
1439 return _contains_asexpr(node.children[1])
1440 elif node.type == syms.testlist_gexp:
1441 return any(_contains_asexpr(child) for child in node.children)
1442 return False
1445def detect_target_versions(
1446 node: Node, *, future_imports: Optional[set[str]] = None
1447) -> set[TargetVersion]:
1448 """Detect the version to target based on the nodes used."""
1449 features = get_features_used(node, future_imports=future_imports)
1450 return {
1451 version for version in TargetVersion if features <= VERSION_TO_FEATURES[version]
1452 }
1455def get_future_imports(node: Node) -> set[str]:
1456 """Return a set of __future__ imports in the file."""
1457 imports: set[str] = set()
1459 def get_imports_from_children(children: list[LN]) -> Generator[str, None, None]:
1460 for child in children:
1461 if isinstance(child, Leaf):
1462 if child.type == token.NAME:
1463 yield child.value
1465 elif child.type == syms.import_as_name:
1466 orig_name = child.children[0]
1467 assert isinstance(orig_name, Leaf), "Invalid syntax parsing imports"
1468 assert orig_name.type == token.NAME, "Invalid syntax parsing imports"
1469 yield orig_name.value
1471 elif child.type == syms.import_as_names:
1472 yield from get_imports_from_children(child.children)
1474 else:
1475 raise AssertionError("Invalid syntax parsing imports")
1477 for child in node.children:
1478 if child.type != syms.simple_stmt:
1479 break
1481 first_child = child.children[0]
1482 if isinstance(first_child, Leaf):
1483 # Continue looking if we see a docstring; otherwise stop.
1484 if (
1485 len(child.children) == 2
1486 and first_child.type == token.STRING
1487 and child.children[1].type == token.NEWLINE
1488 ):
1489 continue
1491 break
1493 elif first_child.type == syms.import_from:
1494 module_name = first_child.children[1]
1495 if not isinstance(module_name, Leaf) or module_name.value != "__future__":
1496 break
1498 imports |= set(get_imports_from_children(first_child.children[3:]))
1499 else:
1500 break
1502 return imports
1505def _black_info() -> str:
1506 return (
1507 f"Black {__version__} on "
1508 f"Python ({platform.python_implementation()}) {platform.python_version()}"
1509 )
1512def assert_equivalent(src: str, dst: str) -> None:
1513 """Raise AssertionError if `src` and `dst` aren't equivalent."""
1514 try:
1515 src_ast = parse_ast(src)
1516 except Exception as exc:
1517 raise ASTSafetyError(
1518 "cannot use --safe with this file; failed to parse source file AST: "
1519 f"{exc}\n"
1520 "This could be caused by running Black with an older Python version "
1521 "that does not support new syntax used in your source file."
1522 ) from exc
1524 try:
1525 dst_ast = parse_ast(dst)
1526 except Exception as exc:
1527 log = dump_to_file("".join(traceback.format_tb(exc.__traceback__)), dst)
1528 raise ASTSafetyError(
1529 f"INTERNAL ERROR: {_black_info()} produced invalid code: {exc}. "
1530 "Please report a bug on https://github.com/psf/black/issues. "
1531 f"This invalid output might be helpful: {log}"
1532 ) from None
1534 src_ast_str = "\n".join(stringify_ast(src_ast))
1535 dst_ast_str = "\n".join(stringify_ast(dst_ast))
1536 if src_ast_str != dst_ast_str:
1537 log = dump_to_file(diff(src_ast_str, dst_ast_str, "src", "dst"))
1538 raise ASTSafetyError(
1539 f"INTERNAL ERROR: {_black_info()} produced code that is not equivalent to"
1540 " the source. Please report a bug on https://github.com/psf/black/issues."
1541 f" This diff might be helpful: {log}"
1542 ) from None
1545def assert_stable(
1546 src: str, dst: str, mode: Mode, *, lines: Collection[tuple[int, int]] = ()
1547) -> None:
1548 """Raise AssertionError if `dst` reformats differently the second time."""
1549 if lines:
1550 # Formatting specified lines requires `adjusted_lines` to map original lines
1551 # to the formatted lines before re-formatting the previously formatted result.
1552 # Due to less-ideal diff algorithm, some edge cases produce incorrect new line
1553 # ranges. Hence for now, we skip the stable check.
1554 # See https://github.com/psf/black/issues/4033 for context.
1555 return
1556 # We shouldn't call format_str() here, because that formats the string
1557 # twice and may hide a bug where we bounce back and forth between two
1558 # versions.
1559 newdst = _format_str_once(dst, mode=mode, lines=lines)
1560 if dst != newdst:
1561 log = dump_to_file(
1562 str(mode),
1563 diff(src, dst, "source", "first pass"),
1564 diff(dst, newdst, "first pass", "second pass"),
1565 )
1566 raise AssertionError(
1567 f"INTERNAL ERROR: {_black_info()} produced different code on the second"
1568 " pass of the formatter. Please report a bug on"
1569 f" https://github.com/psf/black/issues. This diff might be helpful: {log}"
1570 ) from None
1573@contextmanager
1574def nullcontext() -> Iterator[None]:
1575 """Return an empty context manager.
1577 To be used like `nullcontext` in Python 3.7.
1578 """
1579 yield
1582def patched_main() -> None:
1583 # PyInstaller patches multiprocessing to need freeze_support() even in non-Windows
1584 # environments so just assume we always need to call it if frozen.
1585 if getattr(sys, "frozen", False):
1586 from multiprocessing import freeze_support
1588 freeze_support()
1590 main()
1593if __name__ == "__main__":
1594 patched_main()