Skip to content

Commit bc55110

Browse files
authored
[CI] Add ruff python checker. NFC (#8131)
1 parent ccd2c82 commit bc55110

Some content is hidden

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

41 files changed

+238
-209
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jobs:
3939
sudo ./llvm.sh ${LLVM_VERSION}
4040
sudo apt-get install clang-format clang-format-${LLVM_VERSION} clang-tidy-${LLVM_VERSION}
4141
- run: flake8
42+
- run: ruff check
4243
- run: ./scripts/clang-format-diff.sh
4344
- name: clang-tidy
4445
run: |

.ruff.toml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
target-version = "py310"
2+
3+
exclude = [
4+
'third_party',
5+
'test/lit/lit.cfg.py',
6+
'test/spec/testsuite',
7+
]
8+
9+
[lint]
10+
select = [
11+
"ARG",
12+
"ASYNC",
13+
"B",
14+
"C4",
15+
"C90",
16+
"COM",
17+
"E",
18+
"F",
19+
"I",
20+
"PERF",
21+
"PIE",
22+
"PL",
23+
"UP",
24+
"W",
25+
"YTT",
26+
]
27+
28+
ignore = [
29+
"C901", # https://docs.astral.sh/ruff/rules/complex-structure/
30+
"B006", # https://docs.astral.sh/ruff/rules/mutable-argument-default/
31+
"B011", # https://docs.astral.sh/ruff/rules/assert-false/
32+
"B023", # https://docs.astral.sh/ruff/rules/function-uses-loop-variable/
33+
"E501", # https://docs.astral.sh/ruff/rules/line-too-long/
34+
"PERF401", # https://docs.astral.sh/ruff/rules/manual-list-comprehension/
35+
"PLR0912", # https://docs.astral.sh/ruff/rules/too-many-branches/
36+
"PLR0913", # https://docs.astral.sh/ruff/rules/too-many-arguments/
37+
"PLR0915", # https://docs.astral.sh/ruff/rules/too-many-statements/
38+
"PLR2004", # https://docs.astral.sh/ruff/rules/magic-value-comparison/
39+
"PLW0603", # https://docs.astral.sh/ruff/rules/global-statement/
40+
"PLW1510", # https://docs.astral.sh/ruff/rules/subprocess-run-without-check/
41+
"PLW2901", # https://docs.astral.sh/ruff/rules/redefined-loop-name/
42+
]

check.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,16 @@
1515
# limitations under the License.
1616

1717
import glob
18+
import io
1819
import os
1920
import subprocess
2021
import sys
2122
import unittest
22-
from multiprocessing.pool import ThreadPool
2323
from collections import OrderedDict
24+
from multiprocessing.pool import ThreadPool
2425
from pathlib import Path
25-
import io
2626

27-
from scripts.test import binaryenjs
28-
from scripts.test import lld
29-
from scripts.test import shared
30-
from scripts.test import support
31-
from scripts.test import wasm2js
32-
from scripts.test import wasm_opt
27+
from scripts.test import binaryenjs, lld, shared, support, wasm2js, wasm_opt
3328

3429

3530
def get_changelog_version():
@@ -38,7 +33,7 @@ def get_changelog_version():
3833
lines = [line for line in lines if len(line.split()) == 1]
3934
lines = [line for line in lines if line.startswith('v')]
4035
version = lines[0][1:]
41-
print("Parsed CHANGELOG.md version: %s" % version)
36+
print(f"Parsed CHANGELOG.md version: {version}")
4237
return int(version)
4338

4439

@@ -52,16 +47,16 @@ def run_version_tests():
5247
not any(f.endswith(s) for s in not_executable_suffix) and
5348
any(os.path.basename(f).startswith(s) for s in executable_prefix)]
5449
executables = sorted(executables)
55-
assert len(executables)
50+
assert executables
5651

