Search in sources :

Example 1 with PyFunctionTypeAnnotation

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

the class PyTypingTypeProvider method getReturnTypeAnnotation.

@Nullable
private static PyExpression getReturnTypeAnnotation(@NotNull PyFunction function) {
    final PyAnnotation annotation = function.getAnnotation();
    if (annotation != null) {
        // XXX: Requires switching from stub to AST
        return annotation.getValue();
    }
    final PyFunctionTypeAnnotation functionAnnotation = getFunctionTypeAnnotation(function);
    if (functionAnnotation != null) {
        return functionAnnotation.getReturnType();
    }
    return null;
}
Also used : PyFunctionTypeAnnotation(com.jetbrains.python.codeInsight.functionTypeComments.psi.PyFunctionTypeAnnotation) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with PyFunctionTypeAnnotation

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

the class PyFunctionTypeAnnotationParsingTest method testNoReturnType.

public void testNoReturnType() {
    doCodeTest("(int) -> ");
    final PyFunctionTypeAnnotation annotation = getParsedAnnotation();
    final List<PyExpression> paramTypes = annotation.getParameterTypeList().getParameterTypes();
    assertSize(1, paramTypes);
    assertNull(annotation.getReturnType());
}
Also used : PyFunctionTypeAnnotation(com.jetbrains.python.codeInsight.functionTypeComments.psi.PyFunctionTypeAnnotation) PyExpression(com.jetbrains.python.psi.PyExpression)

Example 3 with PyFunctionTypeAnnotation

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

the class PyFunctionTypeAnnotationParsingTest method testEllipsis.

public void testEllipsis() {
    doCodeTest("(...) -> None");
    final PyFunctionTypeAnnotation annotation = getParsedAnnotation();
    final List<PyExpression> paramTypes = annotation.getParameterTypeList().getParameterTypes();
    assertSize(1, paramTypes);
    assertInstanceOf(paramTypes.get(0), PyNoneLiteralExpression.class);
    final PyExpression returnType = annotation.getReturnType();
    assertNotNull(returnType);
}
Also used : PyFunctionTypeAnnotation(com.jetbrains.python.codeInsight.functionTypeComments.psi.PyFunctionTypeAnnotation) PyExpression(com.jetbrains.python.psi.PyExpression)

Example 4 with PyFunctionTypeAnnotation

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

the class PyFunctionTypeAnnotationParsingTest method testEmpty.

public void testEmpty() {
    doCodeTest("() -> None");
    final PyFunctionTypeAnnotation annotation = getParsedAnnotation();
    assertNotNull(annotation);
    assertEmpty(annotation.getParameterTypeList().getParameterTypes());
    final PyExpression returnType = annotation.getReturnType();
    assertNotNull(returnType);
    assertEquals("None", returnType.getText());
}
Also used : PyFunctionTypeAnnotation(com.jetbrains.python.codeInsight.functionTypeComments.psi.PyFunctionTypeAnnotation) PyExpression(com.jetbrains.python.psi.PyExpression)

Example 5 with PyFunctionTypeAnnotation

use of com.jetbrains.python.codeInsight.functionTypeComments.psi.PyFunctionTypeAnnotation 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)5 PyExpression (com.jetbrains.python.psi.PyExpression)3 Nullable (org.jetbrains.annotations.Nullable)2 PyParameterTypeList (com.jetbrains.python.codeInsight.functionTypeComments.psi.PyParameterTypeList)1