mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 04:51:24 +07:00
PY-70528 Refactor handling of the *Ts and Unpack[Ts] syntax in type hints
to have better code re-use between PEP 692 and PEP 646 implementations. GitOrigin-RevId: 39d714fcb14ba1014099fd57325c5185df34ce45
This commit is contained in:
committed by
intellij-monorepo-bot
parent
93ac5fe566
commit
46d7223d9f
@@ -1619,22 +1619,14 @@ public final class PyTypingTypeProvider extends PyTypeProviderWithCustomContext<
|
||||
|
||||
@Nullable
|
||||
public static PyVariadicType getUnpackedType(@NotNull PsiElement element, @NotNull TypeEvalContext context) {
|
||||
PyExpression typeHint;
|
||||
if (element instanceof PyStarExpression starExpression) {
|
||||
typeHint = starExpression.getExpression();
|
||||
Ref<@Nullable PyType> typeRef = getTypeFromStarExpression(element, context);
|
||||
if (typeRef == null) {
|
||||
typeRef = getTypeFromUnpackOperator(element, context);
|
||||
}
|
||||
else if (element instanceof PySubscriptionExpression subscription &&
|
||||
resolvesToQualifiedNames(subscription.getOperand(), context, UNPACK, UNPACK_EXT)) {
|
||||
typeHint = subscription.getIndexExpression();
|
||||
}
|
||||
else {
|
||||
if (typeRef == null) {
|
||||
return null;
|
||||
}
|
||||
if (!(typeHint instanceof PyReferenceExpression) && !(typeHint instanceof PySubscriptionExpression)) return null;
|
||||
var typeRef = getType(typeHint, context);
|
||||
if (typeRef == null) return null;
|
||||
var expressionType = typeRef.get();
|
||||
|
||||
if (expressionType instanceof PyTupleType tupleType) {
|
||||
return new PyUnpackedTupleTypeImpl(tupleType.getElementTypes(), tupleType.isHomogeneous());
|
||||
}
|
||||
@@ -1644,17 +1636,23 @@ public final class PyTypingTypeProvider extends PyTypeProviderWithCustomContext<
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static Ref<@Nullable PyType> getTypeFromUnpackOperator(@NotNull PsiElement element, @NotNull TypeEvalContext context) {
|
||||
if (!(element instanceof PySubscriptionExpression subscriptionExpr)) return null;
|
||||
if (!resolvesToQualifiedNames(subscriptionExpr.getOperand(), context, UNPACK, UNPACK_EXT)) {
|
||||
private static @Nullable Ref<@Nullable PyType> getTypeFromUnpackOperator(@NotNull PsiElement element, @NotNull TypeEvalContext context) {
|
||||
if (!(element instanceof PySubscriptionExpression subscriptionExpr) ||
|
||||
!resolvesToQualifiedNames(subscriptionExpr.getOperand(), context, UNPACK, UNPACK_EXT)) {
|
||||
return null;
|
||||
}
|
||||
PyExpression indexExpression = subscriptionExpr.getIndexExpression();
|
||||
if (indexExpression == null) return null;
|
||||
if (!(indexExpression instanceof PyReferenceExpression || indexExpression instanceof PySubscriptionExpression)) return null;
|
||||
return Ref.create(Ref.deref(getType(indexExpression, context)));
|
||||
}
|
||||
|
||||
private static @Nullable Ref<@Nullable PyType> getTypeFromStarExpression(@NotNull PsiElement element, @NotNull TypeEvalContext context) {
|
||||
if (!(element instanceof PyStarExpression starExpression)) return null;
|
||||
PyExpression starredExpression = starExpression.getExpression();
|
||||
if (!(starredExpression instanceof PyReferenceExpression || starredExpression instanceof PySubscriptionExpression)) return null;
|
||||
return Ref.create(Ref.deref(getType(starredExpression, context)));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PyParamSpecType getParamSpecType(@NotNull PsiElement element, @NotNull Context context) {
|
||||
if (!(element instanceof PyCallExpression assignedCall)) return null;
|
||||
|
||||
Reference in New Issue
Block a user