Skip to content

Commit 1dba30b

Browse files
Improve output of consider-using-generator message for min() calls with default keyword (#8582) (#8583)
(cherry picked from commit 4a485e2) Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
1 parent ec96bdc commit 1dba30b

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

doc/whatsnew/fragments/8563.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improve output of ``consider-using-generator`` message for ``min()` calls with ``default`` keyword.
2+
3+
Closes #8563

pylint/checkers/refactoring/refactoring_checker.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1070,11 +1070,15 @@ def _check_consider_using_generator(self, node: nodes.Call) -> None:
10701070
and isinstance(node.func, nodes.Name)
10711071
and node.func.name in checked_call
10721072
):
1073-
# functions in checked_calls take exactly one argument
1073+
# functions in checked_calls take exactly one positional argument
10741074
# check whether the argument is list comprehension
10751075
if len(node.args) == 1 and isinstance(node.args[0], nodes.ListComp):
10761076
# remove square brackets '[]'
10771077
inside_comp = node.args[0].as_string()[1:-1]
1078+
if node.keywords:
1079+
inside_comp = f"({inside_comp})"
1080+
inside_comp += ", "
1081+
inside_comp += ", ".join(kw.as_string() for kw in node.keywords)
10781082
call_name = node.func.name
10791083
if call_name in {"any", "all"}:
10801084
self.add_message(

tests/functional/c/consider/consider_using_generator.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@
1818
sum(x*x for x in range(10))
1919
min(x*x for x in range(10))
2020
max(x*x for x in range(10))
21+
22+
# Keyword arguments
23+
# https://github.com/pylint-dev/pylint/issues/8563
24+
min([x*x for x in range(10)], default=42) # [consider-using-generator]
25+
min((x*x for x in range(10)), default=42)

tests/functional/c/consider/consider_using_generator.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ consider-using-generator:11:0:11:35::Consider using a generator instead 'tuple(0
33
consider-using-generator:12:0:12:29::Consider using a generator instead 'sum(x * x for x in range(10))':UNDEFINED
44
consider-using-generator:13:0:13:29::Consider using a generator instead 'min(x * x for x in range(10))':UNDEFINED
55
consider-using-generator:14:0:14:29::Consider using a generator instead 'max(x * x for x in range(10))':UNDEFINED
6+
consider-using-generator:24:0:24:41::Consider using a generator instead 'min((x * x for x in range(10)), default=42)':UNDEFINED

0 commit comments

Comments
 (0)