Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 07b68e2

Browse files
Mogostichard26
andauthored
add preview option support for blackd (psf#3217)
Fixes psf#3195 Co-authored-by: Richard Si <[email protected]>
1 parent 680cbe3 commit 07b68e2

5 files changed

Lines changed: 18 additions & 1 deletion

File tree

AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Multiple contributions by:
2020
- [Adam Johnson](mailto:[email protected])
2121
- [Adam Williamson](mailto:[email protected])
2222
- [Alexander Huynh](mailto:[email protected])
23+
- [Alexandr Artemyev](mailto:[email protected])
2324
- [Alex Vandiver](mailto:[email protected])
2425
- [Allan Simon](mailto:[email protected])
2526
- Anders-Petter Ljungquist

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
<!-- Changes to blackd -->
3131

32+
- `blackd` now supports preview style via `X-Preview` header (#3217)
33+
3234
### Configuration
3335

3436
<!-- Changes to how Black can be configured -->

docs/usage_and_configuration/black_as_a_server.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ The headers controlling how source code is formatted are:
5454
command line flag. If present and its value is not the empty string, no string
5555
normalization will be performed.
5656
- `X-Skip-Magic-Trailing-Comma`: corresponds to the `--skip-magic-trailing-comma`
57-
command line flag. If present and its value is not the empty string, trailing commas
57+
command line flag. If present and its value is not an empty string, trailing commas
5858
will not be used as a reason to split lines.
59+
- `X-Preview`: corresponds to the `--preview` command line flag. If present and its
60+
value is not an empty string, experimental and potentially disruptive style changes
61+
will be used.
5962
- `X-Fast-Or-Safe`: if set to `fast`, `blackd` will act as _Black_ does when passed the
6063
`--fast` command line flag.
6164
- `X-Python-Variant`: if set to `pyi`, `blackd` will act as _Black_ does when passed the

src/blackd/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
PYTHON_VARIANT_HEADER = "X-Python-Variant"
3333
SKIP_STRING_NORMALIZATION_HEADER = "X-Skip-String-Normalization"
3434
SKIP_MAGIC_TRAILING_COMMA = "X-Skip-Magic-Trailing-Comma"
35+
PREVIEW = "X-Preview"
3536
FAST_OR_SAFE_HEADER = "X-Fast-Or-Safe"
3637
DIFF_HEADER = "X-Diff"
3738

@@ -41,6 +42,7 @@
4142
PYTHON_VARIANT_HEADER,
4243
SKIP_STRING_NORMALIZATION_HEADER,
4344
SKIP_MAGIC_TRAILING_COMMA,
45+
PREVIEW,
4446
FAST_OR_SAFE_HEADER,
4547
DIFF_HEADER,
4648
]
@@ -109,6 +111,7 @@ async def handle(request: web.Request, executor: Executor) -> web.Response:
109111
skip_magic_trailing_comma = bool(
110112
request.headers.get(SKIP_MAGIC_TRAILING_COMMA, False)
111113
)
114+
preview = bool(request.headers.get(PREVIEW, False))
112115
fast = False
113116
if request.headers.get(FAST_OR_SAFE_HEADER, "safe") == "fast":
114117
fast = True
@@ -118,6 +121,7 @@ async def handle(request: web.Request, executor: Executor) -> web.Response:
118121
line_length=line_length,
119122
string_normalization=not skip_string_normalization,
120123
magic_trailing_comma=not skip_magic_trailing_comma,
124+
preview=preview,
121125
)
122126
req_bytes = await request.content.read()
123127
charset = request.charset if request.charset is not None else "utf8"

tests/test_blackd.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ async def test_blackd_invalid_line_length(self) -> None:
167167
)
168168
self.assertEqual(response.status, 400)
169169

170+
@unittest_run_loop
171+
async def test_blackd_preview(self) -> None:
172+
response = await self.client.post(
173+
"/", data=b'print("hello")\n', headers={blackd.PREVIEW: "true"}
174+
)
175+
self.assertEqual(response.status, 204)
176+
170177
@unittest_run_loop
171178
async def test_blackd_response_black_version_header(self) -> None:
172179
response = await self.client.post("/")

0 commit comments

Comments
 (0)