Skip to content

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

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
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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

* ``used-before-assignment`` now considers that assignments in a try block
may not have occurred when the except or finally blocks are executed.

Expand Down
5 changes: 5 additions & 0 deletions doc/whatsnew/2.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

* By default, pylint does no longer take files starting with ``.#`` into account. Those are
considered `emacs file locks`_. This behavior can be reverted by redefining the
``ignore-patterns`` option.
Expand Down
25 changes: 22 additions & 3 deletions pylint/checkers/refactoring/refactoring_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,9 +934,28 @@ def _check_consider_using_comprehension_constructor(self, node):
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":
Expand Down
Original file line number Diff line number Diff line change
@@ -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]

Expand All @@ -8,5 +8,10 @@

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"]])
Original file line number Diff line number Diff line change
@@ -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