summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgithub-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>2023-04-17 07:25:14 +0000
committerGitHub <noreply@github.com>2023-04-17 07:25:14 +0000
commit1dba30b43ce7d2c43e6cc94120d8daca5d9ddabd (patch)
treebc8b8094f1b980173a6532ca6a1336d7654a7cf3
parentec96bdc206350e378137da27c2f4cd103a94dc63 (diff)
downloadpylint-git-1dba30b43ce7d2c43e6cc94120d8daca5d9ddabd.tar.gz
Improve output of `consider-using-generator` message for `min()` calls with `default` keyword (#8582) (#8583)
(cherry picked from commit 4a485e28f0a5118b37550123c79f1f6d0dec42a4) Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
-rw-r--r--doc/whatsnew/fragments/8563.bugfix3
-rw-r--r--pylint/checkers/refactoring/refactoring_checker.py6
-rw-r--r--tests/functional/c/consider/consider_using_generator.py5
-rw-r--r--tests/functional/c/consider/consider_using_generator.txt1
4 files changed, 14 insertions, 1 deletions
diff --git a/doc/whatsnew/fragments/8563.bugfix b/doc/whatsnew/fragments/8563.bugfix
new file mode 100644
index 000000000..3c9d38b1c
--- /dev/null
+++ b/doc/whatsnew/fragments/8563.bugfix
@@ -0,0 +1,3 @@
+Improve output of ``consider-using-generator`` message for ``min()` calls with ``default`` keyword.
+
+Closes #8563
diff --git a/pylint/checkers/refactoring/refactoring_checker.py b/pylint/checkers/refactoring/refactoring_checker.py
index b0a87b4c9..ec4d3d71e 100644
--- a/pylint/checkers/refactoring/refactoring_checker.py
+++ b/pylint/checkers/refactoring/refactoring_checker.py
@@ -1070,11 +1070,15 @@ class RefactoringChecker(checkers.BaseTokenChecker):
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(
diff --git a/tests/functional/c/consider/consider_using_generator.py b/tests/functional/c/consider/consider_using_generator.py
index e5ac31f32..ee561a7e9 100644
--- a/tests/functional/c/consider/consider_using_generator.py
+++ b/tests/functional/c/consider/consider_using_generator.py
@@ -18,3 +18,8 @@ tuple(0 for y in list(range(10)))
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)
diff --git a/tests/functional/c/consider/consider_using_generator.txt b/tests/functional/c/consider/consider_using_generator.txt
index 2c89a8d7e..8a2a4ec6d 100644
--- a/tests/functional/c/consider/consider_using_generator.txt
+++ b/tests/functional/c/consider/consider_using_generator.txt
@@ -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