Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

goto() does not always return a definition for the target node #1507

Closed
mlangkabel opened this issue Feb 17, 2020 · 4 comments
Closed

goto() does not always return a definition for the target node #1507

mlangkabel opened this issue Feb 17, 2020 · 4 comments

Comments

@mlangkabel
Copy link
Contributor

I see that for implementing issue #1175 the behavior of the goto function has been changed slightly to return the definition of the overridden method if the node at the target location is already a definition.

This may be handy for some usecases, but others (like me) may rely on the old behavior. As an example: I am using the goto() function to get a "canonical" version of a node.

Would it be possible to add an optional parameter to the goto() function that prevents this new behavior?

@davidhalter
Copy link
Owner

What's your use case? I don't understand why you wouldn't want this.

@mlangkabel
Copy link
Contributor Author

I'm one of the maintainers of Sourcetrail where we use Jedi for our Python indexer. What happens behind the scenes:

  • we use parso to get an AST of the indexed source file
  • then, for each node of interest (where we want to record a symbol definition or a reference to a symbol), we call Jedi's goto() to get the symbol's definition.
  • And from that definition we derive the node's canonical name (e.g. my_package.my_module.my_class.my_function<my_local_variable>). We need a canonical name, so that Sourcetrail's graph always points to the same node for the same symbol.

So for this, no matter what the context of a name's usage is (e.g. in a class definition or in a class instantiation), we were expecting Jedi to always return the node's definition.

@davidhalter
Copy link
Owner

The question is probably why you're using parso. It's possible to do this with Jedi: Script(...).get_names(all_scopes=True, references=True).

At that point you can use each definition.is_definition() or definition.goto(). This makes it a slight bit more complicated for you, but I feel like goto in Jedi is supposed to mimick what a user wants in an IDE, so I'm not going to implement this (for now), exactly because there's good ways around it.

If you really need parso (for whatever reason, but you probably don't), I suggest you use Name.is_definition(). In those cases you don't even need to use Jedi, because parso will already tell you that it's a definition and will go nowhere.

@mlangkabel
Copy link
Contributor Author

Thanks for your quick replies and for your clear statements. I will check your suggestions to get around this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants