mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[python] PY-80238 add completion for TypeAlias in type annotations
GitOrigin-RevId: e5f66e1fcf75b3eb42d06c5700d6b3b9de325a03
This commit is contained in:
committed by
intellij-monorepo-bot
parent
08243c85a4
commit
57904e8c2d
+8
-1
@@ -206,7 +206,7 @@ public final class PyClassNameCompletionContributor extends PyImportableNameComp
|
||||
// Not all names from typing.py are defined as classes
|
||||
return (
|
||||
definition instanceof PyClass ||
|
||||
definition instanceof PyTypeAliasStatement ||
|
||||
isSuitableTypeAlias(definition, context) ||
|
||||
ArrayUtil.contains(fqn.getFirstComponent(), "typing", "typing_extensions")
|
||||
);
|
||||
}
|
||||
@@ -216,6 +216,13 @@ public final class PyClassNameCompletionContributor extends PyImportableNameComp
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean isSuitableTypeAlias(@NotNull PyElement element, @NotNull TypeEvalContext context) {
|
||||
if (element instanceof PyTargetExpression && element.getParent() instanceof PyAssignmentStatement assignmentStatement) {
|
||||
if (PyTypingTypeProvider.isExplicitTypeAlias(assignmentStatement, context)) return true;
|
||||
}
|
||||
return element instanceof PyTypeAliasStatement;
|
||||
}
|
||||
|
||||
private static boolean isInsideErrorElement(@NotNull PyReferenceExpression referenceExpression) {
|
||||
return PsiTreeUtil.getParentOfType(referenceExpression, PsiErrorElement.class) != null;
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
x: NewStyle<caret>
|
||||
@@ -0,0 +1,3 @@
|
||||
type NewStyleString = str
|
||||
type NewStyleNumber = int | float
|
||||
type NewStyleUnion = str | int | None
|
||||
@@ -0,0 +1 @@
|
||||
x : OldStyle<caret>
|
||||
@@ -0,0 +1,5 @@
|
||||
from typing import TypeAlias
|
||||
|
||||
OldStyleString: TypeAlias = str
|
||||
OldStyleNumber: TypeAlias = int | float
|
||||
OldStyleUnion: TypeAlias = str | int | None
|
||||
@@ -199,6 +199,24 @@ public class PyClassNameCompletionTest extends PyTestCase {
|
||||
});
|
||||
}
|
||||
|
||||
// PY-80238
|
||||
public void testOldStyleTypeAliasCompletion() {
|
||||
myFixture.copyDirectoryToProject(getTestName(true), "");
|
||||
myFixture.configureByFile("a.py");
|
||||
myFixture.complete(CompletionType.BASIC, 1);
|
||||
List<String> variants = myFixture.getLookupElementStrings();
|
||||
assertContainsElements(variants, "OldStyleString", "OldStyleNumber", "OldStyleUnion");
|
||||
}
|
||||
|
||||
// PY-80238
|
||||
public void testNewTypeAliasCompletion() {
|
||||
myFixture.copyDirectoryToProject(getTestName(true), "");
|
||||
myFixture.configureByFile("a.py");
|
||||
myFixture.complete(CompletionType.BASIC, 1);
|
||||
List<String> variants = myFixture.getLookupElementStrings();
|
||||
assertContainsElements(variants, "NewStyleString", "NewStyleNumber", "NewStyleUnion");
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String getElementQualifiedName(@NotNull LookupElement le) {
|
||||
PsiElement element = le.getPsiElement();
|
||||
|
||||
Reference in New Issue
Block a user