diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java index 249e776f8bd6..42b79a66560c 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/PyStringConcatenationToFormatIntention.java @@ -15,8 +15,7 @@ import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.PythonStringUtil; import com.jetbrains.python.psi.*; import com.jetbrains.python.psi.impl.PyBuiltinCache; -import com.jetbrains.python.psi.types.PyTypeChecker; -import com.jetbrains.python.psi.types.TypeEvalContext; +import com.jetbrains.python.psi.types.*; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; @@ -56,8 +55,9 @@ public class PyStringConcatenationToFormatIntention extends BaseIntentionAction return false; } final boolean isStringLiteral = expression instanceof PyStringLiteralExpression; + final PyType type = expression.getType(TypeEvalContext.slow()); final boolean isStringReference = PyTypeChecker.match(cache.getStringType(LanguageLevel.forElement(expression)), - expression.getType(TypeEvalContext.fast()), TypeEvalContext.fast()); + type, TypeEvalContext.slow()) && type != null; if (!(isStringLiteral || ((expression instanceof PyReferenceExpression || expression instanceof PyCallExpression) && isStringReference))) { return false; diff --git a/python/testData/intentions/afterStringConcatToFormat.py b/python/testData/intentions/afterStringConcatToFormat.py index 4ed8d1c6b41d..66e2b1ff68ce 100644 --- a/python/testData/intentions/afterStringConcatToFormat.py +++ b/python/testData/intentions/afterStringConcatToFormat.py @@ -1 +1,4 @@ +def foo(): + return "foo" +c = "string" "a%sf_str%s" % (foo(), c) \ No newline at end of file diff --git a/python/testData/intentions/afterStringConcatToFormat1.py b/python/testData/intentions/afterStringConcatToFormat1.py index e2618d480e2a..905a1680001a 100644 --- a/python/testData/intentions/afterStringConcatToFormat1.py +++ b/python/testData/intentions/afterStringConcatToFormat1.py @@ -1 +1,3 @@ +header = "header" +value = "value" result += "%s : %s\n" % (header, value) \ No newline at end of file diff --git a/python/testData/intentions/afterUnicodeStringConcatToFormat.py b/python/testData/intentions/afterUnicodeStringConcatToFormat.py index ffb33d38a03b..4e4897ebc584 100644 --- a/python/testData/intentions/afterUnicodeStringConcatToFormat.py +++ b/python/testData/intentions/afterUnicodeStringConcatToFormat.py @@ -1 +1,4 @@ +def foo(): + return "foo" + u'a%sf' % foo() \ No newline at end of file diff --git a/python/testData/intentions/beforeStringConcatToFormat.py b/python/testData/intentions/beforeStringConcatToFormat.py index dcbdcd8e28e8..bc0102979c18 100644 --- a/python/testData/intentions/beforeStringConcatToFormat.py +++ b/python/testData/intentions/beforeStringConcatToFormat.py @@ -1 +1,4 @@ +def foo(): + return "foo" +c = "string" "a" + foo() + "f" + "_str" + c \ No newline at end of file diff --git a/python/testData/intentions/beforeStringConcatToFormat1.py b/python/testData/intentions/beforeStringConcatToFormat1.py index d978be40173a..080e87d38703 100644 --- a/python/testData/intentions/beforeStringConcatToFormat1.py +++ b/python/testData/intentions/beforeStringConcatToFormat1.py @@ -1 +1,3 @@ +header = "header" +value = "value" result += header + " : " + value + "\n" \ No newline at end of file diff --git a/python/testData/intentions/beforeStringConcatToFormat4.py b/python/testData/intentions/beforeStringConcatToFormat4.py new file mode 100644 index 000000000000..d5f91faf3162 --- /dev/null +++ b/python/testData/intentions/beforeStringConcatToFormat4.py @@ -0,0 +1,3 @@ +def foo3(x, y, z): + i = x + y + z + return i \ No newline at end of file diff --git a/python/testData/intentions/beforeUnicodeStringConcatToFormat.py b/python/testData/intentions/beforeUnicodeStringConcatToFormat.py index 581ccc7e9e18..1eaa9dd28950 100644 --- a/python/testData/intentions/beforeUnicodeStringConcatToFormat.py +++ b/python/testData/intentions/beforeUnicodeStringConcatToFormat.py @@ -1 +1,4 @@ +def foo(): + return "foo" + u'a' + foo() + u'f' \ 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 7b92102469b9..9ad75e13bf23 100644 --- a/python/testSrc/com/jetbrains/python/PyIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/PyIntentionTest.java @@ -125,6 +125,10 @@ public class PyIntentionTest extends PyTestCase { doTest(PyBundle.message("INTN.replace.plus.with.format.operator")); } + public void testStringConcatToFormat4() { //PY-7968 + doNegativeTest(PyBundle.message("INTN.replace.plus.with.format.operator")); + } + public void testConvertFormatOperatorToMethod() { doTest(PyBundle.message("INTN.replace.with.method"), LanguageLevel.PYTHON26); }