Skip to content

Commit eac3fe3

Browse files
gaogaotiantianHyukjinKwon
authored andcommitted
[SPARK-56018][PYTHON] Use ruff as formatter
### What changes were proposed in this pull request? Replace `black` with `ruff format`. ### Why are the changes needed? There are few reasons we should use `ruff` 1. We already use `ruff` for linter, using it for `format` will reduce a dependency, which makes upgrade easier 2. `ruff` is significantly faster than `black` which is helpful for our pre-commit hooks 3. `ruff` is more customizable if we need 4. Personally I think the taste of `ruff` is slightly better than `black`. For example: * `ruff` enforces blank spaces for `import`, `class` and `function` better * `ruff` will put the code back in a single line if it fits * `ruff` always uses double quote when it can There are some other details that you'll realize if you take a look at the diff. I think overall `ruff` generates slightly better code than `black` (and `ruff` is probably a bit more strict than `black`). ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? CI needs to pass because we removed the black dependency. ### Was this patch authored or co-authored using generative AI tooling? No. Closes #54840 from gaogaotiantian/use-ruff-as-formatter. Authored-by: Tian Gao <gaogaotiantian@hotmail.com> Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
1 parent 282ff6a commit eac3fe3

File tree

135 files changed

+1146
-1355
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+1146
-1355
lines changed

.github/workflows/build_and_test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ jobs:
842842
python-version: '3.12'
843843
- name: Install dependencies for Python CodeGen check
844844
run: |
845-
python3.12 -m pip install 'black==26.3.1' 'protobuf==6.33.5' 'mypy==1.8.0' 'mypy-protobuf==3.3.0'
845+
python3.12 -m pip install 'ruff==0.14.8' 'protobuf==6.33.5' 'mypy==1.8.0' 'mypy-protobuf==3.3.0'
846846
python3.12 -m pip list
847847
- name: Python CodeGen check for branch-3.5
848848
if: inputs.branch == 'branch-3.5'

.github/workflows/pages.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
run: |
6363
pip install 'sphinx==4.5.0' mkdocs 'pydata_sphinx_theme>=0.13' sphinx-copybutton nbsphinx numpydoc jinja2 markupsafe 'pyzmq<24.0.0' \
6464
ipython ipython_genutils sphinx_plotly_directive 'numpy>=1.22' pyarrow 'pandas==2.3.3' 'plotly>=4.8' 'docutils<0.18.0' \
65-
'flake8==3.9.0' 'mypy==1.8.0' 'pytest==7.1.3' 'pytest-mypy-plugins==1.9.3' 'black==26.3.1' \
65+
'flake8==3.9.0' 'mypy==1.8.0' 'pytest==7.1.3' 'pytest-mypy-plugins==1.9.3' 'ruff==0.14.8' \
6666
'pandas-stubs==1.2.0.53' 'grpcio==1.76.0' 'grpcio-status==1.76.0' 'protobuf==6.33.5' 'grpc-stubs==1.24.11' 'googleapis-common-protos-stubs==2.2.0' \
6767
'sphinxcontrib-applehelp==1.0.4' 'sphinxcontrib-devhelp==1.0.2' 'sphinxcontrib-htmlhelp==2.0.1' 'sphinxcontrib-qthelp==1.0.3' 'sphinxcontrib-serializinghtml==1.1.5'
6868
- name: Install Ruby for documentation generation

dev/create-release/spark-rm/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ RUN python3.10 -m pip install 'sphinx==4.5.0' mkdocs 'pydata_sphinx_theme>=0.13'
6161
sphinx-copybutton nbsphinx numpydoc jinja2 markupsafe 'pyzmq<24.0.0' \
6262
ipython ipython_genutils sphinx_plotly_directive 'numpy>=1.22' pyarrow pandas \
6363
'plotly>=4.8' 'docutils<0.18.0' 'flake8==3.9.0' 'mypy==1.19.1' 'pytest==7.1.3' \
64-
'pytest-mypy-plugins==1.9.3' 'black==26.3.1' 'pandas-stubs==1.2.0.53' \
64+
'pytest-mypy-plugins==1.9.3' 'ruff==0.14.8' 'pandas-stubs==1.2.0.53' \
6565
'grpcio==1.76.0' 'grpc-stubs==1.24.11' 'googleapis-common-protos-stubs==2.2.0' \
6666
'sphinxcontrib-applehelp==1.0.4' 'sphinxcontrib-devhelp==1.0.2' \
6767
'sphinxcontrib-htmlhelp==2.0.1' 'sphinxcontrib-qthelp==1.0.3' \

dev/gen-protos.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ for f in `find gen/proto/python -name "*.py*"`; do
116116
rm $f.bak
117117
done
118118
119-
black --config $SPARK_HOME/pyproject.toml gen/proto/python
119+
ruff format --config $SPARK_HOME/pyproject.toml gen/proto/python
120120
121121
# Last step copy the result files to the destination module.
122122
for f in `find gen/proto/python -name "*.py*"`; do

dev/is-changed.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def parse_opts():
4343
"--modules",
4444
type=str,
4545
default=default_value,
46-
help="A comma-separated list of modules to test " "(default: %s)" % default_value,
46+
help="A comma-separated list of modules to test (default: %s)" % default_value,
4747
)
4848

