mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-01 20:31:57 +00:00
[pylint] Avoid flattening nested min/max when outer call has single argument (PLW3301) (#16885)
## Summary Fixes false positives (and incorrect autofixes) in `nested-min-max` (`PLW3301`) when the outer `min`/`max` call only has a single argument. Previously the rule would flatten: ```python min(min([2, 3], [4, 1])) ``` into `min([2, 3], [4, 1])`, changing the semantics. The rule now skips any nested call when the outer call has only one positional argument. The pylint fixture and snapshot were updated accordingly. ## Test Plan Ran Ruff against the updated `nested_min_max.py` fixture: ```shell cargo run -p ruff -- check crates/ruff_linter/resources/test/fixtures/pylint/nested_min_max.py --no-cache --select=PLW3301 --preview ``` to verify that `min(min([2, 3], [4, 1]))` and `max(max([2, 4], [3, 1]))` are no longer flagged. Updated the fixture and snapshot; all other existing warnings remain unchanged. The code compiles and the unit tests pass. --- This PR was generated by an AI system in collaboration with maintainers: @carljm, @ntBre Fixes #16163 --------- Signed-off-by: Gene Parmesan Thomas <201852096+gopoto@users.noreply.github.com> Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
This commit is contained in:
parent
e66f182045
commit
5b3a501fae
2 changed files with 10 additions and 3 deletions
|
|
@ -55,3 +55,8 @@ max_word_len = max(
|
|||
*(len(word) for word in "blah blah blah".split(" ")),
|
||||
len("Done!"),
|
||||
)
|
||||
|
||||
|
||||
# Outer call has a single argument, inner call has multiple arguments; should not trigger.
|
||||
min(min([2, 3], [4, 1]))
|
||||
max(max([2, 4], [3, 1]))
|
||||
|
|
|
|||
|
|
@ -155,9 +155,11 @@ pub(crate) fn nested_min_max(
|
|||
let Some(min_max) = MinMax::try_from_call(func, keywords, checker.semantic()) else {
|
||||
return;
|
||||
};
|
||||
|
||||
if matches!(&args, [Expr::Call(ast::ExprCall { arguments: Arguments {args, .. }, .. })] if args.len() == 1)
|
||||
{
|
||||
// It's only safe to flatten nested calls if the outer call has more than one argument.
|
||||
// When the outer call has a single argument, flattening would change the semantics by
|
||||
// changing the shape of the call from treating the inner result as an iterable (or a scalar)
|
||||
// to passing multiple arguments directly, which can lead to behavioral changes.
|
||||
if args.len() < 2 {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue