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;
}
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());
}
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);
}
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());
}
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;
}
Aggregations