fixed PY-7969 Replace + with string formatting operator: disable intention for expression with undefined types

This commit is contained in:
Ekaterina Tuzova
2012-11-08 17:52:16 +04:00
parent 723180f142
commit 2c025a3237
9 changed files with 26 additions and 3 deletions
@@ -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;
@@ -1 +1,4 @@
def foo():
return "foo"
c = "string"
"a%sf_str%s" % (foo(), c)
@@ -1 +1,3 @@
header = "header"
value = "value"
result += "%s : %s\n" % (header, value)
@@ -1 +1,4 @@
def foo():
return "foo"
u'a%sf' % foo()
@@ -1 +1,4 @@
def foo():
return "foo"
c = "string"
"a" + foo() <caret>+ "f" + "_str" + c
@@ -1 +1,3 @@
header = "header"
value = "value"
result += header<caret> + " : " + value + "\n"
@@ -0,0 +1,3 @@
def foo3(x, y, z):
i = x + <caret>y + z
return i
@@ -1 +1,4 @@
def foo():
return "foo"
u'a' + foo() <caret>+ u'f'
@@ -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);
}