mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
PY-11274 Check right operand of format operator using abc.Mapping interface
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user