Skip to content

Commit fafdb7d

Browse files
author
MarcoFalke
committed
lint: Speed up flake8 checks
Previously they may have taken more than 10 seconds. Now they should finish in less than one second. This also allows to drop one dependency to be installed.
1 parent faf17df commit fafdb7d

File tree

4 files changed

+45
-64
lines changed

4 files changed

+45
-64
lines changed

Diff for: ci/lint/04_install.sh

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ fi
4949

5050
${CI_RETRY_EXE} pip3 install \
5151
codespell==2.2.6 \
52-
flake8==6.1.0 \
5352
lief==0.13.2 \
5453
mypy==1.4.1 \
5554
pyzmq==25.1.0 \

Diff for: test/lint/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ or `--help`:
4545

4646
| Lint test | Dependency |
4747
|-----------|:----------:|
48-
| [`lint-python.py`](/test/lint/lint-python.py) | [flake8](https://github.com/PyCQA/flake8)
4948
| [`lint-python.py`](/test/lint/lint-python.py) | [lief](https://github.com/lief-project/LIEF)
5049
| [`lint-python.py`](/test/lint/lint-python.py) | [mypy](https://github.com/python/mypy)
5150
| [`lint-python.py`](/test/lint/lint-python.py) | [pyzmq](https://github.com/zeromq/pyzmq)

Diff for: test/lint/lint-python.py

+2-57
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,25 @@
55
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
66

77
"""
8-
Check for specified flake8 and mypy warnings in python files.
8+
Check for specified mypy warnings in python files.
99
"""
1010

1111
import os
1212
from pathlib import Path
1313
import subprocess
14-
import sys
1514

1615
from importlib.metadata import metadata, PackageNotFoundError
1716

1817
# Customize mypy cache dir via environment variable
1918
cache_dir = Path(__file__).parent.parent / ".mypy_cache"
2019
os.environ["MYPY_CACHE_DIR"] = str(cache_dir)
2120

22-
DEPS = ['flake8', 'lief', 'mypy', 'pyzmq']
23-
24-
# All .py files, except those in src/ (to exclude subtrees there)
25-
FLAKE_FILES_ARGS = ['git', 'ls-files', '*.py', ':!:src/*.py']
21+
DEPS = ['lief', 'mypy', 'pyzmq']
2622

2723
# Only .py files in test/functional and contrib/devtools have type annotations
2824
# enforced.
2925
MYPY_FILES_ARGS = ['git', 'ls-files', 'test/functional/*.py', 'contrib/devtools/*.py']
3026

31-
ENABLED = (
32-
'E101,' # indentation contains mixed spaces and tabs
33-
'E401,' # multiple imports on one line
34-
'E402,' # module level import not at top of file
35-
'E701,' # multiple statements on one line (colon)
36-
'E702,' # multiple statements on one line (semicolon)
37-
'E703,' # statement ends with a semicolon
38-
'E711,' # comparison to None should be 'if cond is None:'
39-
'E714,' # test for object identity should be "is not"
40-
'E721,' # do not compare types, use "isinstance()"
41-
'E722,' # do not use bare 'except'
42-
'E742,' # do not define classes named "l", "O", or "I"
43-
'E743,' # do not define functions named "l", "O", or "I"
44-
'F401,' # module imported but unused
45-
'F402,' # import module from line N shadowed by loop variable
46-
'F403,' # 'from foo_module import *' used; unable to detect undefined names
47-
'F404,' # future import(s) name after other statements
48-
'F405,' # foo_function may be undefined, or defined from star imports: bar_module
49-
'F406,' # "from module import *" only allowed at module level
50-
'F407,' # an undefined __future__ feature name was imported
51-
'F601,' # dictionary key name repeated with different values
52-
'F602,' # dictionary key variable name repeated with different values
53-
'F621,' # too many expressions in an assignment with star-unpacking
54-
'F631,' # assertion test is a tuple, which are always True
55-
'F632,' # use ==/!= to compare str, bytes, and int literals
56-
'F811,' # redefinition of unused name from line N
57-
'F821,' # undefined name 'Foo'
58-
'F822,' # undefined name name in __all__
59-
'F823,' # local variable name … referenced before assignment
60-
'F841,' # local variable 'foo' is assigned to but never used
61-
'W191,' # indentation contains tabs
62-
'W291,' # trailing whitespace
63-
'W292,' # no newline at end of file
64-
'W293,' # blank line contains whitespace
65-
'W605,' # invalid escape sequence "x"
66-
)
67-
6827

6928
def check_dependencies():
7029
for dep in DEPS:
@@ -78,20 +37,6 @@ def check_dependencies():
7837
def main():
7938
check_dependencies()
8039

81-
if len(sys.argv) > 1:
82-
flake8_files = sys.argv[1:]
83-
else:
84-
flake8_files = subprocess.check_output(FLAKE_FILES_ARGS).decode("utf-8").splitlines()
85-
86-
flake8_args = ['flake8', '--ignore=B,C,E,F,I,N,W', f'--select={ENABLED}'] + flake8_files
87-
flake8_env = os.environ.copy()
88-
flake8_env["PYTHONWARNINGS"] = "ignore"
89-
90-
try:
91-
subprocess.check_call(flake8_args, env=flake8_env)
92-
except subprocess.CalledProcessError:
93-
exit(1)
94-
9540
mypy_files = subprocess.check_output(MYPY_FILES_ARGS).decode("utf-8").splitlines()
9641
mypy_args = ['mypy', '--show-error-codes'] + mypy_files
9742

Diff for: test/lint/test_runner/src/main.rs

+43-5
Original file line numberDiff line numberDiff line change
@@ -187,18 +187,56 @@ fn lint_subtree() -> LintResult {
187187

188188
fn lint_py_lint() -> LintResult {
189189
let bin_name = "ruff";
190-
let checks = ["B006", "B008"]
191-
.iter()
192-
.map(|c| format!("--select={}", c))
193-
.collect::<Vec<_>>();
190+
let checks = format!(
191+
"--select={}",
192+
[
193+
"B006", // mutable-argument-default
194+
"B008", // function-call-in-default-argument
195+
"E101", // indentation contains mixed spaces and tabs
196+
"E401", // multiple imports on one line
197+
"E402", // module level import not at top of file
198+
"E701", // multiple statements on one line (colon)
199+
"E702", // multiple statements on one line (semicolon)
200+
"E703", // statement ends with a semicolon
201+
"E711", // comparison to None should be 'if cond is None:'
202+
"E714", // test for object identity should be "is not"
203+
"E721", // do not compare types, use "isinstance()"
204+
"E722", // do not use bare 'except'
205+
"E742", // do not define classes named "l", "O", or "I"
206+
"E743", // do not define functions named "l", "O", or "I"
207+
"F401", // module imported but unused
208+
"F402", // import module from line N shadowed by loop variable
209+
"F403", // 'from foo_module import *' used; unable to detect undefined names
210+
"F404", // future import(s) name after other statements
211+
"F405", // foo_function may be undefined, or defined from star imports: bar_module
212+
"F406", // "from module import *" only allowed at module level
213+
"F407", // an undefined __future__ feature name was imported
214+
"F601", // dictionary key name repeated with different values
215+
"F602", // dictionary key variable name repeated with different values
216+
"F621", // too many expressions in an assignment with star-unpacking
217+
"F631", // assertion test is a tuple, which are always True
218+
"F632", // use ==/!= to compare str, bytes, and int literals
219+
"F811", // redefinition of unused name from line N
220+
"F821", // undefined name 'Foo'
221+
"F822", // undefined name name in __all__
222+
"F823", // local variable name … referenced before assignment
223+
"F841", // local variable 'foo' is assigned to but never used
224+
"W191", // indentation contains tabs
225+
"W291", // trailing whitespace
226+
"W292", // no newline at end of file
227+
"W293", // blank line contains whitespace
228+
"W605", // invalid escape sequence "x"
229+
]
230+
.join(",")
231+
);
194232
let files = check_output(
195233
git()
196234
.args(["ls-files", "--", "*.py"])
197235
.args(get_pathspecs_exclude_subtrees()),
198236
)?;
199237

200238
let mut cmd = Command::new(bin_name);
201-
cmd.arg("check").args(checks).args(files.lines());
239+
cmd.args(["check", &checks]).args(files.lines());
202240

203241
match cmd.status() {
204242
Ok(status) if status.success() => Ok(()),

0 commit comments

Comments
 (0)