Skip to content

Commit 3e59864

Browse files
committed
chore: use ruff to lint and format files
There have been a few attempts at doing these, but the efforts are stale. Since `ruff` is very popular these days and it is only a single binary and is fast, let's use that. From now on we will be able to maintain the files better, because we have a CI step testing the compliance. Closes #3073
1 parent 5511aaf commit 3e59864

94 files changed

Lines changed: 515 additions & 801 deletions

File tree

Some content is hidden

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

.agents/skills/buildkite-get-results/scripts/get_buildkite_results.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ def get_pr_checks(pr_number):
1717
stderr=subprocess.DEVNULL,
1818
)
1919
except FileNotFoundError:
20-
print(
21-
"Error: 'gh' (GitHub CLI) is not installed or not in PATH.", file=sys.stderr
22-
)
20+
print("Error: 'gh' (GitHub CLI) is not installed or not in PATH.", file=sys.stderr)
2321
sys.exit(1)
2422
except subprocess.CalledProcessError:
2523
print("Error: 'gh' command failed. Is it installed?", file=sys.stderr)
@@ -165,15 +163,18 @@ def main():
165163

166164
build_state = data.get("state", "Unknown")
167165
print(f"Build State: {build_state}")
168-
166+
169167
jobs = data.get("jobs", [])
170168
jobs_count = data.get("statistics", {}).get("jobs_count", 0)
171-
169+
172170
print(f"Total jobs reported: {jobs_count}")
173171
print(f"Jobs found in data: {len(jobs)}")
174-
172+
175173
if jobs_count != len(jobs):
176-
print(f"WARNING: Reported job count ({jobs_count}) does not match jobs found ({len(jobs)}).", file=sys.stderr)
174+
print(
175+
f"WARNING: Reported job count ({jobs_count}) does not match jobs found ({len(jobs)}).",
176+
file=sys.stderr,
177+
)
177178

178179
print("-" * 40)
179180

.agents/skills/buildkite-retry-job/scripts/retry_buildkite_jobs.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ def make_request(url, method="GET", data=None, token=None):
3131

3232

3333
def main():
34-
parser = argparse.ArgumentParser(
35-
description="Retry failed jobs in a Buildkite build."
36-
)
34+
parser = argparse.ArgumentParser(description="Retry failed jobs in a Buildkite build.")
3735
parser.add_argument("org", help="Organization slug")
3836
parser.add_argument("pipeline", help="Pipeline slug")
3937
parser.add_argument("build", help="Build number")
@@ -46,9 +44,7 @@ def main():
4644
token = os.environ.get("BUILDKITE_API_TOKEN")
4745

4846
if not token:
49-
print(
50-
"Please set the BUILDKITE_API_TOKEN environment variable.", file=sys.stderr
51-
)
47+
print("Please set the BUILDKITE_API_TOKEN environment variable.", file=sys.stderr)
5248
sys.exit(1)
5349

5450
url = f"https://api.buildkite.com/v2/organizations/{args.org}/pipelines/{args.pipeline}/builds/{args.build}"

.github/workflows/ruff.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: ruff
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
types:
9+
- opened
10+
- synchronize
11+
12+
defaults:
13+
run:
14+
shell: bash
15+
16+
jobs:
17+
lint:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@v6
21+
- name: Install ruff
22+
run: pip install ruff==0.15.13
23+
- name: Run ruff check
24+
run: ruff check --output-format=github
25+
- name: Run ruff format check
26+
run: ruff format --check

.pre-commit-config.yaml

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,14 @@ repos:
2929
- --warnings=all
3030
- id: buildifier-lint
3131
args: *args
32-
- repo: https://github.com/pycqa/isort
33-
rev: 5.12.0
32+
- repo: https://github.com/astral-sh/ruff-pre-commit
33+
rev: v0.15.13
3434
hooks:
35-
- id: isort
36-
name: isort (python)
37-
args:
38-
- --profile
39-
- black
40-
- repo: https://github.com/psf/black
41-
rev: 25.1.0
42-
hooks:
43-
- id: black
35+
- id: ruff
36+
args: [--fix, --exit-non-zero-on-fix]
37+
exclude: ^(gazelle/python/testdata|tests/pypi/whl_library/testdata)
38+
- id: ruff-format
39+
exclude: ^(gazelle/python/testdata|tests/pypi/whl_library/testdata)
4440
- repo: local
4541
hooks:
4642
- id: update-deleted-packages

CONTRIBUTING.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,14 +374,14 @@ If you did `pre-commit install`, various tools are run when you do `git commit`.
374374
This might show as an error such as:
375375

