use of com.jetbrains.python.psi.types.PyClassLikeType in project intellij-community by JetBrains.
the class PySuperMethodsSearchExecutor method execute.
@Override
public boolean execute(@NotNull final PySuperMethodsSearch.SearchParameters queryParameters, @NotNull final Processor<PsiElement> consumer) {
final PyFunction func = queryParameters.getDerivedMethod();
final String name = func.getName();
final PyClass containingClass = func.getContainingClass();
final Set<PyClass> foundMethodContainingClasses = new HashSet<>();
final TypeEvalContext context = queryParameters.getContext();
if (name != null && containingClass != null) {
for (PyClass superClass : containingClass.getAncestorClasses(context)) {
if (!queryParameters.isDeepSearch()) {
boolean isAlreadyFound = false;
for (PyClass alreadyFound : foundMethodContainingClasses) {
if (alreadyFound.isSubclass(superClass, context)) {
isAlreadyFound = true;
}
}
if (isAlreadyFound) {
continue;
}
}
PyFunction superMethod = superClass.findMethodByName(name, false, null);
if (superMethod != null) {
final Property property = func.getProperty();
final Property superProperty = superMethod.getProperty();
if (property != null && superProperty != null) {
final AccessDirection direction = PyUtil.getPropertyAccessDirection(func);
final PyCallable callable = superProperty.getByDirection(direction).valueOrNull();
superMethod = (callable instanceof PyFunction) ? (PyFunction) callable : null;
}
}
if (superMethod == null && context != null) {
// If super method still not found and we have context, we may use it to find method
final PyClassLikeType classLikeType = PyUtil.as(context.getType(superClass), PyClassLikeType.class);
if (classLikeType != null) {
for (final PyFunction function : PyTypeUtil.getMembersOfType(classLikeType, PyFunction.class, true, context)) {
final String elemName = function.getName();
if (elemName != null && elemName.equals(func.getName())) {
consumer.process(function);
}
}
}
}
if (superMethod != null) {
foundMethodContainingClasses.add(superClass);
if (!consumer.process(superMethod)) {
return false;
}
}
}
}
return true;
}
use of com.jetbrains.python.psi.types.PyClassLikeType in project intellij-community by JetBrains.
the class PyiUtil method findSimilarElement.
@Nullable
private static PsiElement findSimilarElement(@NotNull PyElement element, @NotNull PyFile file) {
if (element instanceof PyFile) {
return file;
}
final ScopeOwner owner = ScopeUtil.getScopeOwner(element);
final String name = element.getName();
if (owner != null && name != null) {
assert owner != element;
final PsiElement originalOwner = findSimilarElement(owner, file);
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();
final List<? extends RatedResolveResult> resolveResults = instanceType.resolveMember(name, null, AccessDirection.READ, PyResolveContext.noImplicits(), 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 PyClassMROTest method assertMRO.
public void assertMRO(@NotNull PyClass cls, @NotNull String... mro) {
final List<PyClassLikeType> types = cls.getAncestorTypes(TypeEvalContext.deepCodeInsight(cls.getProject()));
final List<String> classNames = new ArrayList<>();
for (PyClassLikeType type : types) {
if (type != null) {
final String name = type.getName();
if (name != null) {
classNames.add(name);
continue;
}
}
classNames.add("unknown");
}
assertOrderedEquals(classNames, Arrays.asList(mro));
}
Aggregations