Skip to content

Commit da13c74

Browse files
Fixed false positive nested-min-max for nested lists (#9323)
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 <pierre.sassoulas@gmail.com>
1 parent 65cf711 commit da13c74

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed false positive nested-min-max for nested lists.
2+
3+
Closes #9307

pylint/checkers/nested_min_max.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,14 @@ def get_redundant_calls(cls, node: nodes.Call) -> list[nodes.Call]:
6262
return [
6363
arg
6464
for arg in node.args
65-
if cls.is_min_max_call(arg) and arg.func.name == node.func.name
65+
if (
66+
cls.is_min_max_call(arg)
67+
and arg.func.name == node.func.name
68+
# Nesting is useful for finding the maximum in a matrix.
69+
# Allow: max(max([[1, 2, 3], [4, 5, 6]]))
70+
# Meaning, redundant call only if parent max call has more than 1 arg.
71+
and len(arg.parent.args) > 1
72+
)
6673
]
6774

6875
@only_required_for_messages("nested-min-max")

tests/functional/n/nested_min_max.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,11 @@
5454

5555
max(3, max(list(range(4)))) # [nested-min-max]
5656
max(3, *list(range(4)))
57+
58+
# Nesting is useful for finding the maximum in a matrix
59+
# No message if external call has exactly 1 argument
60+
matrix = [[1, 2, 3], [4, 5, 6]]
61+
max(max(matrix))
62+
max(max(max(matrix)))
63+
max(3, max(max(matrix))) # [nested-min-max]
64+
max(max(3, max(matrix))) # [nested-min-max]

tests/functional/n/nested_min_max.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ nested-min-max:46:0:46:25::Do not use nested call of 'max'; it's possible to do
1616
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
1717
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
1818
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
19+
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
20+
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 commit comments

Comments
 (0)