PY-20844 PY-20773 Warn about '#' signs and backslashes inside f-strings

This commit is contained in:
Mikhail Golubev
2016-09-26 09:09:23 +03:00
parent de6feb7cc5
commit 654ac30429
4 changed files with 29 additions and 0 deletions
@@ -49,6 +49,15 @@ public class FStringsAnnotator extends PyAnnotator {
if (fragment.getRightBraceOffset() == -1) {
hasUnclosedBrace = true;
}
for (int i = fragment.getLeftBraceOffset() + 1; i < fragment.getContentEndOffset(); i++) {
final char c = nodeText.charAt(i);
if (c == '\\') {
reportCharacter(nodeOffset + i, "Expression fragments inside f-strings cannot include backslashes");
}
else if (c == '#') {
reportCharacter(nodeOffset + i, "Expressions fragments inside f-strings cannot include '#'");
}
}
// Do not warn about illegal conversion character if '!' is right before closing quotes
if (fragContentEnd < nodeContentEnd && nodeText.charAt(fragContentEnd) == '!' && fragContentEnd + 1 < nodeContentEnd) {
final char conversionChar = nodeText.charAt(fragContentEnd + 1);
@@ -0,0 +1,5 @@
f'{<error descr="Expression fragments inside f-strings cannot include backslashes">\</error>t}'
f'{<error descr="Expression fragments inside f-strings cannot include backslashes">\</error>t<error descr="'}' is expected"></error>'
f'{<error descr="Expression fragments inside f-strings cannot include backslashes">\</error>N{GREEK SMALL LETTER ALPHA}}'
f'{Formatable():\n\t}'
f'{42:{<error descr="Expression fragments inside f-strings cannot include backslashes">\</error>t}}'
@@ -0,0 +1,5 @@
f'{<error descr="Expressions fragments inside f-strings cannot include '#'">#<error descr="'}' is expected"></error></error>'
<error descr="Missing closing quote [']">f'{<error descr="Expressions fragments inside f-strings cannot include '#'">#<error descr="'}' is expected"></error></error></error>
f'{<error descr="Expressions fragments inside f-strings cannot include '#'">#</error>foo<error descr="Expressions fragments inside f-strings cannot include '#'">#</error>}'
f'{42:#}'
f'{42:{<error descr="Expressions fragments inside f-strings cannot include '#'">#</error>}}'
@@ -320,6 +320,16 @@ public class PythonHighlightingTest extends PyTestCase {
runWithLanguageLevel(LanguageLevel.PYTHON36, () -> doTest(true, false));
}
// PY-20773
public void testFStringHashSigns() {
runWithLanguageLevel(LanguageLevel.PYTHON36, () -> doTest(true, false));
}
// PY-20844
public void testFStringBackslashes() {
runWithLanguageLevel(LanguageLevel.PYTHON36, () -> doTest(true, false));
}
// ---
private void doTest(final LanguageLevel languageLevel, final boolean checkWarnings, final boolean checkInfos) {
PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), languageLevel);