Skip to content

Commit

Permalink
Reduce number of patches
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 685718888
  • Loading branch information
AleksMat authored and copybara-github committed Oct 15, 2024
1 parent 83f0ba5 commit 5496c5b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 352 deletions.
294 changes: 11 additions & 283 deletions patches/pyink.patch
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,7 @@
# This file is provided so it's easier to see the actual differences between Black and Pyink.
--- a/__init__.py
+++ b/__init__.py
@@ -1,16 +1,16 @@
+from contextlib import contextmanager
+from dataclasses import replace
+from datetime import datetime, timezone
+from enum import Enum
import io
import json
+from json.decoder import JSONDecodeError
+from pathlib import Path
import platform
import re
import sys
import tokenize
import traceback
-from contextlib import contextmanager
-from dataclasses import replace
-from datetime import datetime, timezone
-from enum import Enum
-from json.decoder import JSONDecodeError
-from pathlib import Path
from typing import (
Any,
Collection,
@@ -24,12 +24,13 @@ from typing import (
@@ -24,6 +24,8 @@ from typing import (
Union,
)

Expand All @@ -34,18 +11,9 @@
import click
from click.core import ParameterSource
from mypy_extensions import mypyc_attr
from pathspec import PathSpec
from pathspec.patterns.gitwildmatch import GitWildMatchPatternError
-
from pyink._pyink_version import version as __version__
from pyink.cache import Cache
from pyink.comments import normalize_fmt_off
@@ -62,9 +63,15 @@ from pyink.handle_ipynb_magics import (
)
from pyink.linegen import LN, LineGenerator, transform_line
@@ -64,7 +66,13 @@ from pyink.linegen import LN, LineGenera
from pyink.lines import EmptyLineTracker, LinesBlock
-from pyink.mode import FUTURE_FLAG_TO_FEATURE, VERSION_TO_FEATURES, Feature
+from pyink.mode import FUTURE_FLAG_TO_FEATURE, Feature, VERSION_TO_FEATURES
from pyink.mode import FUTURE_FLAG_TO_FEATURE, VERSION_TO_FEATURES, Feature
from pyink.mode import Mode as Mode # re-exported
-from pyink.mode import Preview, TargetVersion, supports_feature
+from pyink.mode import (
Expand All @@ -58,7 +26,7 @@
from pyink.nodes import STARS, is_number_token, is_simple_decorator_expression, syms
from pyink.output import color_diff, diff, dump_to_file, err, ipynb_diff, out
from pyink.parsing import ( # noqa F401
@@ -80,9 +87,8 @@ from pyink.ranges import (
@@ -80,9 +88,8 @@ from pyink.ranges import (
parse_line_ranges,
sanitized_lines,
)
Expand All @@ -69,64 +37,7 @@

COMPILED = Path(__file__).suffix in (".pyd", ".so")

@@ -260,25 +266,26 @@ def validate_regex(
multiple=True,
help=(
"Python versions that should be supported by Black's output. You should"
- " include all versions that your code supports. By default, Black will infer"
- " target versions from the project metadata in pyproject.toml. If this does"
- " not yield conclusive results, Black will use per-file auto-detection."
+ " include all versions that your code supports. By default, Black will"
+ " infer target versions from the project metadata in pyproject.toml. If"
+ " this does not yield conclusive results, Black will use per-file"
+ " auto-detection."
),
)
@click.option(
"--pyi",
is_flag=True,
help=(
- "Format all input files like typing stubs regardless of file extension. This"
- " is useful when piping source on standard input."
+ "Format all input files like typing stubs regardless of file extension."
+ " This is useful when piping source on standard input."
),
)
@click.option(
"--ipynb",
is_flag=True,
help=(
- "Format all input files like Jupyter Notebooks regardless of file extension."
- " This is useful when piping source on standard input."
+ "Format all input files like Jupyter Notebooks regardless of file"
+ " extension. This is useful when piping source on standard input."
),
)
@click.option(
@@ -313,17 +320,17 @@ def validate_regex(
"--preview",
is_flag=True,
help=(
- "Enable potentially disruptive style changes that may be added to Black's main"
- " functionality in the next major release."
+ "Enable potentially disruptive style changes that may be added to"
+ " Black's main functionality in the next major release."
),
)
@click.option(
"--unstable",
is_flag=True,
help=(
- "Enable potentially disruptive style changes that have known bugs or are not"
- " currently expected to make it into the stable style Black's next major"
- " release. Implies --preview."
+ "Enable potentially disruptive style changes that have known bugs or"
+ " are not currently expected to make it into the stable style Black's"
+ " next major release. Implies --preview."
),
)
@click.option(
@@ -338,20 +345,68 @@ def validate_regex(
@@ -338,6 +345,53 @@ def validate_regex(
),
)
@click.option(
Expand Down Expand Up @@ -180,190 +91,7 @@
"--check",
is_flag=True,
help=(
- "Don't write the files back, just return the status. Return code 0 means"
- " nothing would change. Return code 1 means some files would be reformatted."
- " Return code 123 means there was an internal error."
+ "Don't write the files back, just return the status. Return code 0"
+ " means nothing would change. Return code 1 means some files would be"
+ " reformatted. Return code 123 means there was an internal error."
),
)
@click.option(
"--diff",
is_flag=True,
help=(
- "Don't write the files back, just output a diff to indicate what changes"
- " Black would've made. They are printed to stdout so capturing them is simple."
+ "Don't write the files back, just output a diff to indicate what"
+ " changes Black would've made. They are printed to stdout so capturing"
+ " them is simple."
),
)
@click.option(
@@ -364,11 +419,11 @@ def validate_regex(
multiple=True,
metavar="START-END",
help=(
- "When specified, Black will try its best to only format these lines. This"
- " option can be specified multiple times, and a union of the lines will be"
- " formatted. Each range must be specified as two integers connected by a `-`:"
- " `<START>-<END>`. The `<START>` and `<END>` integer indices are 1-based and"
- " inclusive on both ends."
+ "When specified, Black will try its best to only format these lines."
+ " This option can be specified multiple times, and a union of the lines"
+ " will be formatted. Each range must be specified as two integers"
+ " connected by a `-`: `<START>-<END>`. The `<START>` and `<END>`"
+ " integer indices are 1-based and inclusive on both ends."
),
default=(),
)
@@ -376,9 +431,9 @@ def validate_regex(
"--fast/--safe",
is_flag=True,
help=(
- "By default, Black performs an AST safety check after formatting your code."
- " The --fast flag turns off this check and the --safe flag explicitly enables"
- " it. [default: --safe]"
+ "By default, Black performs an AST safety check after formatting your"
+ " code. The --fast flag turns off this check and the --safe flag"
+ " explicitly enables it. [default: --safe]"
),
)
@click.option(
@@ -388,8 +443,8 @@ def validate_regex(
"Require a specific version of Black to be running. This is useful for"
" ensuring that all contributors to your project are using the same"
" version, because different versions of Black may format code a little"
- " differently. This option can be set in a configuration file for consistent"
- " results across environments."
+ " differently. This option can be set in a configuration file for"
+ " consistent results across environments."
),
)
@click.option(
@@ -397,11 +452,12 @@ def validate_regex(
type=str,
callback=validate_regex,
help=(
- "A regular expression that matches files and directories that should be"
- " excluded on recursive searches. An empty value means no paths are excluded."
- " Use forward slashes for directories on all platforms (Windows, too)."
- " By default, Black also ignores all paths listed in .gitignore. Changing this"
- f" value will override all default exclusions. [default: {DEFAULT_EXCLUDES}]"
+ "A regular expression that matches files and directories that should"
+ " be excluded on recursive searches. An empty value means no paths are"
+ " excluded. Use forward slashes for directories on all platforms"
+ " (Windows, too). By default, Black also ignores all paths listed in"
+ " .gitignore. Changing this value will override all default"
+ f" exclusions. [default: {DEFAULT_EXCLUDES}]"
),
show_default=False,
)
@@ -410,8 +466,8 @@ def validate_regex(
type=str,
callback=validate_regex,
help=(
- "Like --exclude, but adds additional files and directories on top of the"
- " default values instead of overriding them."
+ "Like --exclude, but adds additional files and directories on top of"
+ " the default values instead of overriding them."
),
)
@click.option(
@@ -419,10 +475,10 @@ def validate_regex(
type=str,
callback=validate_regex,
help=(
- "Like --exclude, but files and directories matching this regex will be excluded"
- " even when they are passed explicitly as arguments. This is useful when"
- " invoking Black programmatically on changed files, such as in a pre-commit"
- " hook or editor plugin."
+ "Like --exclude, but files and directories matching this regex will be"
+ " excluded even when they are passed explicitly as arguments. This is"
+ " useful when invoking Black programmatically on changed files, such as"
+ " in a pre-commit hook or editor plugin."
),
)
@click.option(
@@ -430,9 +486,9 @@ def validate_regex(
type=str,
is_eager=True,
help=(
- "The name of the file when passing it through stdin. Useful to make sure Black"
- " will respect the --force-exclude option on some editors that rely on using"
- " stdin."
+ "The name of the file when passing it through stdin. Useful to make"
+ " sure Black will respect the --force-exclude option on some editors"
+ " that rely on using stdin."
),
)
@click.option(
@@ -442,10 +498,10 @@ def validate_regex(
callback=validate_regex,
help=(
"A regular expression that matches files and directories that should be"
- " included on recursive searches. An empty value means all files are included"
- " regardless of the name. Use forward slashes for directories on all platforms"
- " (Windows, too). Overrides all exclusions, including from .gitignore and"
- " command line options."
+ " included on recursive searches. An empty value means all files are"
+ " included regardless of the name. Use forward slashes for directories"
+ " on all platforms (Windows, too). Overrides all exclusions, including"
+ " from .gitignore and command line options."
),
show_default=True,
)
@@ -455,10 +511,10 @@ def validate_regex(
type=click.IntRange(min=1),
default=None,
help=(
- "When Black formats multiple files, it may use a process pool to speed up"
- " formatting. This option controls the number of parallel workers. This can"
- " also be specified via the PYINK_NUM_WORKERS environment variable. Defaults"
- " to the number of CPUs in the system."
+ "When Black formats multiple files, it may use a process pool to speed"
+ " up formatting. This option controls the number of parallel workers."
+ " This can also be specified via the PYINK_NUM_WORKERS environment"
+ " variable. Defaults to the number of CPUs in the system."
),
)
@click.option(
@@ -466,8 +522,8 @@ def validate_regex(
"--quiet",
is_flag=True,
help=(
- "Stop emitting all non-critical output. Error messages will still be emitted"
- " (which can silenced by 2>/dev/null)."
+ "Stop emitting all non-critical output. Error messages will still be"
+ " emitted (which can silenced by 2>/dev/null)."
),
)
@click.option(
@@ -483,15 +539,20 @@ def validate_regex(
@click.version_option(
version=__version__,
message=(
- f"%(prog)s, %(version)s (compiled: {'yes' if COMPILED else 'no'})\n"
- f"Python ({platform.python_implementation()}) {platform.python_version()}"
+ "%(prog)s, %(version)s (compiled:"
+ f" {'yes' if COMPILED else 'no'})\nPython"
+ f" ({platform.python_implementation()}) {platform.python_version()}"
),
)
@click.argument(
"src",
nargs=-1,
type=click.Path(
- exists=True, file_okay=True, dir_okay=True, readable=True, allow_dash=True
+ exists=True,
+ file_okay=True,
+ dir_okay=True,
+ readable=True,
+ allow_dash=True,
),
is_eager=True,
metavar="SRC ...",
@@ -530,6 +591,11 @@ def main( # noqa: C901
@@ -530,6 +584,11 @@ def main( # noqa: C901
preview: bool,
unstable: bool,
enable_unstable_feature: list[Preview],
Expand All @@ -375,7 +103,7 @@
quiet: bool,
verbose: bool,
required_version: Optional[str],
@@ -636,7 +702,15 @@ def main( # noqa: C901
@@ -636,7 +695,15 @@ def main( # noqa: C901
preview=preview,
unstable=unstable,
python_cell_magics=set(python_cell_magics),
Expand All @@ -392,7 +120,7 @@
)

lines: list[tuple[int, int]] = []
@@ -1132,6 +1206,17 @@ def validate_metadata(nb: MutableMapping
@@ -1132,6 +1199,17 @@ def validate_metadata(nb: MutableMapping
if language is not None and language != "python":
raise NothingChanged from None

Expand All @@ -410,15 +138,15 @@

def format_ipynb_string(src_contents: str, *, fast: bool, mode: Mode) -> FileContent:
"""Format Jupyter notebook.
@@ -1143,7 +1228,6 @@ def format_ipynb_string(src_contents: st
@@ -1143,7 +1221,6 @@ def format_ipynb_string(src_contents: st
raise NothingChanged

trailing_newline = src_contents[-1] == "\n"
- modified = False
nb = json.loads(src_contents)
validate_metadata(nb)
for cell in nb["cells"]:
@@ -1155,14 +1239,15 @@ def format_ipynb_string(src_contents: st
@@ -1155,14 +1232,15 @@ def format_ipynb_string(src_contents: st
pass
else:
cell["source"] = dst.splitlines(keepends=True)
Expand All @@ -442,7 +170,7 @@


def format_str(
@@ -1223,6 +1308,8 @@ def _format_str_once(
@@ -1223,6 +1301,8 @@ def _format_str_once(
future_imports = get_future_imports(src_node)
versions = detect_target_versions(src_node, future_imports=future_imports)

Expand Down
Loading

0 comments on commit 5496c5b

Please sign in to comment.