Jedi: script.usages not working in some circumstances

Created on 18 Jul 2016  ยท  6Comments  ยท  Source: davidhalter/jedi

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>]
bug low-prio

Most helpful comment

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

All 6 comments

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

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?

@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).

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. :)

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

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

blueyed picture blueyed  ยท  4Comments

DoS007 picture DoS007  ยท  4Comments

dpavlic picture dpavlic  ยท  7Comments

davidhalter picture davidhalter  ยท  9Comments

xbsura picture xbsura  ยท  4Comments