How to properly resolve a PyCallExpression

class Vector2(object):

def __init__(self, x, y):
self.x = x
self.y = y

def method(self):
print("hello function!")


vector2 = Vector2(-1, -2)
vector2.method()

Considering this simple snippet when trying to resolve the PyCallExpression "method" like this: (in PyRecursiveElementVisitor.visitPyCallExpression(PyCallExpression node))

List<PyCallableType> callableTypes = node.multiResolveCallee(PyResolveContext.defaultContext());

I get an empty list, but when I use: (in PyRecursiveElementVisitor.visitPyCallExpression(PyCallExpression node))

List<PyCallableType> callableTypes = node.multiResolveCallee(PyResolveContext.implicitContext());

I get a list of multiple elements (11 occurences, one of them is the actual result).

How to tell which element is the most accurate one giving this simple case with explicit usage.

How actually IntelliJ handles Go To Declaration? Because it automatically jumps to the declaration when it is trivial (no errors, and access is explicit without getattr or setattr).

Thanks.

4 comments
Comment actions Permalink

Even this snippet as in https://github.com/JetBrains/intellij-community/blob/master/python/testSrc/com/jetbrains/python/PyResolveCalleeTest.java  :

final TypeEvalContext context = TypeEvalContext.codeAnalysis(project, file);
final PyResolveContext resolveContext = PyResolveContext.defaultContext().withTypeEvalContext(context);

List<PyCallableType> callableTypes = node.multiResolveCallee(resolveContext);

doesn't yield correct result.

0
Comment actions Permalink

I tried this snippet on the callee as PyReferenceExpression:

@NotNull ResolveResult[] elements = node.getReference().multiResolve(false);

Works way better, but sometimes gives duplicated results like when resolving

math.sqrt(1)

I get 2 results with same #getElement.

Is this the best way possible?

Thanks.

0
Comment actions Permalink

Do the object IDs of returned elements also match?

0
Comment actions Permalink

For:

@NotNull ResolveResult[] elements = node.getReference().multiResolve(false);

Yes, they match. Is it the correct way to resolve PyReferenceExpression?

I suppose then the TypeEvalContextPyResolveContext are used for general Find Usage?

0

Please sign in to leave a comment.