summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTushar Sadhwani <86737547+tushar-deepsource@users.noreply.github.com>2021-12-30 23:51:26 +0530
committerGitHub <noreply@github.com>2021-12-30 19:21:26 +0100
commit319ba11e45a987367816560c8aa7ecc82c8af4a0 (patch)
tree852583bcc5cc375849d952c088eac2332dab2163
parent30d1385599beefc8f65f3c71cbbb52666bf1420b (diff)
downloadpylint-git-319ba11e45a987367816560c8aa7ecc82c8af4a0.tar.gz
Fix false positive `consider-using-dict-comprehension` when creating a dict using a list of tuple where key AND value vary depending on the same condition (#5590)
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com> Co-authored-by: Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>
-rw-r--r--.copyrite_aliases5
-rw-r--r--ChangeLog5
-rw-r--r--doc/whatsnew/2.13.rst5
-rw-r--r--pylint/checkers/refactoring/refactoring_checker.py25
-rw-r--r--tests/functional/c/consider/consider_using_dict_comprehension.py7
-rw-r--r--tests/functional/c/consider/consider_using_dict_comprehension.txt2
6 files changed, 45 insertions, 4 deletions
diff --git a/.copyrite_aliases b/.copyrite_aliases
index 3dea23b4f..773b12d61 100644
--- a/.copyrite_aliases
+++ b/.copyrite_aliases
@@ -108,5 +108,10 @@
],
"authoritative_mail": "bot@noreply.github.com",
"name": "bot"
+ },
+ {
+ "mails": ["tushar.sadhwani000@gmail.com", "tushar@deepsource.io"],
+ "authoritative_mail": "tushar.sadhwani000@gmail.com",
+ "name": "Tushar Sadhwani"
}
]
diff --git a/ChangeLog b/ChangeLog
index 19a3b73ea..1e8d79d43 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -11,6 +11,11 @@ Release date: TBA
..
Put new features here and also in 'doc/whatsnew/2.13.rst'
+* Fixed false positive ``consider-using-dict-comprehension`` when creating a dict
+ using a list of tuples where key AND value vary depending on the same condition.
+
+ Closes #5588
+
* When run in parallel mode ``pylint`` now pickles the data passed to subprocesses with
the ``dill`` package. The ``dill`` package has therefore been added as a dependency.
diff --git a/doc/whatsnew/2.13.rst b/doc/whatsnew/2.13.rst
index 9510cd54a..df1a99757 100644
--- a/doc/whatsnew/2.13.rst
+++ b/doc/whatsnew/2.13.rst
@@ -32,6 +32,11 @@ Extensions
Other Changes
=============
+* Fixed false positive ``consider-using-dict-comprehension`` when creating a dict
+ using a list of tuples where key AND value vary depending on the same condition.
+
+ Closes #5588
+
* When run in parallel mode ``pylint`` now pickles the data passed to subprocesses with
the ``dill`` package. The ``dill`` package has therefore been added as a dependency.
diff --git a/pylint/checkers/refactoring/refactoring_checker.py b/pylint/checkers/refactoring/refactoring_checker.py
index d08ad8bdc..40a72a53f 100644
--- a/pylint/checkers/refactoring/refactoring_checker.py
+++ b/pylint/checkers/refactoring/refactoring_checker.py
@@ -934,9 +934,28 @@ class RefactoringChecker(checkers.BaseTokenChecker):
and node.args
and isinstance(node.args[0], nodes.ListComp)
):
- if node.func.name == "dict" and not isinstance(
- node.args[0].elt, nodes.Call
- ):
+ if node.func.name == "dict":
+ element = node.args[0].elt
+ if isinstance(element, nodes.Call):
+ return
+
+ # If we have an `IfExp` here where both the key AND value
+ # are different, then don't raise the issue. See #5588
+ if (
+ isinstance(element, nodes.IfExp)
+ and isinstance(element.body, (nodes.Tuple, nodes.List))
+ and len(element.body.elts) == 2
+ and isinstance(element.orelse, (nodes.Tuple, nodes.List))
+ and len(element.orelse.elts) == 2
+ ):
+ key1, value1 = element.body.elts
+ key2, value2 = element.orelse.elts
+ if (
+ key1.as_string() != key2.as_string()
+ and value1.as_string() != value2.as_string()
+ ):
+ return
+
message_name = "consider-using-dict-comprehension"
self.add_message(message_name, node=node)
elif node.func.name == "set":
diff --git a/tests/functional/c/consider/consider_using_dict_comprehension.py b/tests/functional/c/consider/consider_using_dict_comprehension.py
index c9d740e18..ef9ee3f01 100644
--- a/tests/functional/c/consider/consider_using_dict_comprehension.py
+++ b/tests/functional/c/consider/consider_using_dict_comprehension.py
@@ -1,4 +1,4 @@
-# pylint: disable=missing-docstring, invalid-name, use-dict-literal
+# pylint: disable=missing-docstring, invalid-name, use-dict-literal, line-too-long
numbers = [1, 2, 3, 4, 5, 6]
@@ -8,5 +8,10 @@ dict([])
dict([(number, number*2) for number in numbers]) # [consider-using-dict-comprehension]
+stuff = {1: 10, 2: -20}
+dict([(k, v) if v > 0 else (k, 0) for k, v in stuff.items()]) # [consider-using-dict-comprehension]
+dict([(k, v) if v > 0 else (k*2, v) for k, v in stuff.items()]) # [consider-using-dict-comprehension]
+dict([(k, v) if v > 0 else (k * 2, 0) for k, v in stuff.items()])
+
# Cannot emit as this cannot be written as a comprehension
dict([value.split("=") for value in ["a=b", "c=d"]])
diff --git a/tests/functional/c/consider/consider_using_dict_comprehension.txt b/tests/functional/c/consider/consider_using_dict_comprehension.txt
index a241d1907..2bde7ffa2 100644
--- a/tests/functional/c/consider/consider_using_dict_comprehension.txt
+++ b/tests/functional/c/consider/consider_using_dict_comprehension.txt
@@ -1 +1,3 @@
consider-using-dict-comprehension:9:0:9:48::Consider using a dictionary comprehension:UNDEFINED
+consider-using-dict-comprehension:12:0:12:61::Consider using a dictionary comprehension:UNDEFINED
+consider-using-dict-comprehension:13:0:13:63::Consider using a dictionary comprehension:UNDEFINED