Skip to content

Commit 41b88d8

Browse files
committed
[CI] Add ruff python checker. NFC
1 parent ef29ce2 commit 41b88d8

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

+235
-206
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: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,19 @@
1515
# limitations under the License.
1616

1717
import glob
18+
import io
1819
import os
20+
import queue
1921
import subprocess
2022
import sys
23+
import threading
2124
import unittest
2225
from collections import OrderedDict
2326
from concurrent.futures import ThreadPoolExecutor
24-
from pathlib import Path
25-
import queue
26-
import io
27-
import threading
2827
from functools import partial
28+
from pathlib import Path
2929

30-
from scripts.test import binaryenjs
31-
from scripts.test import lld
32-
from scripts.test import shared
33-
from scripts.test import support
34-
from scripts.test import wasm2js
35-
from scripts.test import wasm_opt
30+
from scripts.test import binaryenjs, lld, shared, support, wasm2js, wasm_opt
3631

3732

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

4742

@@ -55,16 +50,16 @@ def run_version_tests():
5550
not any(f.endswith(s) for s in not_executable_suffix) and
5651
any(os.path.basename(f).startswith(s) for s in executable_prefix)]
5752
executables = sorted(executables)
58-
assert len(executables)
53+
assert executables
5954

6055
changelog_version = get_changelog_version()
6156
for e in executables:
62-
print('.. %s --version' % e)
57+
print(f'.. {e} --version')
6358
proc = subprocess.run([e, '--version'], capture_output=True, text=True)
64-
assert len(proc.stderr) == 0, 'Expected no stderr, got:\n%s' % proc.stderr
59+
assert len(proc.stderr) == 0, f'Expected no stderr, got:\n{proc.stderr}'
6560
out = proc.stdout
66-
assert os.path.basename(e).replace('.exe', '') in out, 'Expected version to contain program name, got:\n%s' % out
67-
assert len(out.strip().splitlines()) == 1, 'Expected only version info, got:\n%s' % out
61+
assert os.path.basename(e).replace('.exe', '') in out, f'Expected version to contain program name, got:\n{out}'
62+
assert len(out.strip().splitlines()) == 1, f'Expected only version info, got:\n{out}'
6863
parts = out.split()
6964
assert parts[1] == 'version'
7065
version = int(parts[2])
@@ -158,7 +153,8 @@ def run_wasm_reduce_tests():
158153
print('..', os.path.basename(t))
159154
# convert to wasm
160155
support.run_command(shared.WASM_AS + [t, '-o', 'a.wasm', '-all'])
161-
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'])
156+
cmd = shared.WASM_OPT[0]
157+
support.run_command(shared.WASM_REDUCE + ['a.wasm', f'--command={cmd} b.wasm --fuzz-exec -all ', '-t', 'b.wasm', '-w', 'c.wasm', '--timeout=4'])
162158
expected = t + '.txt'
163159
support.run_command(shared.WASM_DIS + ['c.wasm', '-o', 'a.wat'])
164160
with open('a.wat') as seen:
@@ -171,14 +167,15 @@ def run_wasm_reduce_tests():
171167
# TODO: re-enable multivalue once it is better optimized
172168
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'])
173169
before = os.stat('a.wasm').st_size
174-
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'])
170+
cmd = shared.WASM_OPT[0]
171+
support.run_command(shared.WASM_REDUCE + ['a.wasm', f'--command={cmd} b.wasm --fuzz-exec --detect-features', '-t', 'b.wasm', '-w', 'c.wasm'])
175172
after = os.stat('c.wasm').st_size
176173
# This number is a custom threshold to check if we have shrunk the
177174
# output sufficiently
178175
assert after < 0.85 * before, [before, after]
179176

180177

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

189186

190-
def run_opt_test(wast, stdout=None, stderr=None):
187+
def run_opt_test(wast, stdout=None, _stderr=None):
191188
# check optimization validation
192189
cmd = shared.WASM_OPT + [wast, '-O', '-all', '-q']
193190
support.run_command(cmd, stdout=stdout)
@@ -242,7 +239,7 @@ def run_one_spec_test(wast: Path, stdout=None, stderr=None):
242239
support.write_wast(split_name, module)
243240
run_opt_test(split_name, stdout=stdout, stderr=stderr) # also that our optimizer doesn't break on it
244241

245-
result_wast_file = shared.binary_format_check(split_name, verify_final_result=False, base_name=base_name, stdout=stdout, stderr=stderr)
242+
result_wast_file = shared.binary_format_check(split_name, verify_final_result=False, base_name=base_name, stdout=stdout)
246243
with open(result_wast_file) as f:
247244
result_wast = f.read()
248245
# add the asserts, and verify that the test still passes
@@ -437,7 +434,7 @@ def main():
437434

438435
for r in shared.requested:
439436
if r not in all_suites:
440-
print('invalid test suite: %s (see --list-suites)\n' % r)
437+
print(f'invalid test suite: {r} (see --list-suites)\n')
441438
return 1
442439

443440
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)