-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Implement blackd python client #4774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cooperlees
merged 6 commits into
psf:main
from
sgaist:feat/implement-python-blackd-client
Oct 19, 2025
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
35a5f70
feat: implement blackd python client
sgaist d72d4c9
Merge branch 'main' into feat/implement-python-blackd-client
sgaist fdcb419
chore: fixed CHANGES.md
sgaist 5b7ae04
Merge branch 'main' into feat/implement-python-blackd-client
sgaist d53ed9e
chore: apply suggestions from code review
sgaist f396104
Merge branch 'main' into feat/implement-python-blackd-client
cooperlees File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| from typing import Optional | ||
|
|
||
| import aiohttp | ||
| from aiohttp.typedefs import StrOrURL | ||
|
|
||
| import black | ||
|
|
||
| _DEFAULT_HEADERS = {"Content-Type": "text/plain; charset=utf-8"} | ||
|
|
||
|
|
||
| class BlackDClient: | ||
| def __init__( | ||
| self, | ||
| url: StrOrURL = "http://localhost:9090", | ||
| line_length: Optional[int] = None, | ||
| skip_source_first_line: bool = False, | ||
| skip_string_normalization: bool = False, | ||
| skip_magic_trailing_comma: bool = False, | ||
| preview: bool = False, | ||
| fast: bool = False, | ||
| python_variant: Optional[str] = None, | ||
| diff: bool = False, | ||
| headers: Optional[dict[str, str]] = None, | ||
| ): | ||
| """ | ||
| Initialize a BlackDClient object. | ||
| :param url: The URL of the BlackD server. | ||
| :param line_length: The maximum line length. | ||
| Corresponds to the ``--line-length`` CLI option. | ||
| :param skip_source_first_line: True to skip the first line of the source. | ||
| Corresponds to the ``--skip-string-normalization`` CLI option. | ||
| :param skip_string_normalization: True to skip string normalization. | ||
| Corresponds to the ``--skip-string-normalization`` CLI option. | ||
| :param skip_magic_trailing_comma: True to skip magic trailing comma. | ||
| Corresponds to the ``--skip-magic-trailing-comma`` CLI option. | ||
| :param preview: True to enable experimental preview mode. | ||
| Corresponds to the ``--preview`` CLI option. | ||
| :param fast: True to enable fast mode. | ||
| Corresponds to the ``--fast`` CLI option. | ||
| :param python_variant: The Python variant to use. | ||
| Corresponds to the ``--pyi`` CLI option if this is "pyi". | ||
| Otherwise, corresponds to the ``--target-version`` CLI option. | ||
| :param diff: True to enable diff mode. | ||
| Corresponds to the ``--diff`` CLI option. | ||
| :param headers: A dictionary of additional custom headers to send with | ||
| the request. | ||
| """ | ||
| self.url = url | ||
| self.headers = _DEFAULT_HEADERS.copy() | ||
|
|
||
| if line_length is not None: | ||
| self.headers["X-Line-Length"] = str(line_length) | ||
| if skip_source_first_line: | ||
| self.headers["X-Skip-Source-First-Line"] = "yes" | ||
| if skip_string_normalization: | ||
| self.headers["X-Skip-String-Normalization"] = "yes" | ||
| if skip_magic_trailing_comma: | ||
| self.headers["X-Skip-Magic-Trailing-Comma"] = "yes" | ||
| if preview: | ||
| self.headers["X-Preview"] = "yes" | ||
| if fast: | ||
| self.headers["X-Fast-Or-Safe"] = "fast" | ||
| if python_variant is not None: | ||
| self.headers["X-Python-Variant"] = python_variant | ||
| if diff: | ||
| self.headers["X-Diff"] = "yes" | ||
|
|
||
| if headers is not None: | ||
| self.headers.update(headers) | ||
|
|
||
| async def format_code(self, unformatted_code: str) -> str: | ||
| async with aiohttp.ClientSession() as session: | ||
| async with session.post( | ||
| self.url, headers=self.headers, data=unformatted_code.encode("utf-8") | ||
| ) as response: | ||
| if response.status == 204: | ||
| # Input is already well-formatted | ||
| return unformatted_code | ||
| elif response.status == 200: | ||
| # Formatting was needed | ||
| return await response.text() | ||
| elif response.status == 400: | ||
| # Input contains a syntax error | ||
| error_message = await response.text() | ||
| raise black.InvalidInput(error_message) | ||
| elif response.status == 500: | ||
| # Other kind of error while formatting | ||
| error_message = await response.text() | ||
| raise RuntimeError(f"Error while formatting: {error_message}") | ||
| else: | ||
| # Unexpected response status code | ||
| raise RuntimeError( | ||
| f"Unexpected response status code: {response.status}" | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.