diff --git a/doc/whatsnew/fragments/9307.false_positive b/doc/whatsnew/fragments/9307.false_positive new file mode 100644 index 0000000000..546959bf66 --- /dev/null +++ b/doc/whatsnew/fragments/9307.false_positive @@ -0,0 +1,3 @@ +Fixed false positive nested-min-max for nested lists. + +Closes #9307 diff --git a/pylint/checkers/nested_min_max.py b/pylint/checkers/nested_min_max.py index a935d62f53..c8231fe7d2 100644 --- a/pylint/checkers/nested_min_max.py +++ b/pylint/checkers/nested_min_max.py @@ -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") diff --git a/tests/functional/n/nested_min_max.py b/tests/functional/n/nested_min_max.py index 7bb11264e9..d4c31bd858 100644 --- a/tests/functional/n/nested_min_max.py +++ b/tests/functional/n/nested_min_max.py @@ -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] diff --git a/tests/functional/n/nested_min_max.txt b/tests/functional/n/nested_min_max.txt index 87b31daf65..80d6a24d32 100644 --- a/tests/functional/n/nested_min_max.txt +++ b/tests/functional/n/nested_min_max.txt @@ -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