Skip to content

Commit

Permalink
[Python] Migrate from yapf to black (#33138)
Browse files Browse the repository at this point in the history
- Switched  from yapf to black
- Reconfigure isort for black
- Resolve black/pylint idiosyncrasies 

Note: I used `--experimental-string-processing` because black was
producing "implicit string concatenation", similar to what described
here: psf/black#1837. While currently this
feature is experimental, it will be enabled by default:
psf/black#2188. After running black with the
new string processing so that the generated code merges these `"hello" "
world"` strings concatenations, then I removed
`--experimental-string-processing` for stability, and regenerated the
code again.

To the reviewer: don't even try to open "Files Changed" tab 😄 It's
better to review commit-by-commit, and ignore `run black and isort`.
  • Loading branch information
sergiitk authored Jun 9, 2023
1 parent 1138c68 commit 9f6423f
Show file tree
Hide file tree
Showing 67 changed files with 6,049 additions and 4,079 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ from your dev environment. You need:

### Making changes to the driver
1. Install additional dev packages: `pip install -r requirements-dev.txt`
2. Use `./bin/yapf.sh` and `./bin/isort.sh` helpers to auto-format code.
2. Use `./bin/black.sh` and `./bin/isort.sh` helpers to auto-format code.

### Updating Python Dependencies

Expand Down
14 changes: 8 additions & 6 deletions bin/yapf.sh → bin/black.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ set -eo pipefail

display_usage() {
cat <<EOF >/dev/stderr
A helper to run yapf formatter.
A helper to run black formatter.
USAGE: $0 [--diff]
--diff: Do not apply changes, only show the diff
--check: Do not apply changes, only print what files will be changed
ENVIRONMENT:
XDS_K8S_DRIVER_VENV_DIR: the path to python virtual environment directory
Default: $XDS_K8S_DRIVER_DIR/venv
EXAMPLES:
$0
$0 --diff
$0 --check
EOF
exit 1
}
Expand All @@ -48,11 +50,11 @@ source "${XDS_K8S_DRIVER_DIR}/bin/ensure_venv.sh"

if [[ "$1" == "--diff" ]]; then
readonly MODE="--diff"
elif [[ "$1" == "--check" ]]; then
readonly MODE="--check"
else
readonly MODE="--in-place"
readonly VERBOSE="--verbose" # print out file names while processing
readonly MODE=""
fi

exec python -m yapf "${MODE}" ${VERBOSE:-} \
--parallel --recursive --style=../../../setup.cfg \
framework bin tests
# shellcheck disable=SC2086
exec python -m black --config=../../../black.toml ${MODE} .
Loading

0 comments on commit 9f6423f

Please sign in to comment.