Search in sources :

Example 1 with PyClassLikeType

use of com.jetbrains.python.psi.types.PyClassLikeType in project intellij-community by JetBrains.

the class PyUserSkeletonsUtil method getUserSkeleton.

@Nullable
private static PsiElement getUserSkeleton(@NotNull PyElement element, @NotNull PyFile skeletonFile, @Nullable TypeEvalContext context) {
    if (element instanceof PyFile) {
        return skeletonFile;
    }
    final ScopeOwner owner = ScopeUtil.getScopeOwner(element);
    final String name = element.getName();
    if (owner != null && name != null) {
        assert owner != element;
        final PsiElement originalOwner = getUserSkeleton(owner, skeletonFile, context);
        if (originalOwner instanceof PyClass) {
            final PyClass classOwner = (PyClass) originalOwner;
            final PyType type = TypeEvalContext.codeInsightFallback(classOwner.getProject()).getType(classOwner);
            if (type instanceof PyClassLikeType) {
                final PyClassLikeType classType = (PyClassLikeType) type;
                final PyClassLikeType instanceType = classType.toInstance();
                PyResolveContext resolveContext = PyResolveContext.noImplicits();
                if (context != null) {
                    resolveContext = resolveContext.withTypeEvalContext(context);
                }
                final List<? extends RatedResolveResult> resolveResults = instanceType.resolveMember(name, null, AccessDirection.READ, resolveContext, false);
                if (resolveResults != null && !resolveResults.isEmpty()) {
                    return resolveResults.get(0).getElement();
                }
            }
        } else if (originalOwner instanceof PyFile) {
            return ((PyFile) originalOwner).getElementNamed(name);
        }
    }
    return null;
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) PyType(com.jetbrains.python.psi.types.PyType) PyClassLikeType(com.jetbrains.python.psi.types.PyClassLikeType) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with PyClassLikeType

use of com.jetbrains.python.psi.types.PyClassLikeType in project intellij-community by JetBrains.

the class PythonUnitTestUtil method isTestCaseClassWithContext.

public static boolean isTestCaseClassWithContext(@NotNull PyClass cls, Set<String> testQualifiedNames, @Nullable TypeEvalContext context) {
    final TypeEvalContext contextToUse = (context != null ? context : TypeEvalContext.codeInsightFallback(cls.getProject()));
    for (PyClassLikeType type : cls.getAncestorTypes(contextToUse)) {
        if (type != null) {
            if (testQualifiedNames.contains(type.getClassQName())) {
                return true;
            }
        }
    }
    String clsName = cls.getQualifiedName();
    String[] names = new String[0];
    if (clsName != null) {
        names = clsName.split("\\.");
    }
    if (names.length == 0)
        return false;
    clsName = names[names.length - 1];
    if (TEST_MATCH_PATTERN.matcher(clsName).find()) {
        return true;
    }
    return false;
}
Also used : PyClassLikeType(com.jetbrains.python.psi.types.PyClassLikeType) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext)

Example 3 with PyClassLikeType

use of com.jetbrains.python.psi.types.PyClassLikeType in project intellij-community by JetBrains.

the class PyTestUtil method isPyTestClass.

public static boolean isPyTestClass(final PyClass pyClass, @Nullable final TypeEvalContext context) {
    final TypeEvalContext contextToUse = (context != null ? context : TypeEvalContext.codeInsightFallback(pyClass.getProject()));
    for (PyClassLikeType type : pyClass.getAncestorTypes(contextToUse)) {
        if (type != null && PYTHON_TEST_QUALIFIED_CLASSES.contains(type.getClassQName())) {
            return true;
        }
    }
    final String className = pyClass.getName();
    if (className == null)
        return false;
    final String name = className.toLowerCase();
    if (name.startsWith("test")) {
        for (PyFunction cls : pyClass.getMethods()) {
            if (isPyTestFunction(cls)) {
                return true;
            }
        }
    }
    return false;
}
Also used : PyFunction(com.jetbrains.python.psi.PyFunction) PyClassLikeType(com.jetbrains.python.psi.types.PyClassLikeType) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext)

