PY-61856 PEP 695 Type Parameter Syntax: Code compatibility inspection and unsupported features annotator

GitOrigin-RevId: 164a07daff1059ac1a421e1a883168924f82559e
This commit is contained in:
Daniil Kalinin
2023-09-28 15:05:34 +00:00
committed by intellij-monorepo-bot
parent 97185d17c8
commit 9249e0a15c
8 changed files with 49 additions and 2 deletions
@@ -791,6 +791,7 @@ INSP.compatibility.check.for.compatibility.with.python.versions=Check for compat
INSP.compatibility.inspection.unsupported.feature.prefix=Python {0,choice,1#version|2#versions} {1} {0,choice,1#does|2#do} not {2}
INSP.compatibility.feature.support.variable.annotations=support variable annotations
INSP.compatibility.feature.support.this.syntax=support this syntax
INSP.compatibility.feature.support.type.alias.statements=support type alias statements
INSP.compatibility.feature.have.module.builtins=have module builtins
INSP.compatibility.feature.have.module.builtin=have module __builtin__
INSP.compatibility.feature.support.starred.expressions.as.assignment.targets=support starred expressions as assignment targets
@@ -747,6 +747,23 @@ public abstract class CompatibilityVisitor extends PyAnnotator {
matchStatement.getFirstChild());
}
@Override
public void visitPyTypeAliasStatement(@NotNull PyTypeAliasStatement node) {
registerForAllMatchingVersions(level -> level.isOlderThan(LanguageLevel.PYTHON312),
PyPsiBundle.message("INSP.compatibility.feature.support.type.alias.statements"),
node);
}
@Override
public void visitPyTypeParameterList(@NotNull PyTypeParameterList node) {
// No need to report an error inside the statement which is already reported as unsupported
if (!(node.getParent() instanceof PyTypeAliasStatement)) {
registerForAllMatchingVersions(level -> level.isOlderThan(LanguageLevel.PYTHON312),
PyPsiBundle.message("INSP.compatibility.feature.support.this.syntax"),
node);
}
}
private void checkBitwiseOrUnionSyntax(@NotNull PyBinaryExpression node) {
if (node.getOperator() != PyTokenTypes.OR) return;
@@ -0,0 +1 @@
<error descr="Python version 3.11 does not support type alias statements"><info descr="null">type</info> myType[T] = list[T]</error>
@@ -0,0 +1,3 @@
class <info descr="PY.CLASS_DEFINITION">Class</info><error descr="Python version 3.11 does not support this syntax">[T]</error>:
def <info descr="PY.FUNC_DEFINITION">foo</info><error descr="Python version 3.11 does not support this syntax">[U]</error>(<info descr="PY.SELF_PARAMETER">x</info>: <info descr="PY.ANNOTATION">U</info>):
pass
@@ -0,0 +1 @@
<error descr="Python version 3.11 does not support type alias statements" textAttributesKey="ERRORS_ATTRIBUTES"><warning descr="Python versions 2.7, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10, 3.11 do not support type alias statements" textAttributesKey="WARNING_ATTRIBUTES">type MyType = str</warning></error>
@@ -0,0 +1,3 @@
class Clazz<error descr="Python version 3.11 does not support this syntax" textAttributesKey="ERRORS_ATTRIBUTES"><warning descr="Python versions 2.7, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10, 3.11 do not support this syntax" textAttributesKey="WARNING_ATTRIBUTES">[T]</warning></error>:
def foo<error descr="Python version 3.11 does not support this syntax" textAttributesKey="ERRORS_ATTRIBUTES"><warning descr="Python versions 2.7, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10, 3.11 do not support this syntax" textAttributesKey="WARNING_ATTRIBUTES">[U]</warning></error>(self):
pass
@@ -580,16 +580,26 @@ public class PythonHighlightingTest extends PyTestCase {
doTest(LanguageLevel.getLatest(), false, true);
}
// PY-60767
// PY-61856
public void testTypeAliasStatement() {
doTest(LanguageLevel.PYTHON312, false, true);
}
// PY-60767
// PY-61856
public void testTypeParameter() {
doTest(LanguageLevel.PYTHON312, false, true);
}
// PY-61856
public void testTypeAliasStatementBefore312() {
doTest(LanguageLevel.PYTHON311, false, true);
}
// PY-61856
public void testTypeParameterListBefore312() {
doTest(LanguageLevel.PYTHON311, false, true);
}
@NotNull
private static EditorColorsScheme createTemporaryColorScheme() {
EditorColorsManager manager = EditorColorsManager.getInstance();
@@ -270,6 +270,17 @@ public class PyCompatibilityInspectionTest extends PyInspectionTestCase {
doTest(LanguageLevel.PYTHON311);
}
// PY-60767
public void testTypeAliasStatements() {
doTest(LanguageLevel.PYTHON311);
}
// PY-60767
public void testTypeParameterLists() {
doTest(LanguageLevel.PYTHON311);
}
private void doTest(@NotNull LanguageLevel level) {
runWithLanguageLevel(level, this::doTest);
}