Loading README.md +46 −26 Changes for README.md: 46 added lines, 26 removed lines. Original line number Diff line number Diff line Loading @@ -71,46 +71,60 @@ black {source_file_or_directory} black [OPTIONS] [SRC]... Options: -l, --line-length INTEGER Where to wrap around. [default: 88] --py36 Allow using Python 3.6-only syntax on all input files. This will put trailing commas in function signatures and calls also after *args and **kwargs. [default: per-file auto-detection] -l, --line-length INTEGER How many characters per line to allow. [default: 88] -t, --target-version [pypy35|cpy27|cpy33|cpy34|cpy35|cpy36|cpy37|cpy38] Python versions that should be supported by Black's output. [default: per-file auto- detection] --py36 Allow using Python 3.6-only syntax on all input files. This will put trailing commas in function signatures and calls also after *args and **kwargs. [default: per-file auto-detection] --pyi Format all input files like typing stubs regardless of file extension (useful when piping source on standard input). regardless of file extension (useful when piping source on standard input). -S, --skip-string-normalization Don't normalize string quotes or prefixes. -N, --skip-numeric-underscore-normalization Don't normalize underscores in numeric literals. Don't normalize underscores in numeric literals. --check 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. --diff Don't write the files back, just output a diff for each file on stdout. --fast / --safe If --fast given, skip temporary sanity checks. [default: --safe] change. Return code 1 means some files would be reformatted. Return code 123 means there was an internal error. --diff Don't write the files back, just output a diff for each file on stdout. --fast / --safe If --fast given, skip temporary sanity checks. [default: --safe] --include TEXT A regular expression that matches files and directories that should be included on recursive searches. On Windows, use forward slashes for directories. [default: \.pyi?$] recursive searches. An empty value means all files are included regardless of the name. Use forward slashes for directories on all platforms (Windows, too). Exclusions are calculated first, inclusions later. [default: \.pyi?$] --exclude TEXT A regular expression that matches files and directories that should be excluded on recursive searches. On Windows, use forward slashes for directories. [default: build/|buck-out/|dist/|_build/|\.eggs/|\.git/| \.hg/|\.mypy_cache/|\.nox/|\.tox/|\.venv/] -q, --quiet Don't emit non-error messages to stderr. Errors are still emitted, silence those with recursive searches. An empty value means no paths are excluded. Use forward slashes for directories on all platforms (Windows, too). Exclusions are calculated first, inclusions later. [default: /(\.eggs|\.git|\.hg|\.mypy _cache|\.nox|\.tox|\.venv|_build|buck- out|build|dist)/] -q, --quiet Don't emit non-error messages to stderr. Errors are still emitted, silence those with 2>/dev/null. -v, --verbose Also emit messages to stderr about files that were not changed or were ignored due to --exclude=. --version Show the version and exit. --config PATH Read configuration from PATH. --help Show this message and exit. -h, --help Show this message and exit. ``` *Black* is a well-behaved Unix-style command-line tool: Loading Loading @@ -815,8 +829,9 @@ The headers controlling how code is formatted are: passed the `--fast` command line flag. - `X-Python-Variant`: if set to `pyi`, `blackd` will act as *Black* does when passed the `--pyi` command line flag. Otherwise, its value must correspond to a Python version. If this value represents at least Python 3.6, `blackd` will act as *Black* does when passed the `--py36` command line flag. a Python version or a set of comma-separated Python versions, optionally prefixed with `cpy` or `pypy`. For example, to request code that is compatible with PyPy 3.5 and CPython 3.5, set the header to `pypy3.5,cpy3.5`. If any of these headers are set to invalid values, `blackd` returns a `HTTP 400` error response, mentioning the name of the problematic header in the message body. Loading Loading @@ -935,6 +950,11 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md). ## Change Log ### 18.11b0 * new option `--target-version` to control which Python versions *Black*-formatted code should target ### 18.9b0 * numeric literals are now formatted by *Black* (#452, #461, #464, #469): Loading black.py +229 −132 File changed.Preview size limit exceeded, changes collapsed. Show changes blackd.py +60 −24 Changes for blackd.py: 60 added lines, 24 removed lines. Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ from functools import partial import logging from multiprocessing import freeze_support from typing import Set, Tuple from aiohttp import web import aiohttp_cors Loading @@ -29,6 +30,10 @@ ] class InvalidVariantHeader(Exception): pass @click.command(context_settings={"help_option_names": ["-h", "--help"]}) @click.option( "--bind-host", type=str, help="Address to bind the server to.", default="localhost" Loading Loading @@ -73,22 +78,20 @@ async def handle(request: web.Request, executor: Executor) -> web.Response: ) except ValueError: return web.Response(status=400, text="Invalid line length header value") py36 = False pyi = False if PYTHON_VARIANT_HEADER in request.headers: value = request.headers[PYTHON_VARIANT_HEADER] if value == "pyi": pyi = True else: try: major, *rest = value.split(".") if int(major) == 3 and len(rest) > 0: if int(rest[0]) >= 6: py36 = True except ValueError: pyi, versions = parse_python_variant_header(value) except InvalidVariantHeader as e: return web.Response( status=400, text=f"Invalid value for {PYTHON_VARIANT_HEADER}" status=400, text=f"Invalid value for {PYTHON_VARIANT_HEADER}: {e.args[0]}", ) else: pyi = False versions = set() skip_string_normalization = bool( request.headers.get(SKIP_STRING_NORMALIZATION_HEADER, False) ) Loading @@ -98,25 +101,19 @@ async def handle(request: web.Request, executor: Executor) -> web.Response: fast = False if request.headers.get(FAST_OR_SAFE_HEADER, "safe") == "fast": fast = True mode = black.FileMode.from_configuration( py36=py36, pyi=pyi, skip_string_normalization=skip_string_normalization, skip_numeric_underscore_normalization=skip_numeric_underscore_normalization, mode = black.FileMode( target_versions=versions, is_pyi=pyi, line_length=line_length, string_normalization=not skip_string_normalization, numeric_underscore_normalization=not skip_numeric_underscore_normalization, ) req_bytes = await request.content.read() charset = request.charset if request.charset is not None else "utf8" req_str = req_bytes.decode(charset) loop = asyncio.get_event_loop() formatted_str = await loop.run_in_executor( executor, partial( black.format_file_contents, req_str, line_length=line_length, fast=fast, mode=mode, ), executor, partial(black.format_file_contents, req_str, fast=fast, mode=mode) ) return web.Response( content_type=request.content_type, charset=charset, text=formatted_str Loading @@ -130,6 +127,45 @@ async def handle(request: web.Request, executor: Executor) -> web.Response: return web.Response(status=500, text=str(e)) def parse_python_variant_header(value: str) -> Tuple[bool, Set[black.TargetVersion]]: if value == "pyi": return True, set() else: versions = set() for version in value.split(","): tag = "cpy" if version.startswith("cpy"): version = version[len("cpy") :] elif version.startswith("pypy"): tag = "pypy" version = version[len("pypy") :] major_str, *rest = version.split(".") try: major = int(major_str) if major not in (2, 3): raise InvalidVariantHeader("major version must be 2 or 3") if len(rest) > 0: minor = int(rest[0]) if major == 2 and minor != 7: raise InvalidVariantHeader( "minor version must be 7 for Python 2" ) else: # Default to lowest supported minor version. minor = 7 if major == 2 else 3 version_str = f"{tag.upper()}{major}{minor}" # If PyPY is the same as CPython in some version, use # the corresponding CPython version. if tag == "pypy" and not hasattr(black.TargetVersion, version_str): version_str = f"CPY{major}{minor}" if major == 3 and not hasattr(black.TargetVersion, version_str): raise InvalidVariantHeader(f"3.{minor} is not supported") versions.add(black.TargetVersion[version_str]) except (KeyError, ValueError): raise InvalidVariantHeader("expected e.g. '3.7', 'pypy3.5'") return False, versions def patched_main() -> None: freeze_support() black.patch_click() Loading tests/data/fmtonoff.py +2 −2 Changes for tests/data/fmtonoff.py: 2 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -39,7 +39,7 @@ def function_signature_stress_test(number:int,no_annotation=None,text:str='defau return text[number:-1] # fmt: on def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r''): offset = attr.ib(default=attr.Factory( lambda: _r.uniform(10000, 200000))) offset = attr.ib(default=attr.Factory( lambda: _r.uniform(1, 2))) assert task._cancel_stack[:len(old_stack)] == old_stack def spaces_types(a: int = 1, b: tuple = (), c: list = [], d: dict = {}, e: bool = True, f: int = -1, g: int = 1 if False else 2, h: str = "", i: str = r''): ... def spaces2(result= _core.Value(None)): Loading Loading @@ -225,7 +225,7 @@ def function_signature_stress_test(number:int,no_annotation=None,text:str='defau return text[number:-1] # fmt: on def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r""): offset = attr.ib(default=attr.Factory(lambda: _r.uniform(10000, 200_000))) offset = attr.ib(default=attr.Factory(lambda: _r.uniform(1, 2))) assert task._cancel_stack[: len(old_stack)] == old_stack Loading tests/empty.toml 0 → 100644 +1 −0 Changes for tests/empty.toml: 1 added line, 0 removed lines. Original line number Diff line number Diff line # Empty configuration file; used in tests to avoid interference from Black's own config. Loading
README.md +46 −26 Changes for README.md: 46 added lines, 26 removed lines. Original line number Diff line number Diff line Loading @@ -71,46 +71,60 @@ black {source_file_or_directory} black [OPTIONS] [SRC]... Options: -l, --line-length INTEGER Where to wrap around. [default: 88] --py36 Allow using Python 3.6-only syntax on all input files. This will put trailing commas in function signatures and calls also after *args and **kwargs. [default: per-file auto-detection] -l, --line-length INTEGER How many characters per line to allow. [default: 88] -t, --target-version [pypy35|cpy27|cpy33|cpy34|cpy35|cpy36|cpy37|cpy38] Python versions that should be supported by Black's output. [default: per-file auto- detection] --py36 Allow using Python 3.6-only syntax on all input files. This will put trailing commas in function signatures and calls also after *args and **kwargs. [default: per-file auto-detection] --pyi Format all input files like typing stubs regardless of file extension (useful when piping source on standard input). regardless of file extension (useful when piping source on standard input). -S, --skip-string-normalization Don't normalize string quotes or prefixes. -N, --skip-numeric-underscore-normalization Don't normalize underscores in numeric literals. Don't normalize underscores in numeric literals. --check 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. --diff Don't write the files back, just output a diff for each file on stdout. --fast / --safe If --fast given, skip temporary sanity checks. [default: --safe] change. Return code 1 means some files would be reformatted. Return code 123 means there was an internal error. --diff Don't write the files back, just output a diff for each file on stdout. --fast / --safe If --fast given, skip temporary sanity checks. [default: --safe] --include TEXT A regular expression that matches files and directories that should be included on recursive searches. On Windows, use forward slashes for directories. [default: \.pyi?$] recursive searches. An empty value means all files are included regardless of the name. Use forward slashes for directories on all platforms (Windows, too). Exclusions are calculated first, inclusions later. [default: \.pyi?$] --exclude TEXT A regular expression that matches files and directories that should be excluded on recursive searches. On Windows, use forward slashes for directories. [default: build/|buck-out/|dist/|_build/|\.eggs/|\.git/| \.hg/|\.mypy_cache/|\.nox/|\.tox/|\.venv/] -q, --quiet Don't emit non-error messages to stderr. Errors are still emitted, silence those with recursive searches. An empty value means no paths are excluded. Use forward slashes for directories on all platforms (Windows, too). Exclusions are calculated first, inclusions later. [default: /(\.eggs|\.git|\.hg|\.mypy _cache|\.nox|\.tox|\.venv|_build|buck- out|build|dist)/] -q, --quiet Don't emit non-error messages to stderr. Errors are still emitted, silence those with 2>/dev/null. -v, --verbose Also emit messages to stderr about files that were not changed or were ignored due to --exclude=. --version Show the version and exit. --config PATH Read configuration from PATH. --help Show this message and exit. -h, --help Show this message and exit. ``` *Black* is a well-behaved Unix-style command-line tool: Loading Loading @@ -815,8 +829,9 @@ The headers controlling how code is formatted are: passed the `--fast` command line flag. - `X-Python-Variant`: if set to `pyi`, `blackd` will act as *Black* does when passed the `--pyi` command line flag. Otherwise, its value must correspond to a Python version. If this value represents at least Python 3.6, `blackd` will act as *Black* does when passed the `--py36` command line flag. a Python version or a set of comma-separated Python versions, optionally prefixed with `cpy` or `pypy`. For example, to request code that is compatible with PyPy 3.5 and CPython 3.5, set the header to `pypy3.5,cpy3.5`. If any of these headers are set to invalid values, `blackd` returns a `HTTP 400` error response, mentioning the name of the problematic header in the message body. Loading Loading @@ -935,6 +950,11 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md). ## Change Log ### 18.11b0 * new option `--target-version` to control which Python versions *Black*-formatted code should target ### 18.9b0 * numeric literals are now formatted by *Black* (#452, #461, #464, #469): Loading
blackd.py +60 −24 Changes for blackd.py: 60 added lines, 24 removed lines. Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ from functools import partial import logging from multiprocessing import freeze_support from typing import Set, Tuple from aiohttp import web import aiohttp_cors Loading @@ -29,6 +30,10 @@ ] class InvalidVariantHeader(Exception): pass @click.command(context_settings={"help_option_names": ["-h", "--help"]}) @click.option( "--bind-host", type=str, help="Address to bind the server to.", default="localhost" Loading Loading @@ -73,22 +78,20 @@ async def handle(request: web.Request, executor: Executor) -> web.Response: ) except ValueError: return web.Response(status=400, text="Invalid line length header value") py36 = False pyi = False if PYTHON_VARIANT_HEADER in request.headers: value = request.headers[PYTHON_VARIANT_HEADER] if value == "pyi": pyi = True else: try: major, *rest = value.split(".") if int(major) == 3 and len(rest) > 0: if int(rest[0]) >= 6: py36 = True except ValueError: pyi, versions = parse_python_variant_header(value) except InvalidVariantHeader as e: return web.Response( status=400, text=f"Invalid value for {PYTHON_VARIANT_HEADER}" status=400, text=f"Invalid value for {PYTHON_VARIANT_HEADER}: {e.args[0]}", ) else: pyi = False versions = set() skip_string_normalization = bool( request.headers.get(SKIP_STRING_NORMALIZATION_HEADER, False) ) Loading @@ -98,25 +101,19 @@ async def handle(request: web.Request, executor: Executor) -> web.Response: fast = False if request.headers.get(FAST_OR_SAFE_HEADER, "safe") == "fast": fast = True mode = black.FileMode.from_configuration( py36=py36, pyi=pyi, skip_string_normalization=skip_string_normalization, skip_numeric_underscore_normalization=skip_numeric_underscore_normalization, mode = black.FileMode( target_versions=versions, is_pyi=pyi, line_length=line_length, string_normalization=not skip_string_normalization, numeric_underscore_normalization=not skip_numeric_underscore_normalization, ) req_bytes = await request.content.read() charset = request.charset if request.charset is not None else "utf8" req_str = req_bytes.decode(charset) loop = asyncio.get_event_loop() formatted_str = await loop.run_in_executor( executor, partial( black.format_file_contents, req_str, line_length=line_length, fast=fast, mode=mode, ), executor, partial(black.format_file_contents, req_str, fast=fast, mode=mode) ) return web.Response( content_type=request.content_type, charset=charset, text=formatted_str Loading @@ -130,6 +127,45 @@ async def handle(request: web.Request, executor: Executor) -> web.Response: return web.Response(status=500, text=str(e)) def parse_python_variant_header(value: str) -> Tuple[bool, Set[black.TargetVersion]]: if value == "pyi": return True, set() else: versions = set() for version in value.split(","): tag = "cpy" if version.startswith("cpy"): version = version[len("cpy") :] elif version.startswith("pypy"): tag = "pypy" version = version[len("pypy") :] major_str, *rest = version.split(".") try: major = int(major_str) if major not in (2, 3): raise InvalidVariantHeader("major version must be 2 or 3") if len(rest) > 0: minor = int(rest[0]) if major == 2 and minor != 7: raise InvalidVariantHeader( "minor version must be 7 for Python 2" ) else: # Default to lowest supported minor version. minor = 7 if major == 2 else 3 version_str = f"{tag.upper()}{major}{minor}" # If PyPY is the same as CPython in some version, use # the corresponding CPython version. if tag == "pypy" and not hasattr(black.TargetVersion, version_str): version_str = f"CPY{major}{minor}" if major == 3 and not hasattr(black.TargetVersion, version_str): raise InvalidVariantHeader(f"3.{minor} is not supported") versions.add(black.TargetVersion[version_str]) except (KeyError, ValueError): raise InvalidVariantHeader("expected e.g. '3.7', 'pypy3.5'") return False, versions def patched_main() -> None: freeze_support() black.patch_click() Loading
tests/data/fmtonoff.py +2 −2 Changes for tests/data/fmtonoff.py: 2 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -39,7 +39,7 @@ def function_signature_stress_test(number:int,no_annotation=None,text:str='defau return text[number:-1] # fmt: on def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r''): offset = attr.ib(default=attr.Factory( lambda: _r.uniform(10000, 200000))) offset = attr.ib(default=attr.Factory( lambda: _r.uniform(1, 2))) assert task._cancel_stack[:len(old_stack)] == old_stack def spaces_types(a: int = 1, b: tuple = (), c: list = [], d: dict = {}, e: bool = True, f: int = -1, g: int = 1 if False else 2, h: str = "", i: str = r''): ... def spaces2(result= _core.Value(None)): Loading Loading @@ -225,7 +225,7 @@ def function_signature_stress_test(number:int,no_annotation=None,text:str='defau return text[number:-1] # fmt: on def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r""): offset = attr.ib(default=attr.Factory(lambda: _r.uniform(10000, 200_000))) offset = attr.ib(default=attr.Factory(lambda: _r.uniform(1, 2))) assert task._cancel_stack[: len(old_stack)] == old_stack Loading
tests/empty.toml 0 → 100644 +1 −0 Changes for tests/empty.toml: 1 added line, 0 removed lines. Original line number Diff line number Diff line # Empty configuration file; used in tests to avoid interference from Black's own config.