use of com.jetbrains.python.psi.types.TypeEvalContext in project intellij-community by JetBrains.
the class PySuperMethodsSearch method findDeepestSuperMethod.
public static PyFunction findDeepestSuperMethod(PyFunction function) {
TypeEvalContext context = TypeEvalContext.userInitiated(function.getProject(), null);
List<PsiElement> superMethods = new ArrayList<>(search(function, true, context).findAll());
while (superMethods.size() > 0) {
function = getBaseMethod(superMethods, function.getContainingClass());
superMethods = new ArrayList<>(search(function, true, context).findAll());
}
return function;
}
use of com.jetbrains.python.psi.types.TypeEvalContext in project intellij-community by JetBrains.
the class PyIterableVariableMacro method getIterableElements.
@NotNull
protected List<PsiNamedElement> getIterableElements(@NotNull PsiElement element) {
final TypeEvalContext typeEvalContext = TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile());
final List<PsiNamedElement> components = new ArrayList<>();
for (PsiNamedElement namedElement : getVisibleNamedElements(element)) {
if (namedElement instanceof PyTypedElement) {
final PyType type = typeEvalContext.getType((PyTypedElement) namedElement);
if (type != null && PyABCUtil.isSubtype(type, PyNames.ITERABLE, typeEvalContext)) {
components.add(namedElement);
}
}
}
return components;
}
use of com.jetbrains.python.psi.types.TypeEvalContext in project intellij-community by JetBrains.
the class PyMethodMember method buildNameFor.
private static String buildNameFor(final PyElement element) {
if (element instanceof PyFunction) {
final TypeEvalContext context = TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile());
final List<PyParameter> parameters = PyUtil.getParameters((PyFunction) element, context);
return element.getName() + "(" + StringUtil.join(parameters, parameter -> PyUtil.getReadableRepr(parameter, false), ", ") + ")";
}
final PyClass cls = as(element, PyClass.class);
if (cls != null && PyNames.TYPES_INSTANCE_TYPE.equals(cls.getQualifiedName())) {
return "<old-style class>";
}
return element.getName();
}
use of com.jetbrains.python.psi.types.TypeEvalContext in project intellij-community by JetBrains.
the class PyOverrideImplementUtil method buildOverriddenFunction.
private static PyFunctionBuilder buildOverriddenFunction(PyClass pyClass, PyFunction baseFunction, boolean implement) {
final boolean overridingNew = PyNames.NEW.equals(baseFunction.getName());
assert baseFunction.getName() != null;
PyFunctionBuilder pyFunctionBuilder = new PyFunctionBuilder(baseFunction.getName(), baseFunction);
final PyDecoratorList decorators = baseFunction.getDecoratorList();
boolean baseMethodIsStatic = false;
if (decorators != null) {
if (decorators.findDecorator(PyNames.CLASSMETHOD) != null) {
pyFunctionBuilder.decorate(PyNames.CLASSMETHOD);
} else if (decorators.findDecorator(PyNames.STATICMETHOD) != null) {
baseMethodIsStatic = true;
pyFunctionBuilder.decorate(PyNames.STATICMETHOD);
} else if (decorators.findDecorator(PyNames.PROPERTY) != null || decorators.findDecorator(PyNames.ABSTRACTPROPERTY) != null) {
pyFunctionBuilder.decorate(PyNames.PROPERTY);
}
}
final LanguageLevel level = LanguageLevel.forElement(pyClass);
PyAnnotation anno = baseFunction.getAnnotation();
if (anno != null && level.isAtLeast(LanguageLevel.PYTHON30)) {
pyFunctionBuilder.annotation(anno.getText());
}
final TypeEvalContext context = TypeEvalContext.userInitiated(baseFunction.getProject(), baseFunction.getContainingFile());
final List<PyParameter> baseParams = PyUtil.getParameters(baseFunction, context);
for (PyParameter parameter : baseParams) {
final PyNamedParameter namedParameter = parameter.getAsNamed();
if (namedParameter != null) {
final StringBuilder parameterBuilder = new StringBuilder();
if (namedParameter.isPositionalContainer()) {
parameterBuilder.append("*");
} else if (namedParameter.isKeywordContainer()) {
parameterBuilder.append("**");
}
parameterBuilder.append(namedParameter.getName());
final PyAnnotation annotation = namedParameter.getAnnotation();
if (annotation != null && level.isAtLeast(LanguageLevel.PYTHON30)) {
parameterBuilder.append(annotation.getText());
}
final PyExpression defaultValue = namedParameter.getDefaultValue();
if (defaultValue != null) {
parameterBuilder.append("=");
parameterBuilder.append(defaultValue.getText());
}
pyFunctionBuilder.parameter(parameterBuilder.toString());
} else {
pyFunctionBuilder.parameter(parameter.getText());
}
}
PyClass baseClass = baseFunction.getContainingClass();
assert baseClass != null;
StringBuilder statementBody = new StringBuilder();
boolean hadStar = false;
List<String> parameters = new ArrayList<>();
for (PyParameter parameter : baseParams) {
final PyNamedParameter pyNamedParameter = parameter.getAsNamed();
if (pyNamedParameter != null) {
String repr = pyNamedParameter.getRepr(false);
parameters.add(hadStar && !pyNamedParameter.isKeywordContainer() ? pyNamedParameter.getName() + "=" + repr : repr);
if (pyNamedParameter.isPositionalContainer()) {
hadStar = true;
}
} else if (parameter instanceof PySingleStarParameter) {
hadStar = true;
} else {
parameters.add(parameter.getText());
}
}
if (PyNames.TYPES_INSTANCE_TYPE.equals(baseClass.getQualifiedName()) || raisesNotImplementedError(baseFunction) || implement) {
statementBody.append(PyNames.PASS);
} else {
if (!PyNames.INIT.equals(baseFunction.getName()) && context.getReturnType(baseFunction) != PyNoneType.INSTANCE || overridingNew) {
statementBody.append("return ");
}
if (baseClass.isNewStyleClass(context)) {
statementBody.append(PyNames.SUPER);
statementBody.append("(");
final LanguageLevel langLevel = ((PyFile) pyClass.getContainingFile()).getLanguageLevel();
if (!langLevel.isPy3K()) {
final String baseFirstName = !baseParams.isEmpty() ? baseParams.get(0).getName() : null;
final String firstName = baseFirstName != null ? baseFirstName : PyNames.CANONICAL_SELF;
PsiElement outerClass = PsiTreeUtil.getParentOfType(pyClass, PyClass.class, true, PyFunction.class);
String className = pyClass.getName();
final List<String> nameResult = Lists.newArrayList(className);
while (outerClass != null) {
nameResult.add(0, ((PyClass) outerClass).getName());
outerClass = PsiTreeUtil.getParentOfType(outerClass, PyClass.class, true, PyFunction.class);
}
StringUtil.join(nameResult, ".", statementBody);
statementBody.append(", ").append(firstName);
}
statementBody.append(").").append(baseFunction.getName()).append("(");
// type.__new__ is explicitly decorated as @staticmethod in our stubs, but not in real Python code
if (parameters.size() > 0 && !(baseMethodIsStatic || overridingNew)) {
parameters.remove(0);
}
} else {
statementBody.append(getReferenceText(pyClass, baseClass)).append(".").append(baseFunction.getName()).append("(");
}
StringUtil.join(parameters, ", ", statementBody);
statementBody.append(")");
}
pyFunctionBuilder.statement(statementBody.toString());
return pyFunctionBuilder;
}
use of com.jetbrains.python.psi.types.TypeEvalContext in project intellij-community by JetBrains.
the class PyOverrideImplementUtil method chooseAndOverrideOrImplementMethods.
private static void chooseAndOverrideOrImplementMethods(final Project project, @NotNull final Editor editor, @NotNull final PyClass pyClass) {
PyPsiUtils.assertValid(pyClass);
ApplicationManager.getApplication().assertReadAccessAllowed();
final Set<PyFunction> result = new HashSet<>();
TypeEvalContext context = TypeEvalContext.codeCompletion(project, null);
final Collection<PyFunction> superFunctions = getAllSuperFunctions(pyClass, context);
result.addAll(superFunctions);
chooseAndOverrideOrImplementMethods(project, editor, pyClass, result, "Select Methods to Override", false);
}
Aggregations