-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[py] Format the code using black
#13679
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
Conversation
|
PR Description updated to latest commit (9a3dae5)
|
PR Review
✨ Review tool usage guide:Overview: With a configuration file, use the following template:
See the review usage page for a comprehensive guide on using this tool. |
PR Code Suggestions
✨ Improve tool usage guide:Overview: With a configuration file, use the following template:
See the improve usage page for a more comprehensive guide on using this tool. |
|
Should also replace this: Lines 652 to 655 in d65e38e
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this replace the current linting for Python via Tox (isort+black+flake8+docformatter)? It is only of the things that we couldn't migrate to Bazel on CI -
selenium/.github/workflows/ci-python.yml
Lines 36 to 56 in 47b4cdc
| lint: | |
| name: Lint | |
| needs: build | |
| runs-on: ubuntu-20.04 | |
| steps: | |
| - name: Checkout source tree | |
| uses: actions/checkout@v4 | |
| - name: Set up Python 3.8 | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: 3.8 | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install tox==4.6.4 | |
| - name: Test with tox | |
| run: tox -c py/tox.ini | |
| env: | |
| # If this fails, it will exit. Local work should be using `tox -e linting` prior to committing. | |
| # the linting-ci recipe exists solely for CI and will not attempt to rewrite any files in-place etc. | |
| TOXENV: linting-ci |
|
We are using black through the configuration in tox.ini, so this PR is probably no longer needed. |
|
@cgoldberg It would be great to have Black working through Bazel so it's part of ./scripts/format.sh. Can we merge this and use black via tox.ini too? |
|
AFAIK, |
|
Yes, but it depends on a local installation of Python/pip/tox - it would be nice to automatically set it up hermetically via Bazel. |
OK.. I understand now. The only problem with that is we use other linters/formatters besides black (isort, autoflake, flake8, docformatter), so those would need to be added to bazel also if we want to keep the same formatting that we have now with tox. |
|
... just thinking about this some more, we might also consider replacing the Python formatters/linters with ruff: https://docs.astral.sh/ruff/ It can do everything that black/autoflake/flake8/isort can do and more (it's also much faster). That goes beyond this PR, but it's something to think about before making CI changes that tie us to black. |
|
@cgoldberg If we can use ruff for everything, it should be relatively easy to plug it into Bazel because it's just a binary, we don't even need Python installation to run formatting and can probably use https://github.com/aspect-build/rules_lint. I can help with Bazel part if you take care of changes in |
|
I'll setup ruff and and ping you on the PR to configure bazel for it. It might be a few days though. |
|
ruff is really nice btw... It's developed by the same people that made |
Type
enhancement, documentation
Description
py-blacklint configuration for workspace setup.Changes walkthrough
8 files
defs.bzl
Integrate Black Formatting into Bazel Build Definitionspy/defs.bzl
py_with_lint_macro.bzlforpy_binaryandpy_librarydefinitions.black_configto the list of available macros.black-bin.py
Add Black Binary Execution Scriptpy/private/black-bin.py
main function.
black_config.bzl
Define Black Configuration Rulepy/private/black_config.bzl
BlackConfigInfoprovider with fields for black binary, linelength, and Python versions.
black_configrule to provide Black configuration.black_test.bzl
Implement Black Testing Rulepy/private/black_test.bzl
black_testrule implementation for running Black as tests onspecified sources.
BlackConfigInfofor test configuration.py_with_lint_macro.bzl
Macro Definitions for Python Linting with Blackpy/private/py_with_lint_macro.bzl
black_test) to Python targets.py_binary,py_library, andpy_testdefinitions.
pytest.bzl
Integrate Black Lint Tests into Pytest Definitionpy/private/pytest.bzl
pytest_testdefinition.format.sh
Add Python Formatting with Black to Format Scriptscripts/format.sh
length.
BUILD.bazel
Define Black Binary Target and Aliaspy/private/BUILD.bazel
black-binas a py_binary target.blackan alias toblack-bin.2 files
WORKSPACE
Register Python Black Lint ConfigurationWORKSPACE
py-blacklint configuration in the workspace setup.BUILD.bazel
Configure Black Formatting in Python Buildpy/BUILD.bazel
black_configrule instance for Python formatting configuration.2 files
requirements.txt
Add Black to Python Requirementspy/requirements.txt
blackto the list of Python requirements.requirements_lock.txt
Lock Black and Its Dependenciespy/requirements_lock.txt
blackand its dependencies in the locked requirements.