diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/PyDictLiteralFormToConstructorIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/PyDictLiteralFormToConstructorIntention.java index e82409e28cb4..6aad8994388b 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/PyDictLiteralFormToConstructorIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyDictLiteralFormToConstructorIntention.java @@ -3,6 +3,7 @@ package com.jetbrains.python.codeInsight.intentions; import com.intellij.codeInsight.intention.impl.BaseIntentionAction; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiFile; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; @@ -38,23 +39,18 @@ public class PyDictLiteralFormToConstructorIntention extends BaseIntentionAction if (dictExpression != null) { PyKeyValueExpression[] elements = dictExpression.getElements(); - boolean canConvert = true; if (elements.length != 0) { for (PyKeyValueExpression element : elements) { PyExpression key = element.getKey(); if (! (key instanceof PyStringLiteralExpression)) return false; String str = ((PyStringLiteralExpression)key).getStringValue(); if (PyNames.isReserved(str)) return false; + if(str.length() == 0 || Character.isDigit(str.charAt(0))) return false; - try { - Integer.parseInt(str) ; - canConvert = false; - } catch (NumberFormatException e) { - // pass - } + if (!StringUtil.isJavaIdentifier(str)) return false; } } - if (canConvert) return true; + return true; } return false; } diff --git a/python/testData/intentions/beforeDictLiteralFormToConstructor2.py b/python/testData/intentions/beforeDictLiteralFormToConstructor2.py new file mode 100644 index 000000000000..489db637998f --- /dev/null +++ b/python/testData/intentions/beforeDictLiteralFormToConstructor2.py @@ -0,0 +1 @@ +a = {'a b': 3, 'b': 5} \ No newline at end of file diff --git a/python/testData/intentions/beforeDictLiteralFormToConstructor3.py b/python/testData/intentions/beforeDictLiteralFormToConstructor3.py new file mode 100644 index 000000000000..69e583a4328e --- /dev/null +++ b/python/testData/intentions/beforeDictLiteralFormToConstructor3.py @@ -0,0 +1 @@ +a = {'1b': 3, 'b': 5} \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyIntentionTest.java b/python/testSrc/com/jetbrains/python/PyIntentionTest.java index 207baaa03beb..5e1feb7134b9 100644 --- a/python/testSrc/com/jetbrains/python/PyIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/PyIntentionTest.java @@ -156,6 +156,17 @@ public class PyIntentionTest extends PyTestCase { assertNull(action); } + public void testDictLiteralFormToConstructor2() { //PY-5157 + myFixture.configureByFile("intentions/beforeDictLiteralFormToConstructor2" + ".py"); + final IntentionAction action = myFixture.getAvailableIntention(PyBundle.message("INTN.convert.dict.literal.to.dict.constructor")); + assertNull(action); + } + public void testDictLiteralFormToConstructor3() { + myFixture.configureByFile("intentions/beforeDictLiteralFormToConstructor3" + ".py"); + final IntentionAction action = myFixture.getAvailableIntention(PyBundle.message("INTN.convert.dict.literal.to.dict.constructor")); + assertNull(action); + } + public void testQuotedString() { //PY-2915 doTest(PyBundle.message("INTN.quoted.string.double.to.single")); }