diff --git a/python/src/com/jetbrains/python/inspections/PyDictDuplicateKeysInspection.java b/python/src/com/jetbrains/python/inspections/PyDictDuplicateKeysInspection.java index 12125877d1b8..20232dcddc7b 100644 --- a/python/src/com/jetbrains/python/inspections/PyDictDuplicateKeysInspection.java +++ b/python/src/com/jetbrains/python/inspections/PyDictDuplicateKeysInspection.java @@ -25,11 +25,13 @@ import com.intellij.psi.PsiElementVisitor; import com.jetbrains.python.PyBundle; import com.jetbrains.python.inspections.quickfix.PyRemoveDictKeyQuickFix; import com.jetbrains.python.psi.*; +import com.jetbrains.python.psi.impl.PyBuiltinCache; import com.jetbrains.python.psi.impl.PyPsiUtils; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; @@ -90,19 +92,30 @@ public class PyDictDuplicateKeysInspection extends PyInspection { } @Nullable - private static Pair getDictLiteralKey(@NotNull PyKeyValueExpression argument) { + private Pair getDictLiteralKey(@NotNull PyKeyValueExpression argument) { final PyExpression key = argument.getKey(); final String keyValue = getKeyValue(key); return keyValue != null ? Pair.createNonNull(key, keyValue) : null; } @Nullable - private static String getKeyValue(@NotNull PsiElement node) { - return node instanceof PyStringLiteralExpression - ? ((PyStringLiteralExpression)node).getStringValue() - : node instanceof PyLiteralExpression || node instanceof PyReferenceExpression - ? node.getText() - : null; + private String getKeyValue(@NotNull PsiElement node) { + if (node instanceof PyStringLiteralExpression) { + return ((PyStringLiteralExpression)node).getStringValue(); + } + + if (node instanceof PyNumericLiteralExpression) { + final BigDecimal value = ((PyNumericLiteralExpression)node).getBigDecimalValue(); + if (value != null) { + final String keyValue = value.toPlainString(); + return !value.equals(BigDecimal.ZERO) && + myTypeEvalContext.getType((PyNumericLiteralExpression)node) == PyBuiltinCache.getInstance(node).getComplexType() + ? keyValue + "j" + : keyValue; + } + } + + return node instanceof PyLiteralExpression || node instanceof PyReferenceExpression ? node.getText() : null; } private void checkKey(@NotNull Map map, @@ -121,7 +134,7 @@ public class PyDictDuplicateKeysInspection extends PyInspection { } @Nullable - private static Pair getDictCallKey(@Nullable PyExpression argument) { + private Pair getDictCallKey(@Nullable PyExpression argument) { if (argument instanceof PyParenthesizedExpression) { final PyExpression expression = PyPsiUtils.flattenParens(argument); if (expression instanceof PyTupleExpression) { diff --git a/python/testData/inspections/PyDictDuplicateKeysInspection/test.py b/python/testData/inspections/PyDictDuplicateKeysInspection/test.py index 25b13f86b107..ac2066177e57 100644 --- a/python/testData/inspections/PyDictDuplicateKeysInspection/test.py +++ b/python/testData/inspections/PyDictDuplicateKeysInspection/test.py @@ -27,4 +27,10 @@ d = {True: d = dict([(True, 1), (True, 2)]) d = {None: 1, None: 2} -d = dict([(None, 1), (None, 2)]) \ No newline at end of file +d = dict([(None, 1), (None, 2)]) + +d = {11: 1, 1_1: 1} +d = {11.1: 1, 1_1.1: 1} +d = {11j: 1, 1_1j: 1} +d = {11j: 1, 11: 1} +d = {0j: 1, 0: 2} \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java b/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java index 5d3d4c2e1ed2..6065ce4f9e78 100644 --- a/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java +++ b/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java @@ -210,7 +210,7 @@ public class PythonInspectionsTest extends PyTestCase { } public void testPyDictDuplicateKeysInspection() { - doHighlightingTest(PyDictDuplicateKeysInspection.class); + doHighlightingTest(PyDictDuplicateKeysInspection.class, LanguageLevel.PYTHON37); } public void testPyListCreationInspection() { //PY-2823