diff options
Diffstat (limited to 'pylint/checkers')
-rw-r--r-- | pylint/checkers/refactoring/refactoring_checker.py | 25 |
1 files changed, 22 insertions, 3 deletions
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": |