PY-33218 Don't warn about %-formatting argument type at tuple declaration site

that might be far away from the place where the corresponding formatting
operation was actually used.
This commit is contained in:
Mikhail Golubev
2019-01-09 19:48:39 +03:00
parent f71913288d
commit 85cf94e7d9
3 changed files with 24 additions and 9 deletions
@@ -135,20 +135,28 @@ public class PyStringFormatInspection extends PyInspection {
else if (rightExpression instanceof PyParenthesizedExpression) {
final PyExpression rhs = ((PyParenthesizedExpression)rightExpression).getContainedExpression();
if (rhs != null) {
return inspectArguments(rhs, rhs);
return inspectArguments(rhs, problemTarget);
}
}
else if (rightExpression instanceof PyTupleExpression) {
final PyExpression[] expressions = ((PyTupleExpression)rightExpression).getElements();
int i = 1;
for (PyExpression expression : expressions) {
final String formatSpec = myFormatSpec.get(Integer.toString(i));
if (formatSpec != null) {
checkExpressionType(expression, formatSpec, expression);
if (PsiTreeUtil.isAncestor(problemTarget, rightExpression, false)) {
final PyExpression[] expressions = ((PyTupleExpression)rightExpression).getElements();
int i = 1;
for (PyExpression expression : expressions) {
final String formatSpec = myFormatSpec.get(Integer.toString(i));
if (formatSpec != null) {
checkExpressionType(expression, formatSpec, expression);
}
++i;
}
++i;
return expressions.length;
}
else {
final PyTupleType tupleType = (PyTupleType)myTypeEvalContext.getType(rightExpression);
assert tupleType != null;
matchEntireTupleTypes(problemTarget, tupleType);
return tupleType.getElementCount();
}
return expressions.length;
}
else if (rightExpression instanceof PyDictLiteralExpression) {
return inspectDict(rightExpression, problemTarget, false);
@@ -0,0 +1,2 @@
args = ('foo', 'bar')
s = '%d %d' % <warning descr="Unexpected type (str, str)">args</warning>
@@ -219,6 +219,11 @@ public class PyStringFormatInspectionTest extends PyInspectionTestCase {
doTest();
}
// PY-33218
public void testNoTypeMismatchOnElementsOfTupleDeclaration() {
doTest();
}
@NotNull
@Override
protected Class<? extends PyInspection> getInspectionClass() {