Search in sources :

Example 1 with PyParameterTypeList

use of com.jetbrains.python.codeInsight.functionTypeComments.psi.PyParameterTypeList in project intellij-community by JetBrains.

the class PyTypingTypeProvider method getParameterType.

@Nullable
public Ref<PyType> getParameterType(@NotNull PyNamedParameter param, @NotNull PyFunction func, @NotNull TypeEvalContext context) {
    final Ref<PyType> typeFromAnnotation = getParameterTypeFromAnnotation(param, context);
    if (typeFromAnnotation != null) {
        return typeFromAnnotation;
    }
    final Ref<PyType> typeFromTypeComment = getParameterTypeFromTypeComment(param, context);
    if (typeFromTypeComment != null) {
        return typeFromTypeComment;
    }
    final PyFunctionTypeAnnotation annotation = getFunctionTypeAnnotation(func);
    if (annotation == null) {
        return null;
    }
    final PyParameterTypeList list = annotation.getParameterTypeList();
    final List<PyExpression> params = list.getParameterTypes();
    if (params.size() == 1) {
        final PyNoneLiteralExpression noneExpr = as(params.get(0), PyNoneLiteralExpression.class);
        if (noneExpr != null && noneExpr.isEllipsis()) {
            return Ref.create();
        }
    }
    final int startOffset = omitFirstParamInTypeComment(func) ? 1 : 0;
    final List<PyParameter> funcParams = Arrays.asList(func.getParameterList().getParameters());
    final int i = funcParams.indexOf(param) - startOffset;
    if (i >= 0 && i < params.size()) {
        return getParameterTypeFromFunctionComment(params.get(i), context);
    }
    return null;
}
Also used : PyFunctionTypeAnnotation(com.jetbrains.python.codeInsight.functionTypeComments.psi.PyFunctionTypeAnnotation) PyParameterTypeList(com.jetbrains.python.codeInsight.functionTypeComments.psi.PyParameterTypeList) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PyFunctionTypeAnnotation (com.jetbrains.python.codeInsight.functionTypeComments.psi.PyFunctionTypeAnnotation)1 PyParameterTypeList (com.jetbrains.python.codeInsight.functionTypeComments.psi.PyParameterTypeList)1 Nullable (org.jetbrains.annotations.Nullable)1