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.
8 from contextlib import contextmanager
9 from dataclasses import replace
10 from datetime import datetime
12 from json.decoder import JSONDecodeError
13 from pathlib import Path
31 from click.core import ParameterSource
32 from mypy_extensions import mypyc_attr
33 from pathspec.patterns.gitwildmatch import GitWildMatchPatternError
35 from _black_version import version as __version__
36 from black.cache import Cache, get_cache_info, read_cache, write_cache
37 from black.comments import normalize_fmt_off
38 from black.const import (
44 from black.files import (
47 find_user_pyproject_toml,
50 normalize_path_maybe_ignore,
52 wrap_stream_for_windows,
54 from black.handle_ipynb_magics import (
57 jupyter_dependencies_are_installed,
59 put_trailing_semicolon_back,
60 remove_trailing_semicolon,
63 from black.linegen import LN, LineGenerator, transform_line
64 from black.lines import EmptyLineTracker, Line
65 from black.mode import (
66 FUTURE_FLAG_TO_FEATURE,
73 from black.nodes import (
76 is_simple_decorator_expression,
80 from black.output import color_diff, diff, dump_to_file, err, ipynb_diff, out
81 from black.parsing import InvalidInput # noqa F401
82 from black.parsing import lib2to3_parse, parse_ast, stringify_ast
83 from black.report import Changed, NothingChanged, Report
84 from black.trans import iter_fexpr_spans
85 from blib2to3.pgen2 import token
86 from blib2to3.pytree import Leaf, Node
88 COMPILED = Path(__file__).suffix in (".pyd", ".so")
96 class WriteBack(Enum):
104 def from_configuration(
105 cls, *, check: bool, diff: bool, color: bool = False
107 if check and not diff:
111 return cls.COLOR_DIFF
113 return cls.DIFF if diff else cls.YES
116 # Legacy name, left for integrations.
120 def read_pyproject_toml(
121 ctx: click.Context, param: click.Parameter, value: Optional[str]
123 """Inject Black configuration from "pyproject.toml" into defaults in `ctx`.
125 Returns the path to a successfully found and read configuration file, None
129 value = find_pyproject_toml(ctx.params.get("src", ()))
134 config = parse_pyproject_toml(value)
135 except (OSError, ValueError) as e:
136 raise click.FileError(
137 filename=value, hint=f"Error reading configuration file: {e}"
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
147 k: str(v) if not isinstance(v, (list, dict)) else v
148 for k, v in config.items()
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"
157 default_map: Dict[str, Any] = {}
159 default_map.update(ctx.default_map)
160 default_map.update(config)
162 ctx.default_map = default_map
166 def target_version_option_callback(
167 c: click.Context, p: Union[click.Option, click.Parameter], v: Tuple[str, ...]
168 ) -> List[TargetVersion]:
169 """Compute the target versions from a --target-version flag.
171 This is its own function because mypy couldn't infer the type correctly
172 when it was a lambda, causing mypyc trouble.
174 return [TargetVersion[val.upper()] for val in v]
177 def re_compile_maybe_verbose(regex: str) -> Pattern[str]:
178 """Compile a regular expression string in `regex`.
180 If it contains newlines, use verbose mode.
183 regex = "(?x)" + regex
184 compiled: Pattern[str] = re.compile(regex)
190 param: click.Parameter,
191 value: Optional[str],
192 ) -> Optional[Pattern[str]]:
194 return re_compile_maybe_verbose(value) if value is not None else None
195 except re.error as e:
196 raise click.BadParameter(f"Not a valid regular expression: {e}") from None
200 context_settings={"help_option_names": ["-h", "--help"]},
201 # While Click does set this field automatically using the docstring, mypyc
202 # (annoyingly) strips 'em so we need to set it here too.
203 help="The uncompromising code formatter.",
205 @click.option("-c", "--code", type=str, help="Format the code passed in as a string.")
210 default=DEFAULT_LINE_LENGTH,
211 help="How many characters per line to allow.",
217 type=click.Choice([v.name.lower() for v in TargetVersion]),
218 callback=target_version_option_callback,
221 "Python versions that should be supported by Black's output. [default: per-file"
229 "Format all input files like typing stubs regardless of file extension (useful"
230 " when piping source on standard input)."
237 "Format all input files like Jupyter Notebooks regardless of file extension "
238 "(useful when piping source on standard input)."
242 "--python-cell-magics",
245 "When processing Jupyter Notebooks, add the given magic to the list"
246 f" of known python-magics ({', '.join(PYTHON_CELL_MAGICS)})."
247 " Useful for formatting cells with custom python magics."
253 "--skip-source-first-line",
255 help="Skip the first line of the source code.",
259 "--skip-string-normalization",
261 help="Don't normalize string quotes or prefixes.",
265 "--skip-magic-trailing-comma",
267 help="Don't use trailing commas as a reason to split lines.",
270 "--experimental-string-processing",
273 help="(DEPRECATED and now included in --preview) Normalize string literals.",
279 "Enable potentially disruptive style changes that may be added to Black's main"
280 " functionality in the next major release."
287 "Don't write the files back, just return the status. Return code 0 means"
288 " nothing would change. Return code 1 means some files would be reformatted."
289 " Return code 123 means there was an internal error."
295 help="Don't write the files back, just output a diff for each file on stdout.",
298 "--color/--no-color",
300 help="Show colored diff. Only applies when `--diff` is given.",
305 help="If --fast given, skip temporary sanity checks. [default: --safe]",
308 "--required-version",
311 "Require a specific version of Black to be running (useful for unifying results"
312 " across many environments e.g. with a pyproject.toml file). It can be"
313 " either a major version number or an exact version."
319 default=DEFAULT_INCLUDES,
320 callback=validate_regex,
322 "A regular expression that matches files and directories that should be"
323 " included on recursive searches. An empty value means all files are included"
324 " regardless of the name. Use forward slashes for directories on all platforms"
325 " (Windows, too). Exclusions are calculated first, inclusions later."
332 callback=validate_regex,
334 "A regular expression that matches files and directories that should be"
335 " excluded on recursive searches. An empty value means no paths are excluded."
336 " Use forward slashes for directories on all platforms (Windows, too)."
337 " Exclusions are calculated first, inclusions later. [default:"
338 f" {DEFAULT_EXCLUDES}]"
345 callback=validate_regex,
347 "Like --exclude, but adds additional files and directories on top of the"
348 " excluded ones. (Useful if you simply want to add to the default)"
354 callback=validate_regex,
356 "Like --exclude, but files and directories matching this regex will be "
357 "excluded even when they are passed explicitly as arguments."
364 "The name of the file when passing it through stdin. Useful to make "
365 "sure Black will respect --force-exclude option on some "
366 "editors that rely on using stdin."
372 type=click.IntRange(min=1),
374 help="Number of parallel workers [default: number of CPUs in the system]",
381 "Don't emit non-error messages to stderr. Errors are still emitted; silence"
382 " those with 2>/dev/null."
390 "Also emit messages to stderr about files that were not changed or were ignored"
391 " due to exclusion patterns."
394 @click.version_option(
397 f"%(prog)s, %(version)s (compiled: {'yes' if COMPILED else 'no'})\n"
398 f"Python ({platform.python_implementation()}) {platform.python_version()}"
405 exists=True, file_okay=True, dir_okay=True, readable=True, allow_dash=True
421 callback=read_pyproject_toml,
422 help="Read configuration from FILE path.",
425 def main( # noqa: C901
429 target_version: List[TargetVersion],
436 python_cell_magics: Sequence[str],
437 skip_source_first_line: bool,
438 skip_string_normalization: bool,
439 skip_magic_trailing_comma: bool,
440 experimental_string_processing: bool,
444 required_version: Optional[str],
445 include: Pattern[str],
446 exclude: Optional[Pattern[str]],
447 extend_exclude: Optional[Pattern[str]],
448 force_exclude: Optional[Pattern[str]],
449 stdin_filename: Optional[str],
450 workers: Optional[int],
451 src: Tuple[str, ...],
452 config: Optional[str],
454 """The uncompromising code formatter."""
455 ctx.ensure_object(dict)
457 if src and code is not None:
460 + "\n\n'SRC' and 'code' cannot be passed simultaneously."
463 if not src and code is None:
464 out(main.get_usage(ctx) + "\n\nOne of 'SRC' or 'code' is required.")
468 find_project_root(src, stdin_filename) if code is None else (None, None)
470 ctx.obj["root"] = root
475 f"Identified `{root}` as project root containing a {method}.",
482 else (normalize_path_maybe_ignore(Path(source), root), source)
485 srcs_string = ", ".join(
489 else f'\033[31m"{source} (skipping - invalid)"\033[34m'
490 for _norm, source in normalized
493 out(f"Sources to be formatted: {srcs_string}", fg="blue")
496 config_source = ctx.get_parameter_source("config")
497 user_level_config = str(find_user_pyproject_toml())
498 if config == user_level_config:
500 "Using configuration from user-level config at "
501 f"'{user_level_config}'.",
504 elif config_source in (
505 ParameterSource.DEFAULT,
506 ParameterSource.DEFAULT_MAP,
508 out("Using configuration from project root.", fg="blue")
510 out(f"Using configuration in '{config}'.", fg="blue")
512 error_msg = "Oh no! 💥 💔 💥"
515 and required_version != __version__
516 and required_version != __version__.split(".")[0]
519 f"{error_msg} The required version `{required_version}` does not match"
520 f" the running version `{__version__}`!"
524 err("Cannot pass both `pyi` and `ipynb` flags!")
527 write_back = WriteBack.from_configuration(check=check, diff=diff, color=color)
529 versions = set(target_version)
531 # We'll autodetect later.
534 target_versions=versions,
535 line_length=line_length,
538 skip_source_first_line=skip_source_first_line,
539 string_normalization=not skip_string_normalization,
540 magic_trailing_comma=not skip_magic_trailing_comma,
541 experimental_string_processing=experimental_string_processing,
543 python_cell_magics=set(python_cell_magics),
547 # Run in quiet mode by default with -c; the extra output isn't useful.
548 # You can still pass -v to get verbose output.
551 report = Report(check=check, diff=diff, quiet=quiet, verbose=verbose)
555 content=code, fast=fast, write_back=write_back, mode=mode, report=report
559 sources = get_sources(
566 extend_exclude=extend_exclude,
567 force_exclude=force_exclude,
569 stdin_filename=stdin_filename,
571 except GitWildMatchPatternError:
576 "No Python files are present to be formatted. Nothing to do 😴",
582 if len(sources) == 1:
586 write_back=write_back,
591 from black.concurrency import reformat_many
596 write_back=write_back,
602 if verbose or not quiet:
603 if code is None and (verbose or report.change_count or report.failure_count):
605 out(error_msg if report.return_code else "All done! ✨ 🍰 ✨")
607 click.echo(str(report), err=True)
608 ctx.exit(report.return_code)
614 src: Tuple[str, ...],
617 include: Pattern[str],
618 exclude: Optional[Pattern[str]],
619 extend_exclude: Optional[Pattern[str]],
620 force_exclude: Optional[Pattern[str]],
622 stdin_filename: Optional[str],
624 """Compute the set of files to be formatted."""
625 sources: Set[Path] = set()
626 root = ctx.obj["root"]
629 if s == "-" and stdin_filename:
630 p = Path(stdin_filename)
636 if is_stdin or p.is_file():
637 normalized_path = normalize_path_maybe_ignore(p, ctx.obj["root"], report)
638 if normalized_path is None:
641 normalized_path = "/" + normalized_path
642 # Hard-exclude any files that matches the `--force-exclude` regex.
644 force_exclude_match = force_exclude.search(normalized_path)
646 force_exclude_match = None
647 if force_exclude_match and force_exclude_match.group(0):
648 report.path_ignored(p, "matches the --force-exclude regular expression")
652 p = Path(f"{STDIN_PLACEHOLDER}{str(p)}")
654 if p.suffix == ".ipynb" and not jupyter_dependencies_are_installed(
655 verbose=verbose, quiet=quiet
662 exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES)
663 gitignore = get_gitignore(root)
664 p_gitignore = get_gitignore(p)
665 # No need to use p's gitignore if it is identical to root's gitignore
666 # (i.e. root and p point to the same directory).
667 if gitignore != p_gitignore:
668 gitignore += p_gitignore
688 err(f"invalid path: {s}")
693 src: Sized, msg: str, quiet: bool, verbose: bool, ctx: click.Context
696 Exit if there is no `src` provided for formatting
699 if verbose or not quiet:
705 content: str, fast: bool, write_back: WriteBack, mode: Mode, report: Report
708 Reformat and print out `content` without spawning child processes.
709 Similar to `reformat_one`, but for string content.
711 `fast`, `write_back`, and `mode` options are passed to
712 :func:`format_file_in_place` or :func:`format_stdin_to_stdout`.
714 path = Path("<string>")
717 if format_stdin_to_stdout(
718 content=content, fast=fast, write_back=write_back, mode=mode
720 changed = Changed.YES
721 report.done(path, changed)
722 except Exception as exc:
724 traceback.print_exc()
725 report.failed(path, str(exc))
728 # diff-shades depends on being to monkeypatch this function to operate. I know it's
729 # not ideal, but this shouldn't cause any issues ... hopefully. ~ichard26
730 @mypyc_attr(patchable=True)
732 src: Path, fast: bool, write_back: WriteBack, mode: Mode, report: "Report"
734 """Reformat a single file under `src` without spawning child processes.
736 `fast`, `write_back`, and `mode` options are passed to
737 :func:`format_file_in_place` or :func:`format_stdin_to_stdout`.
744 elif str(src).startswith(STDIN_PLACEHOLDER):
746 # Use the original name again in case we want to print something
748 src = Path(str(src)[len(STDIN_PLACEHOLDER) :])
753 if src.suffix == ".pyi":
754 mode = replace(mode, is_pyi=True)
755 elif src.suffix == ".ipynb":
756 mode = replace(mode, is_ipynb=True)
757 if format_stdin_to_stdout(fast=fast, write_back=write_back, mode=mode):
758 changed = Changed.YES
761 if write_back not in (WriteBack.DIFF, WriteBack.COLOR_DIFF):
762 cache = read_cache(mode)
763 res_src = src.resolve()
764 res_src_s = str(res_src)
765 if res_src_s in cache and cache[res_src_s] == get_cache_info(res_src):
766 changed = Changed.CACHED
767 if changed is not Changed.CACHED and format_file_in_place(
768 src, fast=fast, write_back=write_back, mode=mode
770 changed = Changed.YES
771 if (write_back is WriteBack.YES and changed is not Changed.CACHED) or (
772 write_back is WriteBack.CHECK and changed is Changed.NO
774 write_cache(cache, [src], mode)
775 report.done(src, changed)
776 except Exception as exc:
778 traceback.print_exc()
779 report.failed(src, str(exc))
782 def format_file_in_place(
786 write_back: WriteBack = WriteBack.NO,
787 lock: Any = None, # multiprocessing.Manager().Lock() is some crazy proxy
789 """Format file under `src` path. Return True if changed.
791 If `write_back` is DIFF, write a diff to stdout. If it is YES, write reformatted
793 `mode` and `fast` options are passed to :func:`format_file_contents`.
795 if src.suffix == ".pyi":
796 mode = replace(mode, is_pyi=True)
797 elif src.suffix == ".ipynb":
798 mode = replace(mode, is_ipynb=True)
800 then = datetime.utcfromtimestamp(src.stat().st_mtime)
802 with open(src, "rb") as buf:
803 if mode.skip_source_first_line:
804 header = buf.readline()
805 src_contents, encoding, newline = decode_bytes(buf.read())
807 dst_contents = format_file_contents(src_contents, fast=fast, mode=mode)
808 except NothingChanged:
810 except JSONDecodeError:
812 f"File '{src}' cannot be parsed as valid Jupyter notebook."
814 src_contents = header.decode(encoding) + src_contents
815 dst_contents = header.decode(encoding) + dst_contents
817 if write_back == WriteBack.YES:
818 with open(src, "w", encoding=encoding, newline=newline) as f:
819 f.write(dst_contents)
820 elif write_back in (WriteBack.DIFF, WriteBack.COLOR_DIFF):
821 now = datetime.utcnow()
822 src_name = f"{src}\t{then} +0000"
823 dst_name = f"{src}\t{now} +0000"
825 diff_contents = ipynb_diff(src_contents, dst_contents, src_name, dst_name)
827 diff_contents = diff(src_contents, dst_contents, src_name, dst_name)
829 if write_back == WriteBack.COLOR_DIFF:
830 diff_contents = color_diff(diff_contents)
832 with lock or nullcontext():
833 f = io.TextIOWrapper(
839 f = wrap_stream_for_windows(f)
840 f.write(diff_contents)
846 def format_stdin_to_stdout(
849 content: Optional[str] = None,
850 write_back: WriteBack = WriteBack.NO,
853 """Format file on stdin. Return True if changed.
855 If content is None, it's read from sys.stdin.
857 If `write_back` is YES, write reformatted code back to stdout. If it is DIFF,
858 write a diff to stdout. The `mode` argument is passed to
859 :func:`format_file_contents`.
861 then = datetime.utcnow()
864 src, encoding, newline = decode_bytes(sys.stdin.buffer.read())
866 src, encoding, newline = content, "utf-8", ""
870 dst = format_file_contents(src, fast=fast, mode=mode)
873 except NothingChanged:
877 f = io.TextIOWrapper(
878 sys.stdout.buffer, encoding=encoding, newline=newline, write_through=True
880 if write_back == WriteBack.YES:
881 # Make sure there's a newline after the content
882 if dst and dst[-1] != "\n":
885 elif write_back in (WriteBack.DIFF, WriteBack.COLOR_DIFF):
886 now = datetime.utcnow()
887 src_name = f"STDIN\t{then} +0000"
888 dst_name = f"STDOUT\t{now} +0000"
889 d = diff(src, dst, src_name, dst_name)
890 if write_back == WriteBack.COLOR_DIFF:
892 f = wrap_stream_for_windows(f)
897 def check_stability_and_equivalence(
898 src_contents: str, dst_contents: str, *, mode: Mode
900 """Perform stability and equivalence checks.
902 Raise AssertionError if source and destination contents are not
903 equivalent, or if a second pass of the formatter would format the
906 assert_equivalent(src_contents, dst_contents)
907 assert_stable(src_contents, dst_contents, mode=mode)
910 def format_file_contents(src_contents: str, *, fast: bool, mode: Mode) -> FileContent:
911 """Reformat contents of a file and return new contents.
913 If `fast` is False, additionally confirm that the reformatted code is
914 valid by calling :func:`assert_equivalent` and :func:`assert_stable` on it.
915 `mode` is passed to :func:`format_str`.
917 if not src_contents.strip():
921 dst_contents = format_ipynb_string(src_contents, fast=fast, mode=mode)
923 dst_contents = format_str(src_contents, mode=mode)
924 if src_contents == dst_contents:
927 if not fast and not mode.is_ipynb:
928 # Jupyter notebooks will already have been checked above.
929 check_stability_and_equivalence(src_contents, dst_contents, mode=mode)
933 def validate_cell(src: str, mode: Mode) -> None:
934 """Check that cell does not already contain TransformerManager transformations,
935 or non-Python cell magics, which might cause tokenizer_rt to break because of
938 If a cell contains ``!ls``, then it'll be transformed to
939 ``get_ipython().system('ls')``. However, if the cell originally contained
940 ``get_ipython().system('ls')``, then it would get transformed in the same way:
942 >>> TransformerManager().transform_cell("get_ipython().system('ls')")
943 "get_ipython().system('ls')\n"
944 >>> TransformerManager().transform_cell("!ls")
945 "get_ipython().system('ls')\n"
947 Due to the impossibility of safely roundtripping in such situations, cells
948 containing transformed magics will be ignored.
950 if any(transformed_magic in src for transformed_magic in TRANSFORMED_MAGICS):
954 and src.split()[0][2:] not in PYTHON_CELL_MAGICS | mode.python_cell_magics
959 def format_cell(src: str, *, fast: bool, mode: Mode) -> str:
960 """Format code in given cell of Jupyter notebook.
964 - if cell has trailing semicolon, remove it;
965 - if cell has IPython magics, mask them;
967 - reinstate IPython magics;
968 - reinstate trailing semicolon (if originally present);
969 - strip trailing newlines.
971 Cells with syntax errors will not be processed, as they
972 could potentially be automagics or multi-line magics, which
973 are currently not supported.
975 validate_cell(src, mode)
976 src_without_trailing_semicolon, has_trailing_semicolon = remove_trailing_semicolon(
980 masked_src, replacements = mask_cell(src_without_trailing_semicolon)
982 raise NothingChanged from None
983 masked_dst = format_str(masked_src, mode=mode)
985 check_stability_and_equivalence(masked_src, masked_dst, mode=mode)
986 dst_without_trailing_semicolon = unmask_cell(masked_dst, replacements)
987 dst = put_trailing_semicolon_back(
988 dst_without_trailing_semicolon, has_trailing_semicolon
990 dst = dst.rstrip("\n")
992 raise NothingChanged from None
996 def validate_metadata(nb: MutableMapping[str, Any]) -> None:
997 """If notebook is marked as non-Python, don't format it.
999 All notebook metadata fields are optional, see
1000 https://nbformat.readthedocs.io/en/latest/format_description.html. So
1001 if a notebook has empty metadata, we will try to parse it anyway.
1003 language = nb.get("metadata", {}).get("language_info", {}).get("name", None)
1004 if language is not None and language != "python":
1005 raise NothingChanged from None
1008 def format_ipynb_string(src_contents: str, *, fast: bool, mode: Mode) -> FileContent:
1009 """Format Jupyter notebook.
1011 Operate cell-by-cell, only on code cells, only for Python notebooks.
1012 If the ``.ipynb`` originally had a trailing newline, it'll be preserved.
1014 trailing_newline = src_contents[-1] == "\n"
1016 nb = json.loads(src_contents)
1017 validate_metadata(nb)
1018 for cell in nb["cells"]:
1019 if cell.get("cell_type", None) == "code":
1021 src = "".join(cell["source"])
1022 dst = format_cell(src, fast=fast, mode=mode)
1023 except NothingChanged:
1026 cell["source"] = dst.splitlines(keepends=True)
1029 dst_contents = json.dumps(nb, indent=1, ensure_ascii=False)
1030 if trailing_newline:
1031 dst_contents = dst_contents + "\n"
1034 raise NothingChanged
1037 def format_str(src_contents: str, *, mode: Mode) -> str:
1038 """Reformat a string and return new contents.
1040 `mode` determines formatting options, such as how many characters per line are
1044 >>> print(black.format_str("def f(arg:str='')->None:...", mode=black.Mode()))
1045 def f(arg: str = "") -> None:
1048 A more complex example:
1051 ... black.format_str(
1052 ... "def f(arg:str='')->None: hey",
1053 ... mode=black.Mode(
1054 ... target_versions={black.TargetVersion.PY36},
1056 ... string_normalization=False,
1067 dst_contents = _format_str_once(src_contents, mode=mode)
1068 # Forced second pass to work around optional trailing commas (becoming
1069 # forced trailing commas on pass 2) interacting differently with optional
1070 # parentheses. Admittedly ugly.
1071 if src_contents != dst_contents:
1072 return _format_str_once(dst_contents, mode=mode)
1076 def _format_str_once(src_contents: str, *, mode: Mode) -> str:
1077 src_node = lib2to3_parse(src_contents.lstrip(), mode.target_versions)
1079 if mode.target_versions:
1080 versions = mode.target_versions
1082 future_imports = get_future_imports(src_node)
1083 versions = detect_target_versions(src_node, future_imports=future_imports)
1085 normalize_fmt_off(src_node, preview=mode.preview)
1086 lines = LineGenerator(mode=mode)
1087 elt = EmptyLineTracker(is_pyi=mode.is_pyi)
1088 empty_line = Line(mode=mode)
1090 split_line_features = {
1092 for feature in {Feature.TRAILING_COMMA_IN_CALL, Feature.TRAILING_COMMA_IN_DEF}
1093 if supports_feature(versions, feature)
1095 for current_line in lines.visit(src_node):
1096 dst_contents.append(str(empty_line) * after)
1097 before, after = elt.maybe_empty_lines(current_line)
1098 dst_contents.append(str(empty_line) * before)
1099 for line in transform_line(
1100 current_line, mode=mode, features=split_line_features
1102 dst_contents.append(str(line))
1103 return "".join(dst_contents)
1106 def decode_bytes(src: bytes) -> Tuple[FileContent, Encoding, NewLine]:
1107 """Return a tuple of (decoded_contents, encoding, newline).
1109 `newline` is either CRLF or LF but `decoded_contents` is decoded with
1110 universal newlines (i.e. only contains LF).
1112 srcbuf = io.BytesIO(src)
1113 encoding, lines = tokenize.detect_encoding(srcbuf.readline)
1115 return "", encoding, "\n"
1117 newline = "\r\n" if b"\r\n" == lines[0][-2:] else "\n"
1119 with io.TextIOWrapper(srcbuf, encoding) as tiow:
1120 return tiow.read(), encoding, newline
1123 def get_features_used( # noqa: C901
1124 node: Node, *, future_imports: Optional[Set[str]] = None
1126 """Return a set of (relatively) new Python features used in this file.
1128 Currently looking for:
1130 - self-documenting expressions in f-strings (f"{x=}");
1131 - underscores in numeric literals;
1132 - trailing commas after * or ** in function signatures and calls;
1133 - positional only arguments in function signatures and lambdas;
1134 - assignment expression;
1135 - relaxed decorator syntax;
1136 - usage of __future__ flags (annotations);
1137 - print / exec statements;
1139 features: Set[Feature] = set()
1142 FUTURE_FLAG_TO_FEATURE[future_import]
1143 for future_import in future_imports
1144 if future_import in FUTURE_FLAG_TO_FEATURE
1147 for n in node.pre_order():
1148 if is_string_token(n):
1149 value_head = n.value[:2]
1150 if value_head in {'f"', 'F"', "f'", "F'", "rf", "fr", "RF", "FR"}:
1151 features.add(Feature.F_STRINGS)
1152 if Feature.DEBUG_F_STRINGS not in features:
1153 for span_beg, span_end in iter_fexpr_spans(n.value):
1154 if n.value[span_beg : span_end - 1].rstrip().endswith("="):
1155 features.add(Feature.DEBUG_F_STRINGS)
1158 elif is_number_token(n):
1160 features.add(Feature.NUMERIC_UNDERSCORES)
1162 elif n.type == token.SLASH:
1163 if n.parent and n.parent.type in {
1168 features.add(Feature.POS_ONLY_ARGUMENTS)
1170 elif n.type == token.COLONEQUAL:
1171 features.add(Feature.ASSIGNMENT_EXPRESSIONS)
1173 elif n.type == syms.decorator:
1174 if len(n.children) > 1 and not is_simple_decorator_expression(
1177 features.add(Feature.RELAXED_DECORATORS)
1180 n.type in {syms.typedargslist, syms.arglist}
1182 and n.children[-1].type == token.COMMA
1184 if n.type == syms.typedargslist:
1185 feature = Feature.TRAILING_COMMA_IN_DEF
1187 feature = Feature.TRAILING_COMMA_IN_CALL
1189 for ch in n.children:
1190 if ch.type in STARS:
1191 features.add(feature)
1193 if ch.type == syms.argument:
1194 for argch in ch.children:
1195 if argch.type in STARS:
1196 features.add(feature)
1199 n.type in {syms.return_stmt, syms.yield_expr}
1200 and len(n.children) >= 2
1201 and n.children[1].type == syms.testlist_star_expr
1202 and any(child.type == syms.star_expr for child in n.children[1].children)
1204 features.add(Feature.UNPACKING_ON_FLOW)
1207 n.type == syms.annassign
1208 and len(n.children) >= 4
1209 and n.children[3].type == syms.testlist_star_expr
1211 features.add(Feature.ANN_ASSIGN_EXTENDED_RHS)
1214 n.type == syms.except_clause
1215 and len(n.children) >= 2
1216 and n.children[1].type == token.STAR
1218 features.add(Feature.EXCEPT_STAR)
1220 elif n.type in {syms.subscriptlist, syms.trailer} and any(
1221 child.type == syms.star_expr for child in n.children
1223 features.add(Feature.VARIADIC_GENERICS)
1226 n.type == syms.tname_star
1227 and len(n.children) == 3
1228 and n.children[2].type == syms.star_expr
1230 features.add(Feature.VARIADIC_GENERICS)
1235 def detect_target_versions(
1236 node: Node, *, future_imports: Optional[Set[str]] = None
1237 ) -> Set[TargetVersion]:
1238 """Detect the version to target based on the nodes used."""
1239 features = get_features_used(node, future_imports=future_imports)
1241 version for version in TargetVersion if features <= VERSION_TO_FEATURES[version]
1245 def get_future_imports(node: Node) -> Set[str]:
1246 """Return a set of __future__ imports in the file."""
1247 imports: Set[str] = set()
1249 def get_imports_from_children(children: List[LN]) -> Generator[str, None, None]:
1250 for child in children:
1251 if isinstance(child, Leaf):
1252 if child.type == token.NAME:
1255 elif child.type == syms.import_as_name:
1256 orig_name = child.children[0]
1257 assert isinstance(orig_name, Leaf), "Invalid syntax parsing imports"
1258 assert orig_name.type == token.NAME, "Invalid syntax parsing imports"
1259 yield orig_name.value
1261 elif child.type == syms.import_as_names:
1262 yield from get_imports_from_children(child.children)
1265 raise AssertionError("Invalid syntax parsing imports")
1267 for child in node.children:
1268 if child.type != syms.simple_stmt:
1271 first_child = child.children[0]
1272 if isinstance(first_child, Leaf):
1273 # Continue looking if we see a docstring; otherwise stop.
1275 len(child.children) == 2
1276 and first_child.type == token.STRING
1277 and child.children[1].type == token.NEWLINE
1283 elif first_child.type == syms.import_from:
1284 module_name = first_child.children[1]
1285 if not isinstance(module_name, Leaf) or module_name.value != "__future__":
1288 imports |= set(get_imports_from_children(first_child.children[3:]))
1295 def assert_equivalent(src: str, dst: str) -> None:
1296 """Raise AssertionError if `src` and `dst` aren't equivalent."""
1298 src_ast = parse_ast(src)
1299 except Exception as exc:
1300 raise AssertionError(
1301 "cannot use --safe with this file; failed to parse source file AST: "
1303 "This could be caused by running Black with an older Python version "
1304 "that does not support new syntax used in your source file."
1308 dst_ast = parse_ast(dst)
1309 except Exception as exc:
1310 log = dump_to_file("".join(traceback.format_tb(exc.__traceback__)), dst)
1311 raise AssertionError(
1312 f"INTERNAL ERROR: Black produced invalid code: {exc}. "
1313 "Please report a bug on https://github.com/psf/black/issues. "
1314 f"This invalid output might be helpful: {log}"
1317 src_ast_str = "\n".join(stringify_ast(src_ast))
1318 dst_ast_str = "\n".join(stringify_ast(dst_ast))
1319 if src_ast_str != dst_ast_str:
1320 log = dump_to_file(diff(src_ast_str, dst_ast_str, "src", "dst"))
1321 raise AssertionError(
1322 "INTERNAL ERROR: Black produced code that is not equivalent to the"
1323 " source. Please report a bug on "
1324 f"https://github.com/psf/black/issues. This diff might be helpful: {log}"
1328 def assert_stable(src: str, dst: str, mode: Mode) -> None:
1329 """Raise AssertionError if `dst` reformats differently the second time."""
1330 # We shouldn't call format_str() here, because that formats the string
1331 # twice and may hide a bug where we bounce back and forth between two
1333 newdst = _format_str_once(dst, mode=mode)
1337 diff(src, dst, "source", "first pass"),
1338 diff(dst, newdst, "first pass", "second pass"),
1340 raise AssertionError(
1341 "INTERNAL ERROR: Black produced different code on the second pass of the"
1342 " formatter. Please report a bug on https://github.com/psf/black/issues."
1343 f" This diff might be helpful: {log}"
1348 def nullcontext() -> Iterator[None]:
1349 """Return an empty context manager.
1351 To be used like `nullcontext` in Python 3.7.
1356 def patch_click() -> None:
1357 """Make Click not crash on Python 3.6 with LANG=C.
1359 On certain misconfigured environments, Python 3 selects the ASCII encoding as the
1360 default which restricts paths that it can access during the lifetime of the
1361 application. Click refuses to work in this scenario by raising a RuntimeError.
1363 In case of Black the likelihood that non-ASCII characters are going to be used in
1364 file paths is minimal since it's Python source code. Moreover, this crash was
1365 spurious on Python 3.7 thanks to PEP 538 and PEP 540.
1367 modules: List[Any] = []
1369 from click import core
1373 modules.append(core)
1375 # Removed in Click 8.1.0 and newer; we keep this around for users who have
1376 # older versions installed.
1377 from click import _unicodefun # type: ignore
1381 modules.append(_unicodefun)
1383 for module in modules:
1384 if hasattr(module, "_verify_python3_env"):
1385 module._verify_python3_env = lambda: None
1386 if hasattr(module, "_verify_python_env"):
1387 module._verify_python_env = lambda: None
1390 def patched_main() -> None:
1391 # PyInstaller patches multiprocessing to need freeze_support() even in non-Windows
1392 # environments so just assume we always need to call it if frozen.
1393 if getattr(sys, "frozen", False):
1394 from multiprocessing import freeze_support
1402 if __name__ == "__main__":