use of com.jetbrains.python.psi.resolve.PyResolveContext in project intellij-community by JetBrains.
the class PyNamedParameterImpl method getType.
public PyType getType(@NotNull final TypeEvalContext context, @NotNull TypeEvalContext.Key key) {
final PsiElement parent = getParentByStub();
if (parent instanceof PyParameterList) {
PyParameterList parameterList = (PyParameterList) parent;
PyFunction func = parameterList.getContainingFunction();
if (func != null) {
for (PyTypeProvider provider : Extensions.getExtensions(PyTypeProvider.EP_NAME)) {
final Ref<PyType> resultRef = provider.getParameterType(this, func, context);
if (resultRef != null) {
return resultRef.get();
}
}
if (isSelf()) {
// must be 'self' or 'cls'
final PyClass containingClass = func.getContainingClass();
if (containingClass != null) {
final PyFunction.Modifier modifier = func.getModifier();
return new PyClassTypeImpl(containingClass, modifier == PyFunction.Modifier.CLASSMETHOD);
}
}
if (isKeywordContainer()) {
return PyTypeUtil.toKeywordContainerType(this, null);
}
if (isPositionalContainer()) {
return PyTypeUtil.toPositionalContainerType(this, null);
}
if (context.maySwitchToAST(this)) {
final PyExpression defaultValue = getDefaultValue();
if (defaultValue != null) {
final PyType type = context.getType(defaultValue);
if (type != null && !(type instanceof PyNoneType)) {
if (type instanceof PyTupleType) {
return PyUnionType.createWeakType(type);
}
return type;
}
}
}
// Guess the type from file-local calls
if (context.allowCallContext(this)) {
final List<PyType> types = new ArrayList<>();
final PyResolveContext resolveContext = PyResolveContext.noImplicits().withTypeEvalContext(context);
processLocalCalls(func, call -> {
StreamEx.of(call.multiMapArguments(resolveContext)).flatMap(mapping -> mapping.getMappedParameters().entrySet().stream()).filter(entry -> entry.getValue() == this).map(Map.Entry::getKey).nonNull().map(context::getType).nonNull().forEach(types::add);
return true;
});
if (!types.isEmpty()) {
return PyUnionType.createWeakType(PyUnionType.union(types));
}
}
if (context.maySwitchToAST(this)) {
final Set<String> attributes = collectUsedAttributes(context);
if (!attributes.isEmpty()) {
return new PyStructuralType(attributes, true);
}
}
}
}
return null;
}
use of com.jetbrains.python.psi.resolve.PyResolveContext in project intellij-community by JetBrains.
the class PyCallExpressionHelper method mapArguments.
@NotNull
public static ArgumentMappingResults mapArguments(@NotNull PyCallSiteExpression callSite, @NotNull PyCallable callable, @NotNull List<PyParameter> parameters, @NotNull TypeEvalContext context) {
final List<PyExpression> arguments = PyTypeChecker.getArguments(callSite, callable);
final PyResolveContext resolveContext = PyResolveContext.noImplicits().withTypeEvalContext(context);
final List<PyParameter> explicitParameters = PyTypeChecker.filterExplicitParameters(parameters, callable, callSite, resolveContext);
return analyzeArguments(arguments, explicitParameters);
}
use of com.jetbrains.python.psi.resolve.PyResolveContext in project intellij-community by JetBrains.
the class PyCallExpressionHelper method getCallType.
public static PyType getCallType(@NotNull PyCallExpression call, @NotNull TypeEvalContext context) {
if (!TypeEvalStack.mayEvaluate(call)) {
return null;
}
try {
PyExpression callee = call.getCallee();
if (callee instanceof PyReferenceExpression) {
// hardwired special cases
if (PyNames.SUPER.equals(callee.getText())) {
final Maybe<PyType> superCallType = getSuperCallType(call, context);
if (superCallType.isDefined()) {
return superCallType.value();
}
}
if ("type".equals(callee.getText())) {
final PyExpression[] args = call.getArguments();
if (args.length == 1) {
final PyExpression arg = args[0];
final PyType argType = context.getType(arg);
if (argType instanceof PyClassType) {
final PyClassType classType = (PyClassType) argType;
if (!classType.isDefinition()) {
final PyClass cls = classType.getPyClass();
return context.getType(cls);
}
} else {
return null;
}
}
}
// normal cases
final PyResolveContext resolveContext = PyResolveContext.noImplicits().withTypeEvalContext(context);
final PsiPolyVariantReference reference = ((PyReferenceExpression) callee).getReference(resolveContext);
final List<PyType> members = new ArrayList<>();
for (PsiElement target : PyUtil.multiResolveTopPriority(reference)) {
PyUtil.verboseOnly(() -> PyPsiUtils.assertValid(target));
if (target != null) {
final Ref<? extends PyType> typeRef = getCallTargetReturnType(call, target, context);
if (typeRef != null) {
members.add(typeRef.get());
}
}
}
if (!members.isEmpty()) {
return PyUnionType.union(members);
}
}
if (callee == null) {
return null;
} else {
final PyType type = context.getType(callee);
if (type instanceof PyCallableType) {
final PyCallableType callableType = (PyCallableType) type;
return callableType.getCallType(context, call);
}
if (type instanceof PyUnionType) {
return getCallResultTypeFromUnion(call, context, (PyUnionType) type);
}
return null;
}
} finally {
TypeEvalStack.evaluated(call);
}
}
use of com.jetbrains.python.psi.resolve.PyResolveContext in project intellij-community by JetBrains.
the class PyClassImpl method resolveTypeMember.
@Nullable
private static PsiElement resolveTypeMember(@NotNull PyType type, @NotNull String name, @NotNull TypeEvalContext context) {
final PyResolveContext resolveContext = PyResolveContext.noImplicits().withTypeEvalContext(context);
final List<? extends RatedResolveResult> results = type.resolveMember(name, null, AccessDirection.READ, resolveContext);
return (results != null && !results.isEmpty()) ? results.get(0).getElement() : null;
}
use of com.jetbrains.python.psi.resolve.PyResolveContext in project intellij-community by JetBrains.
the class PyResolveCalleeTest method resolveCallee.
@NotNull
private PyCallExpression.PyMarkedCallee resolveCallee() {
final PsiReference ref = myFixture.getReferenceAtCaretPosition("/resolve/callee/" + getTestName(false) + ".py");
final PyCallExpression call = PsiTreeUtil.getParentOfType(ref.getElement(), PyCallExpression.class);
final TypeEvalContext context = TypeEvalContext.codeAnalysis(myFixture.getProject(), myFixture.getFile());
final PyResolveContext resolveContext = PyResolveContext.noImplicits().withTypeEvalContext(context);
final List<PyCallExpression.PyMarkedCallee> callees = call.multiResolveCallee(resolveContext);
assertEquals(1, callees.size());
return callees.get(0);
}
Aggregations