Skip to content

Improve output of consider-using-generator message for min() calls with default keyword #8582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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