From a88719627ff09540900799c2900668db77eaa833 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Tue, 25 Oct 2016 18:15:54 +0300 Subject: [PATCH] PY-21161 Don't suggest intention for string methods other than "format" --- .../codeInsight/intentions/PyConvertToFStringIntention.java | 3 ++- .../formatMethodWrongStringMethod.py | 1 + .../python/intentions/PyConvertToFStringIntentionTest.java | 4 ++++ 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodWrongStringMethod.py diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertToFStringIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertToFStringIntention.java index 5c8d677e5840..1bdc2dc7efcb 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertToFStringIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyConvertToFStringIntention.java @@ -27,6 +27,7 @@ import com.intellij.psi.PsiFile; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; import com.jetbrains.python.PyBundle; +import com.jetbrains.python.PyNames; import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.codeInsight.PySubstitutionChunkReference; import com.jetbrains.python.codeInsight.PythonFormattedStringReferenceProvider; @@ -180,7 +181,7 @@ public class PyConvertToFStringIntention extends PyBaseIntentionAction { final PyCallExpression callExpr = PsiTreeUtil.getParentOfType(anchor, PyCallExpression.class); if (callExpr != null) { final PyReferenceExpression callee = as(callExpr.getCallee(), PyReferenceExpression.class); - if (callee != null) { + if (callee != null && PyNames.FORMAT.equals(callee.getName())) { final PyStringLiteralExpression pyString = as(callee.getQualifier(), PyStringLiteralExpression.class); if (pyString != null) { return Pair.create(pyString, false); diff --git a/python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodWrongStringMethod.py b/python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodWrongStringMethod.py new file mode 100644 index 000000000000..91acdc360a29 --- /dev/null +++ b/python/testData/intentions/PyConvertToFStringIntentionTest/formatMethodWrongStringMethod.py @@ -0,0 +1 @@ +'no chunks inside'.upper() \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/intentions/PyConvertToFStringIntentionTest.java b/python/testSrc/com/jetbrains/python/intentions/PyConvertToFStringIntentionTest.java index c8f3ab629ec9..c79de4fd56aa 100644 --- a/python/testSrc/com/jetbrains/python/intentions/PyConvertToFStringIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/intentions/PyConvertToFStringIntentionTest.java @@ -100,6 +100,10 @@ public class PyConvertToFStringIntentionTest extends PyIntentionTestCase { doTest(); } + public void testFormatMethodWrongStringMethod() { + doNegativeTest(); + } + public void testExtractItemAndAttributeAccess() { assertSameElements(PyConvertToFStringIntention.extractItemsAndAttributes("{0.foo.bar.baz}"), ".foo", ".bar", ".baz"); assertSameElements(PyConvertToFStringIntention.extractItemsAndAttributes("{0[foo][.!:][}]}"), "[foo]", "[.!:]", "[}]");