Skip to content

Commit 319ba11

Browse files
tushar-deepsourcePierre-SassoulasDanielNoord
authored
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 <[email protected]> Co-authored-by: Daniël van Noord <[email protected]>
1 parent 30d1385 commit 319ba11

File tree

6 files changed

+45
-4
lines changed

6 files changed

+45
-4
lines changed

.copyrite_aliases

+5
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,10 @@
108108
],
109109
"authoritative_mail": "[email protected]",
110110
"name": "bot"
111+
},
112+
{
113+
114+
"authoritative_mail": "[email protected]",
115+
"name": "Tushar Sadhwani"
111116
}
112117
]

ChangeLog

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ Release date: TBA
1111
..
1212
Put new features here and also in 'doc/whatsnew/2.13.rst'
1313

14+
* Fixed false positive ``consider-using-dict-comprehension`` when creating a dict
15+
using a list of tuples where key AND value vary depending on the same condition.
16+
17+
Closes #5588
18+
1419
* When run in parallel mode ``pylint`` now pickles the data passed to subprocesses with
1520
the ``dill`` package. The ``dill`` package has therefore been added as a dependency.
1621

doc/whatsnew/2.13.rst

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ Extensions
3232
Other Changes
3333
=============
3434

35+
* Fixed false positive ``consider-using-dict-comprehension`` when creating a dict
36+
using a list of tuples where key AND value vary depending on the same condition.
37+
38+
Closes #5588
39+
3540
* When run in parallel mode ``pylint`` now pickles the data passed to subprocesses with
3641
the ``dill`` package. The ``dill`` package has therefore been added as a dependency.
3742

pylint/checkers/refactoring/refactoring_checker.py

+22-3
Original file line numberDiff line numberDiff line change
@@ -934,9 +934,28 @@ def _check_consider_using_comprehension_constructor(self, node):
934934
and node.args
935935
and isinstance(node.args[0], nodes.ListComp)
936936
):
937-
if node.func.name == "dict" and not isinstance(
938-
node.args[0].elt, nodes.Call
939-
):
937+
if node.func.name == "dict":
938+
element = node.args[0].elt
939+
if isinstance(element, nodes.Call):
940+
return
941+
942+
# If we have an `IfExp` here where both the key AND value
943+
# are different, then don't raise the issue. See #5588
944+
if (
945+
isinstance(element, nodes.IfExp)
946+
and isinstance(element.body, (nodes.Tuple, nodes.List))
947+
and len(element.body.elts) == 2
948+
and isinstance(element.orelse, (nodes.Tuple, nodes.List))
949+
and len(element.orelse.elts) == 2
950+
):
951+
key1, value1 = element.body.elts
952+
key2, value2 = element.orelse.elts
953+
if (
954+
key1.as_string() != key2.as_string()
955+
and value1.as_string() != value2.as_string()
956+
):
957+
return
958+
940959
message_name = "consider-using-dict-comprehension"
941960
self.add_message(message_name, node=node)
942961
elif node.func.name == "set":

tests/functional/c/consider/consider_using_dict_comprehension.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# pylint: disable=missing-docstring, invalid-name, use-dict-literal
1+
# pylint: disable=missing-docstring, invalid-name, use-dict-literal, line-too-long
22

33
numbers = [1, 2, 3, 4, 5, 6]
44

@@ -8,5 +8,10 @@
88

99
dict([(number, number*2) for number in numbers]) # [consider-using-dict-comprehension]
1010

11+
stuff = {1: 10, 2: -20}
12+
dict([(k, v) if v > 0 else (k, 0) for k, v in stuff.items()]) # [consider-using-dict-comprehension]
13+
dict([(k, v) if v > 0 else (k*2, v) for k, v in stuff.items()]) # [consider-using-dict-comprehension]
14+
dict([(k, v) if v > 0 else (k * 2, 0) for k, v in stuff.items()])
15+
1116
# Cannot emit as this cannot be written as a comprehension
1217
dict([value.split("=") for value in ["a=b", "c=d"]])
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
consider-using-dict-comprehension:9:0:9:48::Consider using a dictionary comprehension:UNDEFINED
2+
consider-using-dict-comprehension:12:0:12:61::Consider using a dictionary comprehension:UNDEFINED
3+
consider-using-dict-comprehension:13:0:13:63::Consider using a dictionary comprehension:UNDEFINED

0 commit comments

Comments
 (0)