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

script.usages not working in some circumstances #744

Closed
DonJayamanne opened this issue Jul 18, 2016 · 6 comments
Closed

script.usages not working in some circumstances #744

DonJayamanne opened this issue Jul 18, 2016 · 6 comments

Comments

@DonJayamanne
Copy link
Contributor

script.usages doesn't work under certain circumstances.

(Original issue DonJayamanne/pythonVSCode/issues/124)

Here's the sample python code:

var1, var2 = 1, 2
print var1, var2

Finding references for 'var1' from the first line returns an empy list.
Finding references for 'var1' from the second line works.

Non working code (notice the line number):

import jedi
source = '''
var1, var2 = 1, 2
print var1, var2
'''
script = jedi.Script(source, 1, 0, 'example.py')
script.usages()

>>> []

Working code (notice the line number):

import jedi
source = '''
var1, var2 = 1, 2
print var1, var2
'''
script = jedi.Script(source, 1, len('print '), 'example.py')
script.usages()

>>> [<Definition var1, var2 = 1, 2>, <Definition print var1, var2>]
@davidhalter
Copy link
Owner

Usages is really broken in a lot of places. I would probably need to reimplement large parts of it. It might take some time.

@dam5h
Copy link

dam5h commented Jan 26, 2017

I don't know if this is related to this specific bug or not, but I have also just posted this on StackOverflow, in hopes that it is just my configuration. In any case, thank you for this excellent package!

I am using jedi via YCM in vim and an seeing some weird behavior in my project, regarding using the usages endpoint on the jediHttp server. Basically it only finds a small fraction of the usages in my project of a class or function. It does find all the usages in the current file, but does not find the vast majority in other files. The GoTo command works fine and will open a new buffer anywhere in the project as expected. However when I start at the definition and try to see all the usages via the GoToReferences command it only produces about 20% of the actual usages.

I have tried making a dummy project to recreate the behavior and it seems to have worked. Here is the simple project structure:

> tree -I __pycache__
.
├── setup.py
└── test_jedi
    ├── classes.py
    ├── __init__.py
    ├── mod1
    │   ├── __init__.py
    │   └── recessed.py
    └── script.py

2 directories, 6 files

Here are the contents of those files:

classes.py

class Foo:
    def __init__(self, a):
        self.a = a

    def make_a(self):
        print('hello')

mod1/recessed.py:

from test_jedi.classes import Foo
Foo(5).make_a()
print('Ran this')

script.py:

from classes import Foo
a = Foo(5)
a.make_a()

Now from within vim with recessed.py in the buffer I am able to use GoTo on Foo to immediately open a classes.py buffer. When I then try GoToReferences from the classes.py buffer I only get the usages in script.py and in classes.py. I am at a loss to why jedi does not show me the usage in recessed.py

This is the quickfix buffer that shows:

classes.py|2 col 7| class Foo
script.py|2 col 21| from classes import Foo
script.py|5 col 5| a = Foo(5)

But clearly there is no reference to mod1/recessed.py which clearly has a usage of Foo.

Any ideas?

@davidhalter
Copy link
Owner

@dam5h I can very well understand what your problem is. It's just that usages in Jedi is very backwards and has not been worked on for like 2 years. I hope to be able to fix it some day (or maybe someone else can do that).

@davidhalter
Copy link
Owner

davidhalter commented Mar 2, 2018

The case of @DonJayamanne has been solved (some line numbers in the example were wrong). More complex cases still won't work (especially with complicated folder structures). The problem is basically that we just cannot do this on request.

My plan is to create an index (database) that contains all the references to a name. However there's so much work to do, so you just need to be a little bit patient. :)

@dam5h
Copy link

dam5h commented Mar 2, 2018

Sounds good, hope I didn't seem impatient ; ). Jedi is great stuff, thanks for the work on it.

@davidhalter
Copy link
Owner

It looks like this is fixed now. The line numbers in the first comment are wrong. However the basic issue seems to be fixed.

Generally usages should work very well, as long as it's not across folder structures. It only works in the same file (or maybe in the same directory).

To have better support for directory structures, the issue to follow is #1059.
This is basically the issue @dam5h is facing.

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

3 participants