PY-73246 Don't insert square brackets after classes not having free type parameters

Even if they transitively extend typing.Generic, such as the builtin str.

GitOrigin-RevId: 9389ef5846e21ba5e0fed8b835beb2d458e42f13
This commit is contained in:
Mikhail Golubev
2024-06-14 18:26:10 +05:00
committed by intellij-monorepo-bot
parent ebba681c85
commit eb63148798
4 changed files with 24 additions and 3 deletions

View File

@@ -19,7 +19,7 @@ public class PyParameterizedTypeInsertHandler extends ParenthesesInsertHandler<L
public static boolean isCompletingParameterizedType(@NotNull PsiElement definition,
@NotNull PsiElement completionPosition,
@NotNull TypeEvalContext context) {
return PyTypingTypeProvider.isInsideTypeHint(completionPosition, context) && isParameterizedType(definition, context);
return PyTypingTypeProvider.isInsideTypeHint(completionPosition, context) && isParameterizableType(definition, context);
}
public static final PyParameterizedTypeInsertHandler INSTANCE = new PyParameterizedTypeInsertHandler();
@@ -33,8 +33,8 @@ public class PyParameterizedTypeInsertHandler extends ParenthesesInsertHandler<L
return true;
}
private static boolean isParameterizedType(@NotNull PsiElement element, @NotNull TypeEvalContext typeEvalContext) {
private static boolean isParameterizableType(@NotNull PsiElement element, @NotNull TypeEvalContext typeEvalContext) {
return element instanceof PyQualifiedNameOwner qnOwner && PyTypingTypeProvider.GENERIC_CLASSES.contains(qnOwner.getQualifiedName()) ||
element instanceof PyClass pyClass && PyTypingTypeProvider.isGeneric(pyClass, typeEvalContext);
element instanceof PyClass pyClass && new PyTypingTypeProvider().getGenericType(pyClass, typeEvalContext) != null;
}
}

View File

@@ -0,0 +1,8 @@
from typing import Iterable
class StrIterable(Iterable[str]):
pass
x: StrIterable

View File

@@ -0,0 +1,8 @@
from typing import Iterable
class StrIterable(Iterable[str]):
pass
x: StrIter<caret>

View File

@@ -743,6 +743,11 @@ public class Py3CompletionTest extends PyTestCase {
doMultiFileTest();
}
// PY-73246
public void testSquareBracketsNotInsertedAfterAlreadyParameterizedGenericInsideTypeHints() {
doMultiFileTest();
}
private void doTestVariants(String @NotNull ... expected) {
final String testName = getTestName(true);
myFixture.configureByFile(testName + ".py");