5752
changelog_version = get_changelog_version()
5853
for e in executables:
59-
print('.. %s --version' % e)
54+
print(f'.. {e} --version')
6055
proc = subprocess.run([e, '--version'], capture_output=True, text=True)
61-
assert len(proc.stderr) == 0, 'Expected no stderr, got:\n%s' % proc.stderr
56+
assert len(proc.stderr) == 0, f'Expected no stderr, got:\n{proc.stderr}'
6257
out = proc.stdout
63-
assert os.path.basename(e).replace('.exe', '') in out, 'Expected version to contain program name, got:\n%s' % out
64-
assert len(out.strip().splitlines()) == 1, 'Expected only version info, got:\n%s' % out
58+
assert os.path.basename(e).replace('.exe', '') in out, f'Expected version to contain program name, got:\n{out}'
59+
assert len(out.strip().splitlines()) == 1, f'Expected only version info, got:\n{out}'
6560
parts = out.split()
6661
assert parts[1] == 'version'
6762
version = int(parts[2])
@@ -155,7 +150,8 @@ def run_wasm_reduce_tests():
155150
print('..', os.path.basename(t))
156151
# convert to wasm
157152
support.run_command(shared.WASM_AS + [t, '-o', 'a.wasm', '-all'])
158-
support.run_command(shared.WASM_REDUCE + ['a.wasm', '--command=%s b.wasm --fuzz-exec -all ' % shared.WASM_OPT[0], '-t', 'b.wasm', '-w', 'c.wasm', '--timeout=4'])
153+
cmd = shared.WASM_OPT[0]
154+
support.run_command(shared.WASM_REDUCE + ['a.wasm', f'--command={cmd} b.wasm --fuzz-exec -all ', '-t', 'b.wasm', '-w', 'c.wasm', '--timeout=4'])
159155
expected = t + '.txt'
160156
support.run_command(shared.WASM_DIS + ['c.wasm', '-o', 'a.wat'])
161157
with open('a.wat') as seen:
@@ -168,14 +164,15 @@ def run_wasm_reduce_tests():
168164
# TODO: re-enable multivalue once it is better optimized
169165
support.run_command(shared.WASM_OPT + [os.path.join(shared.options.binaryen_test, 'lit/basic/signext.wast'), '-ttf', '-Os', '-o', 'a.wasm', '--detect-features', '--disable-multivalue'])
170166
before = os.stat('a.wasm').st_size
171-
support.run_command(shared.WASM_REDUCE + ['a.wasm', '--command=%s b.wasm --fuzz-exec --detect-features' % shared.WASM_OPT[0], '-t', 'b.wasm', '-w', 'c.wasm'])
167+
cmd = shared.WASM_OPT[0]
168+
support.run_command(shared.WASM_REDUCE + ['a.wasm', f'--command={cmd} b.wasm --fuzz-exec --detect-features', '-t', 'b.wasm', '-w', 'c.wasm'])
172169
after = os.stat('c.wasm').st_size
173170
# This number is a custom threshold to check if we have shrunk the
174171
# output sufficiently
175172
assert after < 0.85 * before, [before, after]
176173

177174

178-
def run_spec_test(wast, stdout=None, stderr=None):
175+
def run_spec_test(wast, stdout=None):
179176
cmd = shared.WASM_SHELL + [wast]
180177
output = support.run_command(cmd, stdout=stdout, stderr=subprocess.PIPE)
181178
# filter out binaryen interpreter logging that the spec suite
@@ -184,7 +181,7 @@ def run_spec_test(wast, stdout=None, stderr=None):
184181
return '\n'.join(filtered) + '\n'
185182

186183

187-
def run_opt_test(wast, stdout=None, stderr=None):
184+
def run_opt_test(wast, stdout=None):
188185
# check optimization validation
189186
cmd = shared.WASM_OPT + [wast, '-O', '-all', '-q']
190187
support.run_command(cmd, stdout=stdout)
@@ -200,7 +197,7 @@ def check_expected(actual, expected, stdout=None):
200197
shared.fail(actual, expected)
201198

202199

203-
def run_one_spec_test(wast: Path, stdout=None, stderr=None):
200+
def run_one_spec_test(wast: Path, stdout=None):
204201
test_name = wast.name
205202

206203
# /path/to/binaryen/test/spec/foo.wast -> test-spec-foo
@@ -215,7 +212,7 @@ def run_one_spec_test(wast: Path, stdout=None, stderr=None):
215212

216213
# some spec tests should fail (actual process failure, not just assert_invalid)
217214
try:
218-
actual = run_spec_test(str(wast), stdout=stdout, stderr=stderr)
215+
actual = run_spec_test(str(wast), stdout=stdout)
219216
except Exception as e:
220217
if ('wasm-validator error' in str(e) or 'error: ' in str(e)) and '.fail.' in test_name:
221218
print('<< test failed as expected >>', file=stdout)
@@ -237,23 +234,23 @@ def run_one_spec_test(wast: Path, stdout=None, stderr=None):
237234
print(f' testing split module {i}', file=stdout)
238235
split_name = base_name + f'_split{i}.wast'
239236
support.write_wast(split_name, module)
240-
run_opt_test(split_name, stdout=stdout, stderr=stderr) # also that our optimizer doesn't break on it
237+
run_opt_test(split_name, stdout=stdout) # also that our optimizer doesn't break on it
241238

242-
result_wast_file = shared.binary_format_check(split_name, verify_final_result=False, base_name=base_name, stdout=stdout, stderr=stderr)
239+
result_wast_file = shared.binary_format_check(split_name, verify_final_result=False, base_name=base_name, stdout=stdout)
243240
with open(result_wast_file) as f:
244241
result_wast = f.read()
245242
# add the asserts, and verify that the test still passes
246243
transformed_spec_file.write(result_wast + '\n' + '\n'.join(asserts))
247244

248245
# compare all the outputs to the expected output
249-
actual = run_spec_test(transformed_path, stdout=stdout, stderr=stderr)
246+
actual = run_spec_test(transformed_path, stdout=stdout)
250247
check_expected(actual, os.path.join(shared.get_test_dir('spec'), 'expected-output', test_name + '.log'), stdout=stdout)
251248

252249