Example 4 with PyClassLikeType

use of com.jetbrains.python.psi.types.PyClassLikeType in project intellij-community by JetBrains.

the class PyJavaSuperMethodsSearchExecutor method execute.

public boolean execute(@NotNull final PySuperMethodsSearch.SearchParameters queryParameters, @NotNull final Processor<PsiElement> consumer) {
    PyFunction func = queryParameters.getDerivedMethod();
    PyClass containingClass = func.getContainingClass();
    if (containingClass != null) {
        for (PyClassLikeType type : containingClass.getSuperClassTypes(TypeEvalContext.codeInsightFallback(containingClass.getProject()))) {
            if (type instanceof PyJavaClassType) {
                final PsiClass psiClass = ((PyJavaClassType) type).getPsiClass();
                PsiMethod[] methods = psiClass.findMethodsByName(func.getName(), true);
                // the Python method actually does override/implement all of Java super methods with the same name
                if (!ContainerUtil.process(methods, consumer))
                    return false;
            }
        }
    }
    return true;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) PyClassLikeType(com.jetbrains.python.psi.types.PyClassLikeType)

Example 5 with PyClassLikeType

use of com.jetbrains.python.psi.types.PyClassLikeType in project intellij-community by JetBrains.

the class PyOperatorReference method resolveMember.

@NotNull
private List<RatedResolveResult> resolveMember(@Nullable PyExpression object, @Nullable String name) {
    final ArrayList<RatedResolveResult> results = new ArrayList<>();
    if (object != null && name != null) {
        final TypeEvalContext typeEvalContext = myContext.getTypeEvalContext();
        PyType type = typeEvalContext.getType(object);
        typeEvalContext.trace("Side text is %s, type is %s", object.getText(), type);
        if (type instanceof PyClassLikeType) {
            if (((PyClassLikeType) type).isDefinition()) {
                type = ((PyClassLikeType) type).getMetaClassType(typeEvalContext, true);
            }
        }
        if (type != null) {
            List<? extends RatedResolveResult> res = type.resolveMember(name, object, AccessDirection.of(myElement), myContext);
            if (res != null && res.size() > 0) {
                results.addAll(res);
            } else if (typeEvalContext.tracing()) {
                VirtualFile vFile = null;
                if (type instanceof PyClassType) {
                    final PyClass pyClass = ((PyClassType) type).getPyClass();
                    vFile = pyClass.getContainingFile().getVirtualFile();
                }
                type.resolveMember(name, object, AccessDirection.of(myElement), myContext);
                typeEvalContext.trace("Could not resolve member %s in type %s from file %s", name, type, vFile);
            }
        }
    }
    return results;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PyClassType(com.jetbrains.python.psi.types.PyClassType) PyType(com.jetbrains.python.psi.types.PyType) ArrayList(java.util.ArrayList) PyClassLikeType(com.jetbrains.python.psi.types.PyClassLikeType) RatedResolveResult(com.jetbrains.python.psi.resolve.RatedResolveResult) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PyClassLikeType (com.jetbrains.python.psi.types.PyClassLikeType)8 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)4 PyType (com.jetbrains.python.psi.types.PyType)3 PsiElement (com.intellij.psi.PsiElement)2 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)2 PyFunction (com.jetbrains.python.psi.PyFunction)2 ArrayList (java.util.ArrayList)2 Nullable (org.jetbrains.annotations.Nullable)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiClass (com.intellij.psi.PsiClass)1 PsiMethod (com.intellij.psi.PsiMethod)1 PyClass (com.jetbrains.python.psi.PyClass)1 RatedResolveResult (com.jetbrains.python.psi.resolve.RatedResolveResult)1 PyClassType (com.jetbrains.python.psi.types.PyClassType)1 HashSet (java.util.HashSet)1 NotNull (org.jetbrains.annotations.NotNull)1