Skip to content

Commit 19345a7

Browse files
authored
Python lint: Use ruff instead of flake8. NFC (#23085)
https://docs.astral.sh/ruff is an extremely fast Python linter and code formatter, written in Rust. With over 800 linting rules, ruff can be used to replace [Flake8](https://pypi.org/project/flake8/) (plus dozens of plugins), [Black](https://github.com/psf/black), [isort](https://pypi.org/project/isort/), [pydocstyle](https://pypi.org/project/pydocstyle/), [pyupgrade](https://pypi.org/project/pyupgrade/), [autoflake](https://pypi.org/project/autoflake/), and more, all while executing tens or hundreds of times faster than any individual tool.
1 parent c08204e commit 19345a7

14 files changed

+127
-45
lines changed

.circleci/config.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -419,12 +419,12 @@ jobs:
419419
- run: make -C site text
420420
- run: tools/maint/check_emcc_help_text.py
421421
- run: make -C site html
422-
flake8:
422+
ruff:
423423
executor: bionic
424424
steps:
425425
- checkout
426426
- pip-install
427-
- run: python3 -m flake8 --show-source --statistics
427+
- run: ruff check
428428
mypy:
429429
executor: bionic
430430
steps:
@@ -970,7 +970,7 @@ jobs:
970970
workflows:
971971
build-test:
972972
jobs:
973-
- flake8
973+
- ruff
974974
- mypy
975975
- eslint
976976
- build-docs

.flake8

-19
This file was deleted.

docs/process.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ pre-processor. See [`.clang-format`][clang-format] for more details.
5555
### Python Code
5656

5757
We generally follow the pep8 standard with the major exception that we use 2
58-
spaces for indentation. `flake8` is run on all PRs to ensure that python code
59-
conforms to this style. See [`.flake8`][flake8] for more details.
58+
spaces for indentation. `ruff` is run on all PRs to ensure that Python code
59+
conforms to this style. See [`pyproject.toml`][pyproject.toml] for more details.
6060

6161
#### Static Type Checking
6262

@@ -304,7 +304,7 @@ To update our libraries to a newer musl release:
304304
[emsdk_tags]: https://github.com/emscripten-core/emsdk/tags
305305
[emscripten_tags]: https://github.com/emscripten-core/emscripten/tags
306306
[clang-format]: https://github.com/emscripten-core/emscripten/blob/main/.clang-format
307-
[flake8]: https://github.com/emscripten-core/emscripten/blob/main/.flake8
307+
[pyproject.toml]: https://github.com/emscripten-core/emscripten/blob/main/pyproject.toml
308308
[mypy]: https://github.com/emscripten-core/emscripten/blob/main/.mypy
309309
[update_docs]: https://github.com/emscripten-core/emscripten/blob/main/tools/maint/update_docs.py
310310
[llvm_repo]: https://github.com/llvm/llvm-project

em-config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def main():
2424
not re.match(r"^[\w\W_][\w\W_\d]*$", sys.argv[1]) or \
2525
not hasattr(config, sys.argv[1]):
2626
print('Usage: em-config VAR_NAME', file=sys.stderr)
27-
exit(1)
27+
sys.exit(1)
2828

2929
print(getattr(config, sys.argv[1]))
3030
return 0

emcc.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ def get_clang_command_asm():
10231023
if state.mode == Mode.PCH:
10241024
inputs = [i[1] for i in input_files]
10251025
for header in inputs:
1026-
if not shared.suffix(header) in HEADER_ENDINGS:
1026+
if shared.suffix(header) not in HEADER_ENDINGS:
10271027
exit_with_error(f'cannot mix precompiled headers with non-header inputs: {inputs} : {header}')
10281028
cmd = get_clang_command() + inputs
10291029
if options.output_file:
@@ -1127,7 +1127,15 @@ def version_string():
11271127
return f'emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) {utils.EMSCRIPTEN_VERSION}{revision_suffix}'
11281128

11291129

1130-
def parse_args(newargs):
1130+
def parse_args(newargs): # noqa: C901, PLR0912, PLR0915
1131+
"""Future modifications should consider refactoring to reduce complexity.
1132+
1133+
* The McCabe cyclomatiic complexity is currently 117 vs 10 recommended.
1134+
* There are currently 115 branches vs 12 recommended.
1135+
* There are currently 302 statements vs 50 recommended.
1136+
1137+
To revalidate these numbers, run `ruff check --select=C901,PLR091`.
1138+
"""
11311139
options = EmccOptions()
11321140
settings_changes = []
11331141
user_js_defines = []

emrun.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1583,7 +1583,15 @@ def parse_args(args):
15831583
return parser.parse_args(args)
15841584

15851585

1586-
def run(args):
1586+
def run(args): # noqa: C901, PLR0912, PLR0915
1587+
"""Future modifications should consider refactoring to reduce complexity.
1588+
1589+
* The McCabe cyclomatiic complexity is currently 74 vs 10 recommended.
1590+
* There are currently 86 branches vs 12 recommended.
1591+
* There are currently 202 statements vs 50 recommended.
1592+
1593+
To revalidate these numbers, run `ruff check --select=C901,PLR091`.
1594+
"""
15871595
global browser_process, browser_exe, processname_killed_atexit, emrun_options, emrun_not_enabled_nag_printed
15881596

15891597
options = emrun_options = parse_args(args)

emsymbolizer.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,8 @@ def parse(self, filename):
121121
self.version = source_map_json['version']
122122
self.sources = source_map_json['sources']
123123

124-
vlq_map = {}
125124
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
126-
for i, c in enumerate(chars):
127-
vlq_map[c] = i
125+
vlq_map = {c: i for i, c in enumerate(chars)}
128126

129127
def decodeVLQ(string):
130128
result = []

pyproject.toml

+67
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,70 @@
1+
[tool.ruff]
2+
exclude = [
3+
"./cache/",
4+
"./node_modules/",
5+
"./site/source/_themes/",
6+
"./site/source/conf.py",
7+
"./system/lib/",
8+
"./test/third_party/",
9+
"./third_party/",
10+
"./tools/filelock.py",
11+
"./tools/scons/",
12+
".git",
13+
]
14+
15+
lint.select = [
16+
"ARG",
17+
"ASYNC",
18+
"B",
19+
"C90",
20+
"E",
21+
"F",
22+
"PERF",
23+
"PL",
24+
"W",
25+
"YTT",
26+
]
27+
28+
lint.ignore = [
29+
"ARG001",
30+
"ARG002",
31+
"ARG005",
32+
"B006",
33+
"B011",
34+
"B018",
35+
"B023",
36+
"B026",
37+
"B904",
38+
"E402",
39+
"E501",
40+
"E721",
41+
"E741",
42+
"PERF203",
43+
"PERF401",
44+
"PLR1704",
45+
"PLR1714",
46+
"PLR5501",
47+
"PLW0602",
48+
"PLW0603",
49+
"PLW1510",
50+
"PLW2901",
51+
]
52+
53+
lint.per-file-ignores."emrun.py" = [ "PLE0704" ]
54+
55+
lint.mccabe.max-complexity = 48 # Recommended: 10
56+
57+
lint.pylint.allow-magic-value-types = [
58+
"bytes",
59+
"float",
60+
"int",
61+
"str",
62+
]
63+
lint.pylint.max-args = 15 # Recommended: 5
64+
lint.pylint.max-branches = 49 # Recommended: 12
65+
lint.pylint.max-returns = 16 # Recommended: 6
66+
lint.pylint.max-statements = 142 # Recommended: 50
67+
168
[tool.coverage.run]
269
source = [ "." ]
370
omit = [

requirements-dev.txt

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
# TODO(sbc): switch to using Pipenv since it seems like that way to go
22
# these day managing python deps.
3-
# These requirements are only needed for developers who want to run flake8 on
3+
# These requirements are only needed for developers who want to run ruff on
44
# the codebase and generate docs using Sphinx, not for users of emscripten.
55
# Install with `pip3 install -r requirements-dev.txt`
66

7-
flake8==5.0.4
8-
flake8-bugbear==22.9.23
9-
flake8-unused-arguments==0.0.11
107
coverage[toml]==5.5
118
mypy==0.971
9+
ruff==0.8.2
1210
types-requests==2.27.14
1311
unittest-xml-reporting==3.1.0
1412

test/benchmark/benchmark_sse.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ def format_comparison(a, b):
134134
total_time_html_scalar = 0
135135
total_time_html_simd = 0
136136

137-
for chart_name in charts_native.keys():
137+
for chart_name, chart_native_results in charts_native.items():
138138
# Extract data for each chart.
139139
categories = []
140140
nativeScalarResults = []
141141
nativeSimdResults = []
142142
htmlScalarResults = []
143143
htmlSimdResults = []
144-
native_results = charts_native[chart_name]
144+
native_results = chart_native_results
145145
wasm_results = charts_html[chart_name]
146146
textual_results_native = '<p>'
147147
textual_results_html = '<p>'

test/runner.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def check_js_engines():
106106
working_engines = [e for e in config.JS_ENGINES if jsrun.check_engine(e)]
107107
if len(working_engines) < len(config.JS_ENGINES):
108108
print('Not all the JS engines in JS_ENGINES appears to work.')
109-
exit(1)
109+
sys.exit(1)
110110

111111
if common.EMTEST_ALL_ENGINES:
112112
print('(using ALL js engines)')
@@ -311,11 +311,9 @@ def load_test_suites(args, modules, start_at, repeat):
311311
def flattened_tests(loaded_tests):
312312
tests = []
313313
for subsuite in loaded_tests:
314-
for test in subsuite:
315-
tests.append(test)
314+
tests.extend(subsuite)
316315
return tests
317316

318-
319317
def suite_for_module(module, tests):
320318
suite_supported = module.__name__ in ('test_core', 'test_other', 'test_posixtest')
321319
if not common.EMTEST_SAVE_DIR and not shared.DEBUG:

tools/file_packager.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,15 @@ def generate_object_file(data_files):
354354
shared.check_call(cmd)
355355

356356

357-
def main():
357+
def main(): # noqa: C901, PLR0912, PLR0915
358+
"""Future modifications should consider refactoring to reduce complexity.
359+
360+
* The McCabe cyclomatiic complexity is currently 60 vs 10 recommended.
361+
* There are currently 63 branches vs 12 recommended.
362+
* There are currently 151 statements vs 50 recommended.
363+
364+
To revalidate these numbers, run `ruff check --select=C901,PLR091`.
365+
"""
358366
if len(sys.argv) == 1:
359367
err('''Usage: file_packager TARGET [--preload A [B..]] [--embed C [D..]] [--exclude E [F..]]] [--js-output=OUTPUT.js] [--no-force] [--use-preload-cache] [--indexedDB-name=EM_PRELOAD_CACHE] [--separate-metadata] [--lz4] [--use-preload-plugins] [--no-node]
360368
See the source for more details.''')

tools/link.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,15 @@ def check_browser_versions():
631631

632632

633633
@ToolchainProfiler.profile_block('linker_setup')
634-
def phase_linker_setup(options, state, newargs):
634+
def phase_linker_setup(options, state, newargs): # noqa: C901, PLR0912, PLR0915
635+
"""Future modifications should consider refactoring to reduce complexity.
636+
637+
* The McCabe cyclomatiic complexity is currently 251 vs 10 recommended.
638+
* There are currently 262 branches vs 12 recommended.
639+
* There are currently 578 statements vs 50 recommended.
640+
641+
To revalidate these numbers, run `ruff check --select=C901,PLR091`.
642+
"""
635643
system_libpath = '-L' + str(cache.get_lib_dir(absolute=True))
636644
state.append_link_flag(system_libpath)
637645

tools/webidl_binder.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,18 @@ def type_to_cdec(raw):
386386
return ret + '*'
387387

388388

389-
def render_function(class_name, func_name, sigs, return_type, non_pointer,
389+
def render_function(class_name, func_name, sigs, return_type, non_pointer, # noqa: C901, PLR0912, PLR0915
390390
copy, operator, constructor, is_static, func_scope,
391391
call_content=None, const=False, array_attribute=False,
392392
bind_to=None):
393+
"""Future modifications should consider refactoring to reduce complexity.
394+
395+
* The McCabe cyclomatiic complexity is currently 67 vs 10 recommended.
396+
* There are currently 79 branches vs 12 recommended.
397+
* There are currently 195 statements vs 50 recommended.
398+
399+
To revalidate these numbers, run `ruff check --select=C901,PLR091`.
400+
"""
393401
legacy_mode = CHECKS not in ['ALL', 'FAST']
394402
all_checks = CHECKS == 'ALL'
395403

0 commit comments

Comments
 (0)