Skip to content

Commit a9de4d5

Browse files
authored
chore: use ruff to lint and format files and apply fixes (#3779)
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 bc8a62b commit a9de4d5

73 files changed

Lines changed: 321 additions & 268 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: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ def fetch_buildkite_data(build_url):
8585
elif isinstance(jobs_data, dict) and "records" in jobs_data:
8686
data["jobs"] = jobs_data["records"]
8787
except Exception as e:
88-
print(f"Warning: Could not fetch detailed jobs from {jobs_url}: {e}", file=sys.stderr)
88+
print(
89+
f"Warning: Could not fetch detailed jobs from {jobs_url}: {e}",
90+
file=sys.stderr,
91+
)
8992

9093
return data
9194

@@ -165,15 +168,18 @@ def main():
165168

166169
build_state = data.get("state", "Unknown")
167170
print(f"Build State: {build_state}")
168-
171+
169172
jobs = data.get("jobs", [])
170173
jobs_count = data.get("statistics", {}).get("jobs_count", 0)
171-
174+
172175
print(f"Total jobs reported: {jobs_count}")
173176
print(f"Jobs found in data: {len(jobs)}")
174-
177+
175178
if jobs_count != len(jobs):
176-
print(f"WARNING: Reported job count ({jobs_count}) does not match jobs found ({len(jobs)}).", file=sys.stderr)
179+
print(
180+
f"WARNING: Reported job count ({jobs_count}) does not match jobs found ({len(jobs)}).",
181+
file=sys.stderr,
182+
)
177183

178184
print("-" * 40)
179185

.github/workflows/ci.yaml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: CI
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+
permissions:
17+
contents: read
18+
19+
jobs:
20+
mypy:
21+
runs-on: ubuntu-latest
22+
steps:
23+
# Checkout the code
24+
- uses: actions/checkout@v6
25+
- uses: jpetrucciani/mypy-check@master
26+
with:
27+
path: 'python/runfiles'
28+
- uses: jpetrucciani/mypy-check@master
29+
with:
30+
path: 'tests/runfiles'
31+
ruff:
32+
runs-on: ubuntu-latest
33+
steps:
34+
- uses: actions/checkout@v6
35+
- uses: astral-sh/ruff-action@v4.0.0
36+
with:
37+
# Keep in sync with .pre-commit-config.yaml
38+
version: 0.15.14
39+
args: check --extend-exclude testdata
40+
- uses: astral-sh/ruff-action@v4.0.0
41+
with:
42+
version: 0.15.14
43+
args: format --check --exclude testdata

.github/workflows/mypy.yaml

Lines changed: 0 additions & 27 deletions
This file was deleted.

.pre-commit-config.yaml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,15 @@ 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+
# Keep in sync with .github/workflows/ruff.yaml
34+
rev: v0.15.14
3435
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
36+
- id: ruff-check
37+
args: [--fix]
38+
exclude: testdata
39+
- id: ruff-format
40+
exclude: testdata
4441
- repo: local
4542
hooks:
4643
- id: update-deleted-packages

docs/conf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 8 additions & 9 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,9 +28,9 @@ 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")
@@ -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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ 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")
@@ -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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ 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")

examples/bzlmod/test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
import os
16-
import pathlib
1716
import re
1817
import sys
1918
import unittest
@@ -59,7 +58,7 @@ def test_coverage_sys_path(self):
5958
f"sys.path has {len(sys.path)} items:\n {all_paths}",
6059
)
6160

62-
first_item, last_item = sys.path[0], sys.path[-1]
61+
first_item, _ = sys.path[0], sys.path[-1]
6362
self.assertFalse(
6463
first_item.endswith("coverage"),
6564
f"Expected the first item in sys.path '{first_item}' to not be related to coverage",

examples/bzlmod/tests/my_lib_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
1615
import sys
1716

1817
import libs.my_lib as my_lib

0 commit comments

Comments
 (0)