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

Get reference in type annotation #1898

Closed
enadeau opened this issue Dec 13, 2022 · 4 comments
Closed

Get reference in type annotation #1898

enadeau opened this issue Dec 13, 2022 · 4 comments

Comments

@enadeau
Copy link

enadeau commented Dec 13, 2022

Jedi seems to not pick up reference that are in type annotation or in f-string. This creates issue when using rename has to reference are not changed.

This snippet prints only one reference when it should find three.

import jedi
source = '''
class Foo:
    def return_me(self) -> "Foo":
        return

    def __str__(self) -> str:
        return "foo"
'''
s = f"My class {Foo()}"

script=jedi.Script(code=source)
for ref in script.get_references(line=2, column=7):
    print(ref)

Output:

<Name full_name='__main__.Foo', description='class Foo'>

I don't know if that is conscious choice, but it would be able to pick those references. I'd be happy to help, maybe by coding a failing test first, if someone can point me where it should go.

@davidhalter
Copy link
Owner

Can you maybe send the full reproduction code? I feel like you removed the jedi calls.

@enadeau enadeau changed the title Get reference in type annotation and f-string Get reference in type annotation Dec 14, 2022
@enadeau
Copy link
Author

enadeau commented Dec 14, 2022

You are right, a silly copy-paste error. Here is the updated script and output.

At the same time, I noticed the part about f-string I mentioned above was actually fixed in 0.18.2, so there is actually only one issue left which is the reference in a type annotation give as a string.

import jedi
source = '''
class Foo:
    def return_me(self) -> "Foo":
        return self
'''

script=jedi.Script(code=source)
for ref in script.get_references(line=2, column=7):
    print(ref)

Output

<Name full_name='__main__.Foo', description='class Foo'>

@davidhalter
Copy link
Owner

Yes, the issue with the references is unfortunate, but I'm not sure this is very easy. I would probably argue that people should just use from __future__ import annotations and not use the strings.

You could try to scan for all functions in a file and then check all annotations and parse those (if they are strings), look for the name and try to resolve it. It's probably not extremely easy, but definitely possible.

@enadeau
Copy link
Author

enadeau commented Jan 8, 2023

I agree with you the use for from __future__ import annotations is the way it should be done. I'm closing the issue

@enadeau enadeau closed this as completed Jan 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants