From 5ade4a037ea5597076a2bb817f0b889e6248b0d5 Mon Sep 17 00:00:00 2001 From: Daniel Rainer Date: Wed, 8 Oct 2025 17:40:31 +0200 Subject: [PATCH] style: replace black with ruff for Python formatting Ruff's default format is very similar to black's, so there are only a few changes made to our Python code. They are all contained in this commit. The primary benefit of this change is that ruff's performance is about an order of magnitude better, reducing runtime on this repo down to under 20ms on my machine, compared to over 150ms with black, and even more if any changes are performed by black. Closes #11894 Closes #11918 --- CONTRIBUTING.rst | 2 +- build_tools/style.fish | 12 ++++++------ share/tools/web_config/webconfig.py | 2 +- tests/pexpects/torn_escapes.py | 8 ++------ 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 3b29d7584..b57d304d4 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -110,7 +110,7 @@ before committing your change. That will run our autoformatters: - ``rustfmt`` for Rust - ``fish_indent`` (shipped with fish) for fish script -- ``black`` for python +- ``ruff format`` for python If you’ve already committed your changes that’s okay since it will then check the files in the most recent commit. This can be useful after diff --git a/build_tools/style.fish b/build_tools/style.fish index 707246825..6f6b18c16 100755 --- a/build_tools/style.fish +++ b/build_tools/style.fish @@ -44,7 +44,7 @@ if test $all = yes end end set fish_files $workspace_root/{benchmarks,build_tools,etc,share}/**.fish - set python_files {doc_src,share,tests}/**.py + set python_files . else # Format the files specified as arguments. set -l files $argv @@ -76,19 +76,19 @@ if set -q fish_files[1] end if set -q python_files[1] - if not type -q black + if not type -q ruff echo - echo $yellow'Please install `black` to style python'$normal + echo $yellow'Please install `ruff` to style python'$normal exit 127 end - echo === Running "$green"black"$normal" + echo === Running "$green"ruff format"$normal" if set -l -q _flag_check - if not black --check $python_files + if not ruff format --check $python_files echo $red"Python files are not formatted correctly."$normal exit 1 end else - black $python_files + ruff format $python_files end end diff --git a/share/tools/web_config/webconfig.py b/share/tools/web_config/webconfig.py index f8538501a..117344509 100755 --- a/share/tools/web_config/webconfig.py +++ b/share/tools/web_config/webconfig.py @@ -1423,7 +1423,7 @@ fish_bin_path = None fish_bin_name = "fish.exe" if is_windows() else "fish" if not fish_bin_dir: - print("The $__fish_bin_dir environment variable is not set. " "Looking in $PATH...") + print("The $__fish_bin_dir environment variable is not set. Looking in $PATH...") fish_bin_path = find_executable(fish_bin_name) if not fish_bin_path: print("fish could not be found. Is fish installed correctly?") diff --git a/tests/pexpects/torn_escapes.py b/tests/pexpects/torn_escapes.py index a528ef81e..1ab25b783 100644 --- a/tests/pexpects/torn_escapes.py +++ b/tests/pexpects/torn_escapes.py @@ -38,9 +38,7 @@ sendline( echo Got SIGUSR1 $sigusr1_count; commandline -f repaint; end -""".strip().replace( - "\n", os.linesep - ) +""".strip().replace("\n", os.linesep) ) expect_prompt() @@ -51,9 +49,7 @@ sendline( set wacky_count (math $wacky_count + 1); echo Wacky Handler $wacky_count end -""".strip().replace( - "\n", os.linesep - ) +""".strip().replace("\n", os.linesep) ) expect_prompt()