Skip to content

Commit

Permalink
Improve output of consider-using-generator message for min() call…
Browse files Browse the repository at this point in the history
…s with `default` keyword (#8582) (#8583)

(cherry picked from commit 4a485e2)

Co-authored-by: Jacob Walls <[email protected]>
  • Loading branch information
github-actions[bot] and jacobtylerwalls committed Apr 17, 2023
1 parent ec96bdc commit 1dba30b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/8563.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improve output of ``consider-using-generator`` message for ``min()` calls with ``default`` keyword.

Closes #8563
6 changes: 5 additions & 1 deletion pylint/checkers/refactoring/refactoring_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,11 +1070,15 @@ def _check_consider_using_generator(self, node: nodes.Call) -> None:
and isinstance(node.func, nodes.Name)
and node.func.name in checked_call
):
# functions in checked_calls take exactly one argument
# functions in checked_calls take exactly one positional argument
# check whether the argument is list comprehension
if len(node.args) == 1 and isinstance(node.args[0], nodes.ListComp):
# remove square brackets '[]'
inside_comp = node.args[0].as_string()[1:-1]
if node.keywords:
inside_comp = f"({inside_comp})"
inside_comp += ", "
inside_comp += ", ".join(kw.as_string() for kw in node.keywords)
call_name = node.func.name
if call_name in {"any", "all"}:
self.add_message(
Expand Down
5 changes: 5 additions & 0 deletions tests/functional/c/consider/consider_using_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@
sum(x*x for x in range(10))
min(x*x for x in range(10))
max(x*x for x in range(10))

# Keyword arguments
# https://github.com/pylint-dev/pylint/issues/8563
min([x*x for x in range(10)], default=42) # [consider-using-generator]
min((x*x for x in range(10)), default=42)
1 change: 1 addition & 0 deletions tests/functional/c/consider/consider_using_generator.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ consider-using-generator:11:0:11:35::Consider using a generator instead 'tuple(0
consider-using-generator:12:0:12:29::Consider using a generator instead 'sum(x * x for x in range(10))':UNDEFINED
consider-using-generator:13:0:13:29::Consider using a generator instead 'min(x * x for x in range(10))':UNDEFINED
consider-using-generator:14:0:14:29::Consider using a generator instead 'max(x * x for x in range(10))':UNDEFINED
consider-using-generator:24:0:24:41::Consider using a generator instead 'min((x * x for x in range(10)), default=42)':UNDEFINED

0 comments on commit 1dba30b

Please sign in to comment.