PY-20744 Compatibility inspection and annotator warn about variable annotations

This commit is contained in:
Mikhail Golubev
2016-09-16 08:16:26 +03:00
parent eb64521ae2
commit 409aad3f71
7 changed files with 49 additions and 0 deletions
@@ -284,4 +284,8 @@ public class PyElementVisitor extends PsiElementVisitor {
public void visitPyTypeDeclarationStatement(PyTypeDeclarationStatement node) {
visitPyStatement(node);
}
public void visitPyAnnotation(PyAnnotation node) {
visitPyElement(node);
}
}
@@ -18,6 +18,7 @@ package com.jetbrains.python.psi.impl;
import com.intellij.lang.ASTNode;
import com.jetbrains.python.PyElementTypes;
import com.jetbrains.python.psi.PyAnnotation;
import com.jetbrains.python.psi.PyElementVisitor;
import com.jetbrains.python.psi.PyExpression;
import com.jetbrains.python.psi.stubs.PyAnnotationStub;
import org.jetbrains.annotations.Nullable;
@@ -39,4 +40,9 @@ public class PyAnnotationImpl extends PyBaseElementImpl<PyAnnotationStub> implem
public PyExpression getValue() {
return findChildByClass(PyExpression.class);
}
@Override
protected void acceptPyVisitor(PyElementVisitor pyVisitor) {
pyVisitor.visitPyAnnotation(this);
}
}
@@ -60,6 +60,21 @@ public abstract class CompatibilityVisitor extends PyAnnotator {
myVersionsToProcess = versionsToProcess;
}
@Override
public void visitPyAnnotation(PyAnnotation node) {
final PsiElement parent = node.getParent();
if (!(parent instanceof PyFunction || parent instanceof PyNamedParameter)) {
final StringBuilder message = new StringBuilder(myCommonMessage);
int len = 0;
for (LanguageLevel languageLevel : myVersionsToProcess) {
if (languageLevel.isOlderThan(LanguageLevel.PYTHON36)) {
len = appendLanguageLevel(message, len, languageLevel);
}
}
commonRegisterProblem(message, " not support variable annotations", len, node, null);
}
}
@Override
public void visitPyDictCompExpression(PyDictCompExpression node) {
super.visitPyDictCompExpression(node);
@@ -0,0 +1,8 @@
class C:
x<error descr="Python version 3.5 does not support variable annotations">: int</error>
y<error descr="Python version 3.5 does not support variable annotations">: None</error> = 42
def m(self, d):
x<error descr="Python version 3.5 does not support variable annotations">: List[bool]</error>
d['foo']<error descr="Python version 3.5 does not support variable annotations">: str</error>
(d['bar'])<error descr="Python version 3.5 does not support variable annotations">: float</error>
@@ -0,0 +1,8 @@
class C:
x<warning descr="Python version 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2, 3.3, 3.4, 3.5 do not support variable annotations">: int</warning>
y<warning descr="Python version 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2, 3.3, 3.4, 3.5 do not support variable annotations">: None</warning> = 42
def m(self, d):
x<warning descr="Python version 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2, 3.3, 3.4, 3.5 do not support variable annotations">: List[bool]</warning>
d['foo']<warning descr="Python version 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2, 3.3, 3.4, 3.5 do not support variable annotations">: str</warning>
(d['bar'])<warning descr="Python version 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2, 3.3, 3.4, 3.5 do not support variable annotations">: float</warning>
@@ -293,6 +293,10 @@ public class PythonHighlightingTest extends PyTestCase {
doTest(LanguageLevel.PYTHON35, true, false);
}
public void testVariableAnnotations() {
doTest(LanguageLevel.PYTHON35, true, false);
}
// ---
private void doTest(final LanguageLevel languageLevel, final boolean checkWarnings, final boolean checkInfos) {
PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), languageLevel);
@@ -190,6 +190,10 @@ public class PyCompatibilityInspectionTest extends PyTestCase {
doTest(LanguageLevel.PYTHON36);
}
public void testVariableAnnotations() {
doTest(LanguageLevel.PYTHON36);
}
private void doTest(@NotNull LanguageLevel level) {
runWithLanguageLevel(level, this::doTest);
}