Highlight bytes and non-bytes literals in Python 3+ (PY-32321)

This commit is contained in:
Semyon Proshev
2018-11-06 17:15:10 +03:00
parent da48486106
commit 7a0b54426e
4 changed files with 25 additions and 2 deletions
@@ -216,10 +216,16 @@ public abstract class CompatibilityVisitor extends PyAnnotator {
public void visitPyStringLiteralExpression(final PyStringLiteralExpression node) {
super.visitPyStringLiteralExpression(node);
boolean seenBytes = false;
boolean seenNonBytes = false;
for (PyStringElement element : node.getStringElements()) {
final String prefix = element.getPrefix().toUpperCase();
if (prefix.isEmpty()) continue;
final boolean bytes = element.isBytes();
seenBytes |= bytes;
seenNonBytes |= !bytes;
final int elementStart = element.getTextOffset();
registerForAllMatchingVersions(level -> !getSupportedStringPrefixes(level).contains(prefix),
" not support a '" + prefix + "' prefix",
@@ -228,6 +234,13 @@ public abstract class CompatibilityVisitor extends PyAnnotator {
new RemovePrefixQuickFix(prefix),
true);
}
if (seenBytes && seenNonBytes) {
registerForAllMatchingVersions(LanguageLevel::isPy3K,
" not allow to mix bytes and non-bytes literals",
node,
null);
}
}
@NotNull
@@ -0,0 +1,3 @@
<error descr="Python version 3.6 does not allow to mix bytes and non-bytes literals">b'qwe'u'asd'</error>
<error descr="Python version 3.6 does not allow to mix bytes and non-bytes literals">b'qwe'r'asd'</error>
<error descr="Python version 3.6 does not allow to mix bytes and non-bytes literals">b'qwe'f'asd'</error>
@@ -45,8 +45,10 @@ a = u""
a = r""
a = <warning descr="Python version 3.4, 3.5, 3.6, 3.7 do not support a 'UR' prefix">ur</warning>""
# combined
b = u"" b""
# combined, PY-32321
b = <warning descr="Python version 3.4, 3.5, 3.6, 3.7 do not allow to mix bytes and non-bytes literals">u"" b""</warning>
b = <warning descr="Python version 3.4, 3.5, 3.6, 3.7 do not allow to mix bytes and non-bytes literals">r"" b""</warning>
b = <warning descr="Python version 3.4, 3.5, 3.6, 3.7 do not allow to mix bytes and non-bytes literals">f"" b""</warning>
# never was available
a = <error descr="Python version 2.7 does not support a 'RR' prefix"><warning descr="Python version 2.6, 2.7, 3.4, 3.5, 3.6, 3.7 do not support a 'RR' prefix">rr</warning></error>""
@@ -411,6 +411,11 @@ public class PythonHighlightingTest extends PyTestCase {
doTest(LanguageLevel.PYTHON36, false, false);
}
// PY-32321
public void testMixedBytesAndNonBytes() {
doTest(LanguageLevel.PYTHON36, false, false);
}
@NotNull
private static EditorColorsScheme createTemporaryColorScheme() {
EditorColorsManager manager = EditorColorsManager.getInstance();