4949
args, unknown = parser.parse_known_args()

dev/lint-python

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ while (( "$#" )); do
7474
shift
7575
done
7676

77-
if [[ -z "$COMPILE_TEST$BLACK_TEST$PYSPARK_CUSTOM_ERRORS_CHECK_TEST$FLAKE8_TEST$RUFF_TEST$MYPY_TEST$MYPY_EXAMPLES_TEST$MYPY_DATA_TEST" ]]; then
77+
if [[ -z "$COMPILE_TEST$PYSPARK_CUSTOM_ERRORS_CHECK_TEST$FLAKE8_TEST$RUFF_TEST$MYPY_TEST$MYPY_EXAMPLES_TEST$MYPY_DATA_TEST" ]]; then
7878
COMPILE_TEST=true
7979
BLACK_TEST=true
8080
PYSPARK_CUSTOM_ERRORS_CHECK_TEST=true
@@ -315,6 +315,19 @@ ruff checks failed."
315315
echo
316316
fi
317317

318+
RUFF_REPORT=$( ($RUFF_BUILD format --diff python/pyspark dev python/packaging python/benchmarks) 2>&1)
319+
RUFF_STATUS=$?
320+
321+
if [ "$RUFF_STATUS" -ne 0 ]; then
322+
echo "ruff format checks failed:"
323+
echo "$RUFF_REPORT"
324+
echo "$RUFF_STATUS"
325+
exit "$RUFF_STATUS"
326+
else
327+
echo "ruff format checks passed."
328+
echo
329+
fi
330+
318331
}
319332

320333
function black_test {

dev/reformat-python

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2121
FWDIR="$( cd "$DIR"/.. && pwd )"
2222
cd "$FWDIR"
2323

24-
BLACK_BUILD="${PYTHON_EXECUTABLE} -m black"
25-
BLACK_VERSION="26.3.1"
26-
$PYTHON_EXECUTABLE -c 'import black' 2> /dev/null
24+
RUFF_BUILD="${PYTHON_EXECUTABLE} -m ruff"
25+
RUFF_VERSION="0.14.8"
26+
$PYTHON_EXECUTABLE -c 'import ruff' 2> /dev/null
2727
if [ $? -ne 0 ]; then
28-
echo "The Python library providing the 'black' module was not found. Please install Black, for example, via 'pip install black==$BLACK_VERSION'."
28+
echo "The Python library providing the 'ruff' module was not found. Please install Ruff, for example, via 'pip install ruff==$RUFF_VERSION'."
2929
exit 1
3030
fi
3131

32-
$BLACK_BUILD python/pyspark dev python/packaging python/benchmarks
32+
$RUFF_BUILD format python/pyspark dev python/packaging python/benchmarks

dev/spark-test-image/connect-gen-protos/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ ENV PATH="$VIRTUAL_ENV/bin:$PATH"
5353
RUN python3.12 -m pip install \
5454
'mypy==1.19.1' \
5555
'mypy-protobuf==3.3.0' \
56-
'black==26.3.1'
56+
'ruff==0.14.8'
5757

5858
# Mount the Spark repo at /spark
5959
WORKDIR /spark

dev/spark-test-image/docs/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ ENV PATH="$VIRTUAL_ENV/bin:$PATH"
8989
# See 'docutils<0.18.0' in SPARK-39421
9090
RUN python3.12 -m pip install 'sphinx==4.5.0' mkdocs 'pydata_sphinx_theme>=0.13' sphinx-copybutton nbsphinx numpydoc jinja2 markupsafe \
9191
ipython ipython_genutils sphinx_plotly_directive 'numpy>=1.22' 'pyarrow>=23.0.0' 'pandas==2.3.3' 'plotly>=4.8' 'docutils<0.18.0' \
92-
'flake8==3.9.0' 'mypy==1.19.1' 'pytest==7.1.3' 'pytest-mypy-plugins==1.9.3' 'black==26.3.1' \
92+
'flake8==3.9.0' 'mypy==1.19.1' 'pytest==7.1.3' 'pytest-mypy-plugins==1.9.3' 'ruff==0.14.8' \
9393
'pandas-stubs==1.2.0.53' 'grpcio==1.76.0' 'grpcio-status==1.76.0' 'protobuf==6.33.5' 'grpc-stubs==1.24.11' 'googleapis-common-protos-stubs==2.2.0' \
9494
'sphinxcontrib-applehelp==1.0.4' 'sphinxcontrib-devhelp==1.0.2' 'sphinxcontrib-htmlhelp==2.0.1' 'sphinxcontrib-qthelp==1.0.3' 'sphinxcontrib-serializinghtml==1.1.5' \
9595
&& python3.12 -m pip cache purge

dev/spark-test-image/lint/Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ RUN python3.12 -m venv $VIRTUAL_ENV
7777
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
7878

7979
RUN python3.12 -m pip install \
80-
'black==26.3.1' \
8180
'flake8==3.9.0' \
8281
'ruff==0.14.8' \
8382
'googleapis-common-protos-stubs==2.2.0' \

0 commit comments

Comments
 (0)