diff --git a/python/src/com/jetbrains/python/inspections/PyStringFormatInspection.java b/python/src/com/jetbrains/python/inspections/PyStringFormatInspection.java index b7c46583b787..15740c15c28b 100644 --- a/python/src/com/jetbrains/python/inspections/PyStringFormatInspection.java +++ b/python/src/com/jetbrains/python/inspections/PyStringFormatInspection.java @@ -183,7 +183,11 @@ public class PyStringFormatInspection extends PyInspection { } else if (rightExpression instanceof PySliceExpression && s != null) { final PyType type = myTypeEvalContext.getType(((PySliceExpression)rightExpression).getOperand()); - if (type == null || "list".equals(type.getName()) || "str".equals(type.getName())) { + final PyType stringType = PyBuiltinCache.getInstance(rightExpression).getStringType(LanguageLevel.forElement(rightExpression)); + final PyType listType = PyBuiltinCache.getInstance(rightExpression).getListType(); + + if (type == null || PyTypeChecker.match(listType, type, myTypeEvalContext) + || PyTypeChecker.match(stringType, type, myTypeEvalContext)) { checkTypeCompatible(problemTarget, builtinCache.getStrType(), PyTypeParser.getTypeByName(problemTarget, s)); return 1; diff --git a/python/testData/inspections/PyStringFormatInspectionSlice/test.py b/python/testData/inspections/PyStringFormatInspectionSlice/test.py new file mode 100644 index 000000000000..8ef36b1ed699 --- /dev/null +++ b/python/testData/inspections/PyStringFormatInspectionSlice/test.py @@ -0,0 +1,9 @@ +def foo(x): + return x + +artist = foo(1) +print('%s' % (artist.lower()[0:10])) + +unicode_content = foo(u"test") +assert isinstance(unicode_content, unicode) +print '%s...' % unicode_content[:200] diff --git a/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java b/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java index 8af7f625bd8d..73a820b1c342 100644 --- a/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java +++ b/python/testSrc/com/jetbrains/python/PythonInspectionsTest.java @@ -266,6 +266,10 @@ public class PythonInspectionsTest extends PyTestCase { doHighlightingTest(PyStringFormatInspection.class); } + public void testPyStringFormatInspectionSlice() { //PY-6756 + doHighlightingTest(PyStringFormatInspection.class); + } + public void testPyUnnecessaryBackslashInspection() { //PY-2952 setLanguageLevel(LanguageLevel.PYTHON27); doHighlightingTest(PyUnnecessaryBackslashInspection.class);