From 22f877a9ed093522b4e415dd5d7a70cbcbf5b6b3 Mon Sep 17 00:00:00 2001 From: Valentina Kiryushkina Date: Sat, 22 Oct 2016 12:45:41 +0300 Subject: [PATCH] PY-21166 No inspection if unknown format code is used in formatted string --- python/src/com/jetbrains/python/PyBundle.properties | 1 + .../inspections/PyStringFormatInspection.java | 13 +++++++++---- .../python/inspections/PyStringFormatParser.java | 2 +- .../UnsupportedFormatSpecifierNewStyleFormatting.py | 1 + .../inspections/PyStringFormatInspectionTest.java | 7 ++++++- 5 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 python/testData/inspections/PyStringFormatInspection/UnsupportedFormatSpecifierNewStyleFormatting.py diff --git a/python/src/com/jetbrains/python/PyBundle.properties b/python/src/com/jetbrains/python/PyBundle.properties index 5243385f3904..b2de5b9433e4 100644 --- a/python/src/com/jetbrains/python/PyBundle.properties +++ b/python/src/com/jetbrains/python/PyBundle.properties @@ -391,6 +391,7 @@ INSP.too.many.args.for.fmt.string=Too many arguments for format string INSP.too.few.args.for.fmt.string=Too few arguments for format string INSP.incompatible.options=The format options in chunk "{0}" are incompatible INSP.unused.mapping = Mapping key "{0}" is unused +INSP.unsupported.format.character=Unsupported format character ''{0}'' # PyMethodOverridingInspection INSP.NAME.method.over=Method signature does not match signature of overridden method diff --git a/python/src/com/jetbrains/python/inspections/PyStringFormatInspection.java b/python/src/com/jetbrains/python/inspections/PyStringFormatInspection.java index 98f64e4b6a8d..2131eb7d8d29 100644 --- a/python/src/com/jetbrains/python/inspections/PyStringFormatInspection.java +++ b/python/src/com/jetbrains/python/inspections/PyStringFormatInspection.java @@ -522,10 +522,15 @@ public class PyStringFormatInspection extends PyInspection { } final char conversionType = chunk.getConversionType(); - if (NEW_STYLE_FORMAT_CONVERSIONS.containsKey(conversionType)) { - final String[] s = NEW_STYLE_FORMAT_CONVERSIONS.get(conversionType).split(" or "); - addTypes(types, Arrays.asList(s)); - hasTypeOptions = true; + if (conversionType != Character.MIN_VALUE) { + if (NEW_STYLE_FORMAT_CONVERSIONS.containsKey(conversionType)) { + final String[] s = NEW_STYLE_FORMAT_CONVERSIONS.get(conversionType).split(" or "); + addTypes(types, Arrays.asList(s)); + hasTypeOptions = true; + } + else { + registerProblem(myFormatExpression, PyBundle.message("INSP.unsupported.format.character", conversionType)); + } } if (!types.isEmpty()) { diff --git a/python/src/com/jetbrains/python/inspections/PyStringFormatParser.java b/python/src/com/jetbrains/python/inspections/PyStringFormatParser.java index e074960f0f52..b93f594e5ffe 100644 --- a/python/src/com/jetbrains/python/inspections/PyStringFormatParser.java +++ b/python/src/com/jetbrains/python/inspections/PyStringFormatParser.java @@ -451,7 +451,7 @@ public class PyStringFormatParser { chunk.setPrecision(parseWhileCharacterInSet(DIGITS)); } - if (isAtSet(NEW_STYLE_CONVERSION_TYPES)) { + if (myPos < end - 1) { chunk.setConversionType(myLiteral.charAt(myPos)); } } diff --git a/python/testData/inspections/PyStringFormatInspection/UnsupportedFormatSpecifierNewStyleFormatting.py b/python/testData/inspections/PyStringFormatInspection/UnsupportedFormatSpecifierNewStyleFormatting.py new file mode 100644 index 000000000000..907a44142ba4 --- /dev/null +++ b/python/testData/inspections/PyStringFormatInspection/UnsupportedFormatSpecifierNewStyleFormatting.py @@ -0,0 +1 @@ +print('{:+q}; {:+f}'.format(3.14, -3.14)) \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/inspections/PyStringFormatInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyStringFormatInspectionTest.java index 6778337d9903..0b992b9d6fb1 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyStringFormatInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyStringFormatInspectionTest.java @@ -203,7 +203,12 @@ public class PyStringFormatInspectionTest extends PyTestCase { } }); } - + + //PY-21166 + public void testUnsupportedFormatSpecifierNewStyleFormatting() { + doTest(); + } + private void doTest() { myFixture.configureByFile(TEST_DIRECTORY + getTestName(false) + ".py"); myFixture.enableInspections(PyStringFormatInspection.class);