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