376376
```
377-
[INFO] Installing environment for https://github.com/psf/black.
377+
[INFO] Installing environment for https://github.com/astral-sh/ruff-pre-commit.
378378
[INFO] Once installed this environment will be reused.
379379
[INFO] This may take a few minutes...
380380
An unexpected error has occurred: CalledProcessError: command: ...
381381
```
382382

383383
To fix, you'll need to figure out what command is failing and why. Because these
384384
are tools that run locally, its likely you'll need to fix something with your
385-
environment or the installation of the tools. For Python tools (e.g. black or
386-
isort), you can try using a different Python version in your shell by using
387-
tools such as [pyenv](https://github.com/pyenv/pyenv).
385+
environment or the installation of the tools. For Python tools (e.g. ruff), you
386+
can try using a different Python version in your shell by using tools such as
387+
[pyenv](https://github.com/pyenv/pyenv).

docs/conf.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,26 @@
6969
"api/python/defs": "/api/rules_python/python/defs.html",
7070
"api/python/index": "/api/rules_python/python/index.html",
7171
"api/python/py_runtime_info": "/api/rules_python/python/py_runtime_info.html",
72-
"api/python/private/common/py_library_rule_bazel": "/api/rules_python/python/private/py_library_rule.html",
73-
"api/python/private/common/py_test_rule_bazel": "/api/rules_python/python/private/py_test_rule_bazel.html",
74-
"api/python/private/common/py_binary_rule_bazel": "/api/rules_python/python/private/py_binary_rule.html",
75-
"api/python/private/common/py_runtime_rule": "/api/rules_python/python/private/py_runtime_rule.html",
76-
"api/python/extensions/pip": "/api/rules_python/python/extensions/pip.html",
77-
"api/python/extensions/python": "/api/rules_python/python/extensions/python.html",
78-
"api/python/entry_points/py_console_script_binary": "/api/rules_python/python/entry_points/py_console_script_binary.html",
79-
"api/python/cc/py_cc_toolchain_info": "/api/rules_python/python/cc/py_cc_toolchain_info.html",
80-
"api/python/cc/index": "/api/rules_python/python/cc/index.html",
81-
"api/python/py_cc_link_params_info": "/api/rules_python/python/py_cc_link_params_info.html",
82-
"api/python/runtime_env_toolchains/index": "/api/rules_python/python/runtime_env_toolchains/index.html",
83-
"api/python/pip": "/api/rules_python/python/pip.html",
84-
"api/python/config_settings/index": "/api/rules_python/python/config_settings/index.html",
85-
"api/python/packaging": "/api/rules_python/python/packaging.html",
86-
"api/python/py_runtime": "/api/rules_python/python/py_runtime.html",
87-
"api/sphinxdocs/sphinx": "/api/sphinxdocs/sphinxdocs/sphinx.html",
88-
"api/sphinxdocs/sphinx_stardoc": "/api/sphinxdocs/sphinxdocs/sphinx_stardoc.html",
89-
"api/sphinxdocs/readthedocs": "/api/sphinxdocs/sphinxdocs/readthedocs.html",
90-
"api/sphinxdocs/index": "sphinxdocs/index.html",
91-
"api/sphinxdocs/private/sphinx_docs_library": "/api/sphinxdocs/sphinxdocs/private/sphinx_docs_library.html",
72+
"api/python/private/common/py_library_rule_bazel": "/api/rules_python/python/private/py_library_rule.html", # noqa: E501
73+
"api/python/private/common/py_test_rule_bazel": "/api/rules_python/python/private/py_test_rule_bazel.html", # noqa: E501
74+
"api/python/private/common/py_binary_rule_bazel": "/api/rules_python/python/private/py_binary_rule.html", # noqa: E501
75+
"api/python/private/common/py_runtime_rule": "/api/rules_python/python/private/py_runtime_rule.html", # noqa: E501
76+
"api/python/extensions/pip": "/api/rules_python/python/extensions/pip.html", # noqa: E501
77+
"api/python/extensions/python": "/api/rules_python/python/extensions/python.html", # noqa: E501
78+
"api/python/entry_points/py_console_script_binary": "/api/rules_python/python/entry_points/py_console_script_binary.html", # noqa: E501
79+
"api/python/cc/py_cc_toolchain_info": "/api/rules_python/python/cc/py_cc_toolchain_info.html", # noqa: E501
80+
"api/python/cc/index": "/api/rules_python/python/cc/index.html", # noqa: E501
81+
"api/python/py_cc_link_params_info": "/api/rules_python/python/py_cc_link_params_info.html", # noqa: E501
82+
"api/python/runtime_env_toolchains/index": "/api/rules_python/python/runtime_env_toolchains/index.html", # noqa: E501
83+
"api/python/pip": "/api/rules_python/python/pip.html", # noqa: E501
84+
"api/python/config_settings/index": "/api/rules_python/python/config_settings/index.html", # noqa: E501
85+
"api/python/packaging": "/api/rules_python/python/packaging.html", # noqa: E501
86+
"api/python/py_runtime": "/api/rules_python/python/py_runtime.html", # noqa: E501
87+
"api/sphinxdocs/sphinx": "/api/sphinxdocs/sphinxdocs/sphinx.html", # noqa: E501
88+
"api/sphinxdocs/sphinx_stardoc": "/api/sphinxdocs/sphinxdocs/sphinx_stardoc.html", # noqa: E501
89+
"api/sphinxdocs/readthedocs": "/api/sphinxdocs/sphinxdocs/readthedocs.html", # noqa: E501
90+
"api/sphinxdocs/index": "sphinxdocs/index.html", # noqa: E501
91+
"api/sphinxdocs/private/sphinx_docs_library": "/api/sphinxdocs/sphinxdocs/private/sphinx_docs_library.html", # noqa: E501
9292
"api/sphinxdocs/sphinx_docs_library": "/api/sphinxdocs/sphinxdocs/sphinx_docs_library.html",
9393
"api/sphinxdocs/inventories/index": "/api/sphinxdocs/sphinxdocs/inventories/index.html",
9494
"pip.html": "pypi/index.html",
@@ -133,11 +133,11 @@
133133
# --- Extlinks configuration
134134
extlinks = {
135135
"gh-issue": (
136-
f"https://github.com/bazel-contrib/rules_python/issues/%s",
136+
"https://github.com/bazel-contrib/rules_python/issues/%s",
137137
"#%s issue",
138138
),
139-
"gh-path": (f"https://github.com/bazel-contrib/rules_python/tree/main/%s", "%s"),
140-
"gh-pr": (f"https://github.com/bazel-contrib/rules_python/pull/%s", "#%s PR"),
139+
"gh-path": ("https://github.com/bazel-contrib/rules_python/tree/main/%s", "%s"),
140+
"gh-pr": ("https://github.com/bazel-contrib/rules_python/pull/%s", "#%s PR"),
141141
}
142142

143143
# --- MyST configuration

examples/bzlmod/entry_points/tests/pylint_deps_test.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import os
1616
import pathlib
1717
import subprocess
18-
import tempfile
1918
import unittest
2019

2120
from python.runfiles import runfiles
@@ -29,17 +28,17 @@ def __init__(self, *args, **kwargs):
2928

3029
def test_pylint_entry_point(self):
3130
rlocation_path = os.environ.get("ENTRY_POINT")
32-
assert (
33-
rlocation_path is not None
34-
), "expected 'ENTRY_POINT' env variable to be set to rlocation of the tool"
31+
assert rlocation_path is not None, (
32+
"expected 'ENTRY_POINT' env variable to be set to rlocation of the tool"
33+
)
3534

3635
entry_point = pathlib.Path(runfiles.Create().Rlocation(rlocation_path))
3736
self.assertTrue(entry_point.exists(), f"'{entry_point}' does not exist")
3837

3938
# Let's run the entrypoint and check the tool version.
4039
#
41-
# NOTE @aignas 2023-08-24: the Windows python launcher with Python 3.9 and bazel 6 is not happy if we start
42-
# passing extra files via `subprocess.run` and it starts to fail with an error that the file which is the
40+
# NOTE @aignas 2023-08-24: the Windows python launcher with Python 3.9 and bazel 6 is not happy if we start # noqa: E501
41+
# passing extra files via `subprocess.run` and it starts to fail with an error that the file which is the # noqa: E501
4342
# entry_point cannot be found. However, just calling `--version` seems to be fine.
4443
proc = subprocess.run(
4544
[str(entry_point), "--version"],
@@ -51,20 +50,20 @@ def test_pylint_entry_point(self):
5150
"",
5251
proc.stderr.decode("utf-8").strip(),
5352
)
54-
self.assertRegex(proc.stdout.decode("utf-8").strip(), "^pylint 2\.15\.9")
53+
self.assertRegex(proc.stdout.decode("utf-8").strip(), r"^pylint 2\.15\.9")
5554

5655
def test_pylint_report_has_expected_warnings(self):
5756
rlocation_path = os.environ.get("PYLINT_REPORT")
58-
assert (
59-
rlocation_path is not None
60-
), "expected 'PYLINT_REPORT' env variable to be set to rlocation of the report"
57+
assert rlocation_path is not None, (
58+
"expected 'PYLINT_REPORT' env variable to be set to rlocation of the report"
59+
)
6160

6261
pylint_report = pathlib.Path(runfiles.Create().Rlocation(rlocation_path))
6362
self.assertTrue(pylint_report.exists(), f"'{pylint_report}' does not exist")
6463

6564
self.assertRegex(
6665
pylint_report.read_text().strip(),
67-
"W8201: Logging should be used instead of the print\(\) function\. \(print-function\)",
66+
r"W8201: Logging should be used instead of the print\(\) function\. \(print-function\)",
6867
)
6968

7069

examples/bzlmod/entry_points/tests/pylint_test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ def __init__(self, *args, **kwargs):
2828

2929
def test_pylint_entry_point(self):
3030
rlocation_path = os.environ.get("ENTRY_POINT")
31-
assert (
32-
rlocation_path is not None
33-
), "expected 'ENTRY_POINT' env variable to be set to rlocation of the tool"
31+
assert rlocation_path is not None, (
32+
"expected 'ENTRY_POINT' env variable to be set to rlocation of the tool"
33+
)
3434

3535
entry_point = pathlib.Path(runfiles.Create().Rlocation(rlocation_path))
3636
self.assertTrue(entry_point.exists(), f"'{entry_point}' does not exist")
3737

3838
# Let's run the entrypoint and check the tool version.
3939
#
40-
# NOTE @aignas 2023-08-24: the Windows python launcher with Python 3.9 and bazel 6 is not happy if we start
41-
# passing extra files via `subprocess.run` and it starts to fail with an error that the file which is the
40+
# NOTE @aignas 2023-08-24: the Windows python launcher with Python 3.9 and bazel 6 is not happy if we start # noqa: E501
41+
# passing extra files via `subprocess.run` and it starts to fail with an error that the file which is the # noqa: E501
4242
# entry_point cannot be found. However, just calling `--version` seems to be fine.
4343
proc = subprocess.run(
4444
[str(entry_point), "--version"],
@@ -50,7 +50,7 @@ def test_pylint_entry_point(self):
5050
"",
5151
proc.stderr.decode("utf-8").strip(),
5252
)
53-
self.assertRegex(proc.stdout.decode("utf-8").strip(), "^pylint 2\.15\.9")
53+
self.assertRegex(proc.stdout.decode("utf-8").strip(), r"^pylint 2\.15\.9")
5454

5555

5656
if __name__ == "__main__":

examples/bzlmod/entry_points/tests/yamllint_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ def __init__(self, *args, **kwargs):
2828

2929
def test_yamllint_entry_point(self):
3030
rlocation_path = os.environ.get("ENTRY_POINT")
31-
assert (
32-
rlocation_path is not None
33-
), "expected 'ENTRY_POINT' env variable to be set to rlocation of the tool"
31+
assert rlocation_path is not None, (
32+
"expected 'ENTRY_POINT' env variable to be set to rlocation of the tool"
33+
)
3434

3535
entry_point = pathlib.Path(runfiles.Create().Rlocation(rlocation_path))
3636
self.assertTrue(entry_point.exists(), f"'{entry_point}' does not exist")
3737

3838
# Let's run the entrypoint and check the tool version.
3939
#
40-
# NOTE @aignas 2023-08-24: the Windows python launcher with Python 3.9 and bazel 6 is not happy if we start
41-
# passing extra files via `subprocess.run` and it starts to fail with an error that the file which is the
40+
# NOTE @aignas 2023-08-24: the Windows python launcher with Python 3.9 and bazel 6 is not happy if we start # noqa: E501
41+
# passing extra files via `subprocess.run` and it starts to fail with an error that the file which is the # noqa: E501
4242
# entry_point cannot be found. However, just calling `--version` seems to be fine.
4343
proc = subprocess.run(
4444
[str(entry_point), "--version"],

examples/bzlmod/runfiles/runfiles_test.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ def testRunfileWithRlocationpath(self):
3737
self.assertEqual(f.read().strip(), "Hello, example_bzlmod!")
3838

3939
def testRunfileInOtherModuleWithOurRepoMapping(self):
40-
data_path = runfiles.Create().Rlocation(
41-
"our_other_module/other_module/pkg/data/data.txt"
42-
)
40+
data_path = runfiles.Create().Rlocation("our_other_module/other_module/pkg/data/data.txt")
4341
with open(data_path, "rt", encoding="utf-8", newline="\n") as f:
4442
self.assertEqual(f.read().strip(), "Hello, other_module!")
4543

0 commit comments

Comments
 (0)