use of com.jetbrains.python.psi.resolve.PyResolveContext in project intellij-community by JetBrains.
the class PyReplaceTupleWithListQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
assert element instanceof PyAssignmentStatement;
PyExpression[] targets = ((PyAssignmentStatement) element).getTargets();
if (targets.length == 1 && targets[0] instanceof PySubscriptionExpression) {
PySubscriptionExpression subscriptionExpression = (PySubscriptionExpression) targets[0];
if (subscriptionExpression.getOperand() instanceof PyReferenceExpression) {
PyReferenceExpression referenceExpression = (PyReferenceExpression) subscriptionExpression.getOperand();
final TypeEvalContext context = TypeEvalContext.userInitiated(project, element.getContainingFile());
final PyResolveContext resolveContext = PyResolveContext.noImplicits().withTypeEvalContext(context);
element = referenceExpression.followAssignmentsChain(resolveContext).getElement();
if (element instanceof PyParenthesizedExpression) {
final PyExpression expression = ((PyParenthesizedExpression) element).getContainedExpression();
replaceWithListLiteral(element, (PyTupleExpression) expression);
} else if (element instanceof PyTupleExpression) {
replaceWithListLiteral(element, (PyTupleExpression) element);
}
}
}
}
use of com.jetbrains.python.psi.resolve.PyResolveContext in project intellij-community by JetBrains.
the class PyRemoveParameterQuickFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement parameter = descriptor.getPsiElement();
assert parameter instanceof PyParameter;
final PyFunction function = PsiTreeUtil.getParentOfType(parameter, PyFunction.class);
if (function != null) {
final PyResolveContext resolveContext = PyResolveContext.noImplicits();
StreamEx.of(PyRefactoringUtil.findUsages(function, false)).map(UsageInfo::getElement).nonNull().map(PsiElement::getParent).select(PyCallExpression.class).flatMap(callExpression -> callExpression.multiMapArguments(resolveContext).stream()).flatMap(mapping -> mapping.getMappedParameters().entrySet().stream()).filter(entry -> entry.getValue() == parameter).forEach(entry -> entry.getKey().delete());
final PyStringLiteralExpression docStringExpression = function.getDocStringExpression();
final String parameterName = ((PyParameter) parameter).getName();
if (docStringExpression != null && parameterName != null) {
PyDocstringGenerator.forDocStringOwner(function).withoutParam(parameterName).buildAndInsert();
}
}
parameter.delete();
}
use of com.jetbrains.python.psi.resolve.PyResolveContext in project intellij-community by JetBrains.
the class PyGotoDeclarationHandler method getGotoDeclarationTarget.
@Nullable
@Override
public PsiElement getGotoDeclarationTarget(@Nullable final PsiElement sourceElement, final Editor editor) {
if (sourceElement == null) {
return null;
}
final PyResolveContext context = PyResolveContext.noImplicits().withTypeEvalContext(TypeEvalContext.userInitiated(sourceElement.getProject(), sourceElement.getContainingFile()));
PyReferenceOwner referenceOwner = null;
final PsiElement parent = sourceElement.getParent();
if (sourceElement instanceof PyReferenceOwner) {
referenceOwner = (PyReferenceOwner) sourceElement;
} else if (parent instanceof PyReferenceOwner) {
//Reference expression may be parent of IDENTIFIER
referenceOwner = (PyReferenceOwner) parent;
}
if (referenceOwner != null) {
return referenceOwner.getReference(context).resolve();
}
// If element is not ref owner, it still may have provided references, lets find some
final PsiElement element = findProvidedReferenceAndResolve(sourceElement);
if (element != null) {
return element;
}
if (parent != null) {
return findProvidedReferenceAndResolve(parent);
}
return null;
}
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;
}
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);
}
Aggregations