253250
def run_spec_test_with_wrapped_stdout(wast: Path):
254251
out = io.StringIO()
255252
try:
256-
run_one_spec_test(wast, stdout=out, stderr=out)
253+
run_one_spec_test(wast, stdout=out)
257254
except Exception as e:
258255
# Serialize exceptions into the output string buffer
259256
# so they can be reported on the main thread.
@@ -412,7 +409,7 @@ def main():
412409

413410
for r in shared.requested:
414411
if r not in all_suites:
415-
print('invalid test suite: %s (see --list-suites)\n' % r)
412+
print(f'invalid test suite: {r} (see --list-suites)\n')
416413
return 1
417414

418415
if not shared.requested:

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
# Install with `pip3 install -r requirements-dev.txt`
55

66
flake8==7.3.0
7+
ruff==0.14.1
78
filecheck==0.0.22
89
lit==0.11.0.post1

scripts/auto_update_tests.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@
1919
import sys
2020
from collections import OrderedDict
2121

22-
from test import binaryenjs
23-
from test import lld
24-
from test import shared
25-
from test import support
26-
from test import wasm2js
27-
from test import wasm_opt
22+
from test import binaryenjs, lld, shared, support, wasm2js, wasm_opt
2823

2924

3025
def update_example_tests():
@@ -119,7 +114,8 @@ def update_reduce_tests():
119114
print('..', os.path.basename(t))
120115
# convert to wasm
121116
support.run_command(shared.WASM_AS + [t, '-o', 'a.wasm', '-all'])
122-
print(support.run_command(shared.WASM_REDUCE + ['a.wasm', '--command=%s b.wasm --fuzz-exec -all' % shared.WASM_OPT[0], '-t', 'b.wasm', '-w', 'c.wasm']))
117+
cmd = shared.WASM_OPT[0]
118+
print(support.run_command(shared.WASM_REDUCE + ['a.wasm', f'--command={cmd} b.wasm --fuzz-exec -all', '-t', 'b.wasm', '-w', 'c.wasm']))
123119
expected = t + '.txt'
124120
support.run_command(shared.WASM_DIS + ['c.wasm', '-o', expected])
125121

scripts/bundle_clusterfuzz.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@
155155
for i, test in enumerate(all_tests):
156156
if not fuzzing.is_fuzzable(test):
157157
continue
158-
for wast, asserts in support.split_wast(test):
158+
for wast, _asserts in support.split_wast(test):
159159
if not wast:
160160
continue
161161
support.write_wast(temp_wasm, wast)

scripts/clusterfuzz/embed_wasms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
wasm_index = 0
5555

5656

57-
def replace_wasm(text):
57+
def replace_wasm(_text):
5858
global wasm_index
5959
wasm_file = in_wasms[wasm_index]
6060
wasm_index += 1

scripts/clusterfuzz/run.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@
2323
bundle_clusterfuzz.py.
2424
'''
2525

26-
import os
2726
import getopt
2827
import math
28+
import os
2929
import random
3030
import subprocess
3131
import sys
3232

33-
3433
# The V8 flags we put in the "fuzzer flags" files, which tell ClusterFuzz how to
3534
# run V8. By default we apply all staging flags.
3635
FUZZER_FLAGS = '--wasm-staging --experimental-wasm-custom-descriptors'
@@ -141,7 +140,7 @@ def get_wasm_contents(name, output_dir, extra_args=[]):
141140

142141
# wasm-opt may fail to run in rare cases (when the fuzzer emits code it
143142
# detects as invalid). Just try again in such a case.
144-
for attempt in range(0, 100):
143+
for attempt in range(100):
145144
# Generate random data.
146145
random_size = system_random.randint(1, MAX_RANDOM_SIZE)
147146
with open(input_data_file_path, 'wb') as file:
@@ -186,7 +185,7 @@ def get_wasm_contents(name, output_dir, extra_args=[]):
186185
global temp_files
187186
temp_files += [
188187
wasm_file_path,
189-
input_data_file_path
188+
input_data_file_path,
190189
]
191190

192191
# Convert to a string, and wrap into a typed array.
@@ -261,7 +260,7 @@ def get_js_file_contents(i, output_dir):
261260
'build(secondBinary, true)',
262261
]
263262

264-
for i in range(num):
263+
for _ in range(num):
265264
choice = system_random.choice(extra_js_operations)
266265
if choice == 'CALL_EXPORTS':
267266
# The random seed can be any unsigned 32-bit number.

scripts/foreach.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
# limitations under the License.
1616

1717
import os
18-
import sys
1918
import subprocess
19+
import sys
2020

2121
from test import support
2222

@@ -33,7 +33,7 @@ def main():
3333
cmd = sys.argv[3:]
3434
returncode = 0
3535
all_modules = open(infile).read()
36-
for i, (module, asserts) in enumerate(support.split_wast(infile)):
36+
for i, (module, _asserts) in enumerate(support.split_wast(infile)):
3737
tempname = tempfile + '.' + str(i)
3838
with open(tempname, 'w') as temp:
3939
print(module, file=temp)

0 commit comments

Comments
 (0)