Fix PY-20599 False positive for %a format specifier

This commit is contained in:
Valentina Kiryushkina
2017-05-15 14:37:53 +03:00
parent 03a05fc2eb
commit caad632837
5 changed files with 24 additions and 2 deletions
@@ -83,6 +83,7 @@ public class PyStringFormatInspection extends PyInspection {
.put('G', "float")
.put('c', "str")
.put('r', "str")
.put('a', "str")
.put('s', "str")
.put('b', "bytes")
.build();
@@ -402,7 +403,8 @@ public class PyStringFormatInspection extends PyInspection {
return;
}
}
if (PERCENT_FORMAT_CONVERSIONS.containsKey(conversionType)) {
final LanguageLevel languageLevel = LanguageLevel.forElement(formatExpression);
if (PERCENT_FORMAT_CONVERSIONS.containsKey(conversionType) && !(!languageLevel.isPy3K() && conversionType == 'a')) {
myFormatSpec.put(mappingKey, PERCENT_FORMAT_CONVERSIONS.get(conversionType));
continue;
}
@@ -271,7 +271,7 @@ public class PyStringFormatParser {
private static final String CONVERSION_FLAGS = "#0- +";
private static final String DIGITS = "0123456789";
private static final String LENGTH_MODIFIERS = "hlL";
private static final String VALID_CONVERSION_TYPES = "diouxXeEfFgGcrsb";
private static final String VALID_CONVERSION_TYPES = "diouxXeEfFgGcrsba";
// new style strings
private static final String ALIGN_SYMBOLS = "<>=^";
@@ -0,0 +1,2 @@
"foo %a" % "foo"
<warning descr="Format specifier character missing">"foo %q"</warning> % "foo"
@@ -552,4 +552,11 @@ public class PyStringFormatParserTest extends TestCase {
assertEquals("a", chunk.getMappingKeyElementIndex());
assertEquals('d', chunk.getConversionType());
}
public void testAsciiFormatSpecifierOldStyleFormat() {
final List<FormatStringChunk> chunks = parsePercentFormat("%a");
assertEquals(1, chunks.size());
final PercentSubstitutionChunk chunk = (PercentSubstitutionChunk)chunks.get(0);
assertEquals('a', chunk.getConversionType());
}
}
@@ -16,6 +16,7 @@
package com.jetbrains.python.inspections;
import com.jetbrains.python.fixtures.PyTestCase;
import com.jetbrains.python.psi.LanguageLevel;
/**
* @author vlan
@@ -188,6 +189,16 @@ public class PyStringFormatInspectionTest extends PyTestCase {
doTest();
}
// PY-20599
public void testPy3kAsciiFormatSpecifier() {
runWithLanguageLevel(LanguageLevel.PYTHON35, new Runnable() {
@Override
public void run() {
doTest();
}
});
}
private void doTest() {
myFixture.configureByFile(TEST_DIRECTORY + getTestName(false) + ".py");
myFixture.enableInspections(PyStringFormatInspection.class);