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:
Mikhail Golubev
2024-05-23 20:43:43 -07:00
committed by intellij-monorepo-bot
parent 93ac5fe566
commit 46d7223d9f

View File

@@ -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;