From a52bcf86ff3ed19b071ec740f4828f213737f9d4 Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Tue, 19 Feb 2013 16:06:02 +0400 Subject: [PATCH] used proper type check fix a PY-6756 PyCharm erroneously reports "too many arguments" for certain string formatting lines. --- .../python/inspections/PyStringFormatInspection.java | 6 +++++- .../inspections/PyStringFormatInspectionSlice/test.py | 9 +++++++++ .../com/jetbrains/python/PythonInspectionsTest.java | 4 ++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 python/testData/inspections/PyStringFormatInspectionSlice/test.py 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);