PY-11274 Check right operand of format operator using abc.Mapping interface

This commit is contained in:
Mikhail Golubev
2014-08-13 14:27:10 +04:00
parent 7bf2cc51f1
commit f987c8f587
2 changed files with 20 additions and 3 deletions
@@ -41,6 +41,7 @@ import java.util.Map;
import static com.jetbrains.python.inspections.PyStringFormatParser.filterSubstitutions;
import static com.jetbrains.python.inspections.PyStringFormatParser.parsePercentFormat;
import static com.jetbrains.python.psi.PyUtil.as;
/**
* @author Alexey.Ivanov
@@ -416,9 +417,9 @@ public class PyStringFormatInspection extends PyInspection {
inspectValues(((PyParenthesizedExpression)rightExpression).getContainedExpression());
}
else {
final PyType type = myTypeEvalContext.getType(rightExpression);
final PyClassType type = as(myTypeEvalContext.getType(rightExpression), PyClassType.class);
if (type != null) {
if (myUsedMappingKeys.size() > 0 && !("dict".equals(type.getName()))) {
if (myUsedMappingKeys.size() > 0 && !PyABCUtil.isSubclass(type.getPyClass(), PyNames.MAPPING)) {
registerProblem(rightExpression, PyBundle.message("INSP.format.requires.mapping"));
return;
}
@@ -102,4 +102,20 @@ print '%d, %d, %d, %d' % my_tuple[:7:3]
print '%d %s' % ((42,) + ('spam',))
print '%d %s' % (('ham',) + ('spam',))
print '%d %s' % ((42,) + ())
print '%d' % ((42,) + ('spam',))
print '%d' % ((42,) + ('spam',))
# PY-11274
import collections
print '%(foo)s' % collections.OrderedDict(foo=None)
class MyDict(collections.Mapping):
def __getitem__(self, key):
return 'spam'
def __iter__(self):
yield 'spam'
def __len__(self):
return 1
print '%(foo)s' % MyDict()