Search in sources :

Example 21 with PyResolveContext

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;
}
Also used : PyNames(com.jetbrains.python.PyNames) java.util(java.util) PythonDocumentationProvider(com.jetbrains.python.documentation.PythonDocumentationProvider) ScopeUtil(com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil) SearchScope(com.intellij.psi.search.SearchScope) ContainerUtil(com.intellij.util.containers.ContainerUtil) PyElementTypes(com.jetbrains.python.PyElementTypes) PythonDialectsTokenSetProvider(com.jetbrains.python.PythonDialectsTokenSetProvider) PyNamedParameterStub(com.jetbrains.python.psi.stubs.PyNamedParameterStub) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ItemPresentation(com.intellij.navigation.ItemPresentation) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) com.jetbrains.python.psi(com.jetbrains.python.psi) com.jetbrains.python.psi.types(com.jetbrains.python.psi.types) PyTypingTypeProvider(com.jetbrains.python.codeInsight.typing.PyTypingTypeProvider) PlatformIcons(com.intellij.util.PlatformIcons) Extensions(com.intellij.openapi.extensions.Extensions) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IStubElementType(com.intellij.psi.stubs.IStubElementType) PyTokenTypes(com.jetbrains.python.PyTokenTypes) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) Collectors(java.util.stream.Collectors) ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable) StreamEx(one.util.streamex.StreamEx) Processor(com.intellij.util.Processor) Pair(com.intellij.openapi.util.Pair) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) javax.swing(javax.swing) com.jetbrains.python.psi.types(com.jetbrains.python.psi.types) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext)

Example 22 with PyResolveContext

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);
}
Also used : PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with PyResolveContext

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);
    }
}
Also used : PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) PsiPolyVariantReference(com.intellij.psi.PsiPolyVariantReference) PsiElement(com.intellij.psi.PsiElement)

Example 24 with PyResolveContext

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;
}
Also used : PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) Nullable(org.jetbrains.annotations.Nullable)

Example 25 with PyResolveContext

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);
}
Also used : PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) PsiReference(com.intellij.psi.PsiReference) PyCallExpression(com.jetbrains.python.psi.PyCallExpression) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PyResolveContext (com.jetbrains.python.psi.resolve.PyResolveContext)27 PsiElement (com.intellij.psi.PsiElement)12 NotNull (org.jetbrains.annotations.NotNull)11 Nullable (org.jetbrains.annotations.Nullable)11 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)6 com.jetbrains.python.psi (com.jetbrains.python.psi)6 ContainerUtil (com.intellij.util.containers.ContainerUtil)5 RatedResolveResult (com.jetbrains.python.psi.resolve.RatedResolveResult)5 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)5 java.util (java.util)5 LookupElement (com.intellij.codeInsight.lookup.LookupElement)4 Extensions (com.intellij.openapi.extensions.Extensions)4 PsiFile (com.intellij.psi.PsiFile)4 ArrayUtil (com.intellij.util.ArrayUtil)4 Processor (com.intellij.util.Processor)4 PyNames (com.jetbrains.python.PyNames)4 Collectors (java.util.stream.Collectors)4 Sets (com.google.common.collect.Sets)3 Pair (com.intellij.openapi.util.Pair)3 QualifiedName (com.intellij.psi.util.QualifiedName)3