From 085c5fd2481b6d3042a244774d397efefc03f16b Mon Sep 17 00:00:00 2001 From: Alexandr Artemyev Date: Thu, 11 Aug 2022 23:41:14 +0600 Subject: [PATCH 1/4] feat(#3195): add preview option support for blackd --- CHANGES.md | 2 ++ docs/usage_and_configuration/black_as_a_server.md | 3 +++ src/blackd/__init__.py | 4 ++++ tests/test_blackd.py | 7 +++++++ 4 files changed, 16 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 1fc8c65d6d8..05b2bc7f359 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -29,6 +29,8 @@ +- `blackd` now supports preview style via `X-PREVIEW` header (#3195) + ### Configuration diff --git a/docs/usage_and_configuration/black_as_a_server.md b/docs/usage_and_configuration/black_as_a_server.md index fc9d1cab716..eee98677a17 100644 --- a/docs/usage_and_configuration/black_as_a_server.md +++ b/docs/usage_and_configuration/black_as_a_server.md @@ -56,6 +56,9 @@ The headers controlling how source code is formatted are: - `X-Skip-Magic-Trailing-Comma`: corresponds to the `--skip-magic-trailing-comma` command line flag. If present and its value is not the empty string, trailing commas will not be used as a reason to split lines. +- `X-Preview`: corresponds to the `--preview`command line flag. If present and its value + is not the empty string, experimental and potentially disruptive style changes will be + used. - `X-Fast-Or-Safe`: if set to `fast`, `blackd` will act as _Black_ does when passed the `--fast` command line flag. - `X-Python-Variant`: if set to `pyi`, `blackd` will act as _Black_ does when passed the diff --git a/src/blackd/__init__.py b/src/blackd/__init__.py index a6de79fbeaa..9fcb85ca5e6 100644 --- a/src/blackd/__init__.py +++ b/src/blackd/__init__.py @@ -32,6 +32,7 @@ PYTHON_VARIANT_HEADER = "X-Python-Variant" SKIP_STRING_NORMALIZATION_HEADER = "X-Skip-String-Normalization" SKIP_MAGIC_TRAILING_COMMA = "X-Skip-Magic-Trailing-Comma" +PREVIEW = "X-PREVIEW" FAST_OR_SAFE_HEADER = "X-Fast-Or-Safe" DIFF_HEADER = "X-Diff" @@ -41,6 +42,7 @@ PYTHON_VARIANT_HEADER, SKIP_STRING_NORMALIZATION_HEADER, SKIP_MAGIC_TRAILING_COMMA, + PREVIEW, FAST_OR_SAFE_HEADER, DIFF_HEADER, ] @@ -109,6 +111,7 @@ async def handle(request: web.Request, executor: Executor) -> web.Response: skip_magic_trailing_comma = bool( request.headers.get(SKIP_MAGIC_TRAILING_COMMA, False) ) + preview = bool(request.headers.get(PREVIEW, False)) fast = False if request.headers.get(FAST_OR_SAFE_HEADER, "safe") == "fast": fast = True @@ -118,6 +121,7 @@ async def handle(request: web.Request, executor: Executor) -> web.Response: line_length=line_length, string_normalization=not skip_string_normalization, magic_trailing_comma=not skip_magic_trailing_comma, + preview=preview, ) req_bytes = await request.content.read() charset = request.charset if request.charset is not None else "utf8" diff --git a/tests/test_blackd.py b/tests/test_blackd.py index 18b2c98ac1f..1d12113a3f3 100644 --- a/tests/test_blackd.py +++ b/tests/test_blackd.py @@ -167,6 +167,13 @@ async def test_blackd_invalid_line_length(self) -> None: ) self.assertEqual(response.status, 400) + @unittest_run_loop + async def test_blackd_preview(self) -> None: + response = await self.client.post( + "/", data=b'print("hello")\n', headers={blackd.PREVIEW: "true"} + ) + self.assertEqual(response.status, 204) + @unittest_run_loop async def test_blackd_response_black_version_header(self) -> None: response = await self.client.post("/") From 4fa5699bd94056c0cce3b654cfbfa3c0b4d6f676 Mon Sep 17 00:00:00 2001 From: Alexandr Artemyev Date: Fri, 12 Aug 2022 13:53:55 +0600 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Richard Si <63936253+ichard26@users.noreply.github.com> --- CHANGES.md | 2 +- docs/usage_and_configuration/black_as_a_server.md | 6 +++--- src/blackd/__init__.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 05b2bc7f359..79f4ce59187 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -29,7 +29,7 @@ -- `blackd` now supports preview style via `X-PREVIEW` header (#3195) +- `blackd` now supports preview style via `X-Preview` header (#3217) ### Configuration diff --git a/docs/usage_and_configuration/black_as_a_server.md b/docs/usage_and_configuration/black_as_a_server.md index eee98677a17..12ee11f4ed0 100644 --- a/docs/usage_and_configuration/black_as_a_server.md +++ b/docs/usage_and_configuration/black_as_a_server.md @@ -54,10 +54,10 @@ The headers controlling how source code is formatted are: command line flag. If present and its value is not the empty string, no string normalization will be performed. - `X-Skip-Magic-Trailing-Comma`: corresponds to the `--skip-magic-trailing-comma` - command line flag. If present and its value is not the empty string, trailing commas + command line flag. If present and its value is not an empty string, trailing commas will not be used as a reason to split lines. -- `X-Preview`: corresponds to the `--preview`command line flag. If present and its value - is not the empty string, experimental and potentially disruptive style changes will be +- `X-Preview`: corresponds to the `--preview` command line flag. If present and its value + is not an empty string, experimental and potentially disruptive style changes will be used. - `X-Fast-Or-Safe`: if set to `fast`, `blackd` will act as _Black_ does when passed the `--fast` command line flag. diff --git a/src/blackd/__init__.py b/src/blackd/__init__.py index 9fcb85ca5e6..e52a9917cf3 100644 --- a/src/blackd/__init__.py +++ b/src/blackd/__init__.py @@ -32,7 +32,7 @@ PYTHON_VARIANT_HEADER = "X-Python-Variant" SKIP_STRING_NORMALIZATION_HEADER = "X-Skip-String-Normalization" SKIP_MAGIC_TRAILING_COMMA = "X-Skip-Magic-Trailing-Comma" -PREVIEW = "X-PREVIEW" +PREVIEW = "X-Preview" FAST_OR_SAFE_HEADER = "X-Fast-Or-Safe" DIFF_HEADER = "X-Diff" From 3118a48c2b71f2ac2cc747418e9141f8545f6b2b Mon Sep 17 00:00:00 2001 From: Alexandr Artemyev Date: Fri, 12 Aug 2022 14:45:16 +0600 Subject: [PATCH 3/4] Update black_as_a_server.md --- docs/usage_and_configuration/black_as_a_server.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/usage_and_configuration/black_as_a_server.md b/docs/usage_and_configuration/black_as_a_server.md index 12ee11f4ed0..a2d4252109a 100644 --- a/docs/usage_and_configuration/black_as_a_server.md +++ b/docs/usage_and_configuration/black_as_a_server.md @@ -56,9 +56,9 @@ The headers controlling how source code is formatted are: - `X-Skip-Magic-Trailing-Comma`: corresponds to the `--skip-magic-trailing-comma` command line flag. If present and its value is not an empty string, trailing commas will not be used as a reason to split lines. -- `X-Preview`: corresponds to the `--preview` command line flag. If present and its value - is not an empty string, experimental and potentially disruptive style changes will be - used. +- `X-Preview`: corresponds to the `--preview` command line flag. If present and its + value is not an empty string, experimental and potentially disruptive style changes + will be used. - `X-Fast-Or-Safe`: if set to `fast`, `blackd` will act as _Black_ does when passed the `--fast` command line flag. - `X-Python-Variant`: if set to `pyi`, `blackd` will act as _Black_ does when passed the From fa67d763d8f3975b00ee32843a8dfaef9aa606a4 Mon Sep 17 00:00:00 2001 From: Alexandr Artemyev Date: Sat, 13 Aug 2022 04:09:17 +0600 Subject: [PATCH 4/4] Add myself to the AUTHORS --- AUTHORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.md b/AUTHORS.md index faa2b05840f..f30cd55a08b 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -20,6 +20,7 @@ Multiple contributions by: - [Adam Johnson](mailto:me@adamj.eu) - [Adam Williamson](mailto:adamw@happyassassin.net) - [Alexander Huynh](mailto:github@grande.coffee) +- [Alexandr Artemyev](mailto:mogost@gmail.com) - [Alex Vandiver](mailto:github@chmrr.net) - [Allan Simon](mailto:allan.simon@supinfo.com) - Anders-Petter Ljungquist