Skip to content

Commit 4422094

Browse files
Fix nested-min-max output msg for sequences (#8234) (#8245)
(cherry picked from commit 29684fe) Co-authored-by: Dani Alcala <[email protected]>
1 parent 10f50a0 commit 4422094

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

doc/whatsnew/fragments/8168.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix ``nested-min-max`` suggestion message to indicate it's possible to splat iterable objects.
2+
3+
Closes #8168

pylint/checkers/nested_min_max.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import copy
1010
from typing import TYPE_CHECKING
1111

12-
from astroid import nodes
12+
from astroid import nodes, objects
1313

1414
from pylint.checkers import BaseChecker
1515
from pylint.checkers.utils import only_required_for_messages, safe_infer
@@ -18,6 +18,13 @@
1818
if TYPE_CHECKING:
1919
from pylint.lint import PyLinter
2020

21+
DICT_TYPES = (
22+
objects.DictValues,
23+
objects.DictKeys,
24+
objects.DictItems,
25+
nodes.node_classes.Dict,
26+
)
27+
2128

2229
class NestedMinMaxChecker(BaseChecker):
2330
"""Multiple nested min/max calls on the same line will raise multiple messages.
@@ -83,6 +90,20 @@ def visit_call(self, node: nodes.Call) -> None:
8390

8491
redundant_calls = self.get_redundant_calls(fixed_node)
8592

93+
for idx, arg in enumerate(fixed_node.args):
94+
if not isinstance(arg, nodes.Const):
95+
inferred = safe_infer(arg)
96+
if isinstance(
97+
inferred, (nodes.List, nodes.Tuple, nodes.Set, *DICT_TYPES)
98+
):
99+
splat_node = nodes.Starred(lineno=inferred.lineno)
100+
splat_node.value = arg
101+
fixed_node.args = (
102+
fixed_node.args[:idx]
103+
+ [splat_node]
104+
+ fixed_node.args[idx + 1 : idx]
105+
)
106+
86107
self.add_message(
87108
"nested-min-max",
88109
node=node,

tests/functional/n/nested_min_max.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,26 @@
1919
# This is too complicated (for now) as there is no clear better way to write it
2020
max(max(i for i in range(10)), 0)
2121
max(max(max(i for i in range(10)), 0), 1)
22+
23+
# These examples can be improved by splicing
24+
lst = [1, 2]
25+
max(3, max(lst)) # [nested-min-max]
26+
max(3, *lst)
27+
28+
nums = (1, 2,)
29+
max(3, max(nums)) # [nested-min-max]
30+
max(3, *nums)
31+
32+
nums = {1, 2}
33+
max(3, max(nums)) # [nested-min-max]
34+
max(3, *nums)
35+
36+
nums = {1: 2, 7: 10}
37+
max(3, max(nums)) # [nested-min-max]
38+
max(3, *nums)
39+
40+
max(3, max(nums.values())) # [nested-min-max]
41+
max(3, *nums.values())
42+
43+
lst2 = [3, 7, 10]
44+
max(3, max(nums), max(lst2)) # [nested-min-max]

tests/functional/n/nested_min_max.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ nested-min-max:8:0:8:25::Do not use nested call of 'min'; it's possible to do 'm
66
nested-min-max:11:0:11:25::Do not use nested call of 'min'; it's possible to do 'min(1, 2, 3, 4)' instead:INFERENCE
77
nested-min-max:12:0:12:40::Do not use nested call of 'min'; it's possible to do 'min(len([]), len([1]), len([1, 2]))' instead:INFERENCE
88
nested-min-max:17:0:17:27::Do not use nested call of 'orig_min'; it's possible to do 'orig_min(1, 2, 3)' instead:INFERENCE
9+
nested-min-max:25:0:25:16::Do not use nested call of 'max'; it's possible to do 'max(3, *lst)' instead:INFERENCE
10+
nested-min-max:29:0:29:17::Do not use nested call of 'max'; it's possible to do 'max(3, *nums)' instead:INFERENCE
11+
nested-min-max:33:0:33:17::Do not use nested call of 'max'; it's possible to do 'max(3, *nums)' instead:INFERENCE
12+
nested-min-max:37:0:37:17::Do not use nested call of 'max'; it's possible to do 'max(3, *nums)' instead:INFERENCE
13+
nested-min-max:40:0:40:26::Do not use nested call of 'max'; it's possible to do 'max(3, *nums.values())' instead:INFERENCE
14+
nested-min-max:44:0:44:28::Do not use nested call of 'max'; it's possible to do 'max(3, *nums, *lst2)' instead:INFERENCE

0 commit comments

Comments
 (0)