Skip to content

Commit 37a6d76

Browse files
committed
Merge bitcoin#30553: lint: Find function calls in default arguments
fac7b7f lint: Find function calls in default arguments (MarcoFalke) Pull request description: This type of bug in the test code keeps biting back regularly, is hard to debug, and wastes review cycles: bitcoin#30543 (comment) . Fix all issues by catching it with a linter that checks for rule B008: https://docs.astral.sh/ruff/rules/function-call-in-default-argument/ This also allows to drop the hand-written linter that checks for rule B006: https://docs.astral.sh/ruff/rules/mutable-argument-default/ ACKs for top commit: davidgumberg: reACK bitcoin@fac7b7f Tree-SHA512: a47a28a35ec9c81947cb8c3e625f1eb8c0d7e780a4d768491ef94b2beed43b9710bf6c1044da18c7fd677ea5576fb9077c7f77b4465033fedfdca9920c185bf7
2 parents 1900336 + fac7b7f commit 37a6d76

File tree

3 files changed

+35
-72
lines changed

3 files changed

+35
-72
lines changed

ci/lint/04_install.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ ${CI_RETRY_EXE} pip3 install \
5353
lief==0.13.2 \
5454
mypy==1.4.1 \
5555
pyzmq==25.1.0 \
56+
ruff==0.5.5 \
5657
vulture==2.6
5758

5859
SHELLCHECK_VERSION=v0.8.0

test/lint/lint-python-mutable-default-parameters.py

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

test/lint/test_runner/src/main.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ fn get_linter_list() -> Vec<&'static Linter> {
3535
name: "markdown",
3636
lint_fn: lint_markdown
3737
},
38+
&Linter {
39+
description: "Check the default arguments in python",
40+
name: "py_mut_arg_default",
41+
lint_fn: lint_py_mut_arg_default,
42+
},
3843
&Linter {
3944
description: "Check that std::filesystem is not used directly",
4045
name: "std_filesystem",
@@ -180,6 +185,35 @@ fn lint_subtree() -> LintResult {
180185
}
181186
}
182187

188+
fn lint_py_mut_arg_default() -> LintResult {
189+
let bin_name = "ruff";
190+
let checks = ["B006", "B008"]
191+
.iter()
192+
.map(|c| format!("--select={}", c))
193+
.collect::<Vec<_>>();
194+
let files = check_output(
195+
git()
196+
.args(["ls-files", "--", "*.py"])
197+
.args(get_pathspecs_exclude_subtrees()),
198+
)?;
199+
200+
let mut cmd = Command::new(bin_name);
201+
cmd.arg("check").args(checks).args(files.lines());
202+
203+
match cmd.status() {
204+
Ok(status) if status.success() => Ok(()),
205+
Ok(_) => Err(format!("`{}` found errors!", bin_name)),
206+
Err(e) if e.kind() == ErrorKind::NotFound => {
207+
println!(
208+
"`{}` was not found in $PATH, skipping those checks.",
209+
bin_name
210+
);
211+
Ok(())
212+
}
213+
Err(e) => Err(format!("Error running `{}`: {}", bin_name, e)),
214+
}
215+
}
216+
183217
fn lint_std_filesystem() -> LintResult {
184218
let found = git()
185219
.args([

0 commit comments

Comments
 (0)