Vim omnicompletion fails with in-import in Python

Omnicompletion for Python seems to fail when there is an import from out instead of the usual one. For example, if I have these two files:

Test.py:

class Test:
    def method(self):
        pass

main.py:

from Test import Test

class Test2:
    def __init__(self):
       self.x = Test()

If I try to activate omnicompletion for self.x ... it says: "Template not found." However, if I changed the import statement to:

import Test

and self.x declaration:

self.x = Test.Test()

then I can use omnicompletion as expected (for example, it offers a "method").

I am using Vim 7.2.245 and the default plugin to complete Python code (pythoncomplete). Should I set some variable? Or is this behavior expected?

Update

Based on Jared's answer, I accidentally found out:

Omnicompletion does not work on this:

from StringIO import StringIO

class Test:
    def __init__(self):
        self.x = StringIO()
        self.x.<C-x><C-o>

s = Test()

But working on this:

from StringIO import StringIO

class Test:
    def __init__(self):
        self.x = StringIO()
        self.x.<C-x><C-o>

s = Test()
s.x = StringIO()

x (, , __init__).

, , , . main.py :

from Test import Test

class Test2:
    def __init__(self):
       self.x = Test()
       self.x.<C-x><C-o>

y = Test()
y.<C-x><C-o>

omnicompletion , . , yep, :)

+3
1

update: ooh,

x = Test()
x.<C-x><C-o>

o = object()
o.x = Test()
o.x.<C-x><C-o>

...

2:

... .

from StringIO import StringIO
class M:
    pass
s = M()
s.x = StringIO()
s.x.<C-x><C-o>

.

from StringIO import StringIO
class M: pass
s = M()
s.x = StringIO()
s.x.<C-x><C-o>

? -

. - ( ast, ...)

[ ]

; :

from os import path
path.<C-x><C-o>

. , , , pythoncomplete 'from'.

:

from StringIO import StringIO
s = StringIO()
s.<C-x><C-o>

... ! , ? , , , ...

, , # 555 pythoncomplete.vim [at /usr/share/vim/vim72/autoload/pythoncomplete.vim ubuntu]:

            elif token == 'from':                                    
                mod, token = self._parsedotname()                    
                if not mod or token != "import":                     
                    print "from: syntax error..."                    
                    continue                                         
                names = self._parseimportlist()                      
                for name, alias in names:                            
                    loc = "from %s import %s" % (mod,name)           
                    if len(alias) > 0: loc += " as %s" % alias       
                    self.scope.local(loc)                            
                freshscope = False                                   

, from.

+2

Source: https://habr.com/ru/post/1753889/


All Articles