Skip to content

Commit

Permalink
[pylint] Fix PLW3301 auto-fix with generators (#4412)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanPlasse authored May 13, 2023
1 parent 0a68636 commit a0258f2
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/ruff/resources/test/fixtures/pylint/nested_min_max.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@
1, # This is a comment.
min(2, 3),
)

# Handle iterable expressions.
min(1, min(a))
min(1, min(i for i in range(10)))
max(1, max(a))
max(1, max(i for i in range(10)))
11 changes: 11 additions & 0 deletions crates/ruff/src/rules/pylint/rules/nested_min_max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ fn collect_nested_args(context: &Context, min_max: MinMax, args: &[Expr]) -> Vec
keywords,
}) = arg.node()
{
if args.len() == 1 {
let new_arg = Expr::new(
TextRange::default(),
ast::ExprStarred {
value: Box::new(args[0].clone()),
ctx: ast::ExprContext::Load,
},
);
new_args.push(new_arg);
continue;
}
if MinMax::try_from_call(func, keywords, context) == Some(min_max) {
inner(context, min_max, args, new_args);
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,83 @@ nested_min_max.py:18:1: PLW3301 Nested `min` calls can be flattened
21 | | min(2, 3),
22 | | )
| |_^ PLW3301
23 |
24 | # Handle iterable expressions.
|
= help: Flatten nested `min` calls

nested_min_max.py:24:1: PLW3301 [*] Nested `min` calls can be flattened
|
24 | # Handle iterable expressions.
25 | min(1, min(a))
| ^^^^^^^^^^^^^^ PLW3301
26 | min(1, min(i for i in range(10)))
27 | max(1, max(a))
|
= help: Flatten nested `min` calls

Suggested fix
21 21 | )
22 22 |
23 23 | # Handle iterable expressions.
24 |-min(1, min(a))
24 |+min(1, *a)
25 25 | min(1, min(i for i in range(10)))
26 26 | max(1, max(a))
27 27 | max(1, max(i for i in range(10)))

nested_min_max.py:25:1: PLW3301 [*] Nested `min` calls can be flattened
|
25 | # Handle iterable expressions.
26 | min(1, min(a))
27 | min(1, min(i for i in range(10)))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PLW3301
28 | max(1, max(a))
29 | max(1, max(i for i in range(10)))
|
= help: Flatten nested `min` calls

Suggested fix
22 22 |
23 23 | # Handle iterable expressions.
24 24 | min(1, min(a))
25 |-min(1, min(i for i in range(10)))
25 |+min(1, *(i for i in range(10)))
26 26 | max(1, max(a))
27 27 | max(1, max(i for i in range(10)))

nested_min_max.py:26:1: PLW3301 [*] Nested `max` calls can be flattened
|
26 | min(1, min(a))
27 | min(1, min(i for i in range(10)))
28 | max(1, max(a))
| ^^^^^^^^^^^^^^ PLW3301
29 | max(1, max(i for i in range(10)))
|
= help: Flatten nested `max` calls

Suggested fix
23 23 | # Handle iterable expressions.
24 24 | min(1, min(a))
25 25 | min(1, min(i for i in range(10)))
26 |-max(1, max(a))
26 |+max(1, *a)
27 27 | max(1, max(i for i in range(10)))

nested_min_max.py:27:1: PLW3301 [*] Nested `max` calls can be flattened
|
27 | min(1, min(i for i in range(10)))
28 | max(1, max(a))
29 | max(1, max(i for i in range(10)))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PLW3301
|
= help: Flatten nested `max` calls

Suggested fix
24 24 | min(1, min(a))
25 25 | min(1, min(i for i in range(10)))
26 26 | max(1, max(a))
27 |-max(1, max(i for i in range(10)))
27 |+max(1, *(i for i in range(10)))


0 comments on commit a0258f2

Please sign in to comment.