Skip to content

Commit

Permalink
Fixed false positive nested-min-max for nested lists (#9323) (#9337)
Browse files Browse the repository at this point in the history
Nesting is useful for finding the maximum in a matrix.
Therefore, pylint allows nesting of the form:
max(max([[1, 2, 3], [4, 5, 6]]))

Closes #9307

Co-authored-by: Pierre Sassoulas <[email protected]>
(cherry picked from commit da13c74)

Co-authored-by: Udi Fuchs <[email protected]>
  • Loading branch information
github-actions[bot] and udifuchs authored Dec 30, 2023
1 parent 1a5ffc1 commit 015c43a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/9307.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed false positive nested-min-max for nested lists.

Closes #9307
9 changes: 8 additions & 1 deletion pylint/checkers/nested_min_max.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@ def get_redundant_calls(cls, node: nodes.Call) -> list[nodes.Call]:
return [
arg
for arg in node.args
if cls.is_min_max_call(arg) and arg.func.name == node.func.name
if (
cls.is_min_max_call(arg)
and arg.func.name == node.func.name
# Nesting is useful for finding the maximum in a matrix.
# Allow: max(max([[1, 2, 3], [4, 5, 6]]))
# Meaning, redundant call only if parent max call has more than 1 arg.
and len(arg.parent.args) > 1
)
]

@only_required_for_messages("nested-min-max")
Expand Down
8 changes: 8 additions & 0 deletions tests/functional/n/nested_min_max.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,11 @@

max(3, max(list(range(4)))) # [nested-min-max]
max(3, *list(range(4)))

# Nesting is useful for finding the maximum in a matrix
# No message if external call has exactly 1 argument
matrix = [[1, 2, 3], [4, 5, 6]]
max(max(matrix))
max(max(max(matrix)))
max(3, max(max(matrix))) # [nested-min-max]
max(max(3, max(matrix))) # [nested-min-max]
2 changes: 2 additions & 0 deletions tests/functional/n/nested_min_max.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ nested-min-max:46:0:46:25::Do not use nested call of 'max'; it's possible to do
nested-min-max:49:0:49:45::Do not use nested call of 'max'; it's possible to do 'max(3, *[5] + [i for i in range(4) if i])' instead:INFERENCE
nested-min-max:52:0:52:33::Do not use nested call of 'max'; it's possible to do 'max(3, *[5] + list(range(4)))' instead:INFERENCE
nested-min-max:55:0:55:27::Do not use nested call of 'max'; it's possible to do 'max(3, *list(range(4)))' instead:INFERENCE
nested-min-max:63:0:63:24::Do not use nested call of 'max'; it's possible to do 'max(3, max(matrix))' instead:INFERENCE
nested-min-max:64:4:64:23::Do not use nested call of 'max'; it's possible to do 'max(3, *matrix)' instead:INFERENCE

0 comments on commit 015c43a

Please sign in to comment.