Python omni-completion fails on bad import

470 views
Skip to first unread message

Chris Lasher

unread,
Dec 22, 2008, 7:50:29 PM12/22/08
to vim_dev
Hello,

I sent this email to bu...@vim.org, but I am also sending it here so
more eyes have a chance to see it.

Vim's omni-completion fails for Python any time a file contains an
import statement which fails (i.e., any import statement that will
raise an ImportError). For example:

import notamodule

class Foo:
def bar(self):
pass

# try omni-completing Foo here <C-x><C-o>
# then comment out the above import statement and try again
Fo


This bug exists even if the import statement is wrapped within a try,
except clause, e.g.,:

try:
import notamodule
except ImportError:
pass


Best,
Chris Lasher

Bram Moolenaar

unread,
Dec 23, 2008, 2:58:44 PM12/23/08
to Chris Lasher, Aaron Griffin, vim_dev

Chris Lasher wrote:

I don't think this can be solved. The completion relies on parsing the
file by Python, thus when parsing fails somehow it doesn't know what to
complete.

Aaron, is there something that can be done to avoid this problem?

--
Lawmakers made it obligatory for everybody to take at least one bath
each week -- on Saturday night.
[real standing law in Vermont, United States of America]

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ download, build and distribute -- http://www.A-A-P.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Chris Lasher

unread,
Dec 23, 2008, 3:57:27 PM12/23/08
to Aaron Griffin, Bram Moolenaar, vim_dev
On Tue, Dec 23, 2008 at 3:50 PM, Aaron Griffin <aaronm...@gmail.com> wrote:

> On Tue, Dec 23, 2008 at 1:58 PM, Bram Moolenaar <Br...@moolenaar.net> wrote:
>>
>> Chris Lasher wrote:
>>
>>> I sent this email to bu...@vim.org, but I am also sending it here so
>>> more eyes have a chance to see it.
>>>
>>> Vim's omni-completion fails for Python any time a file contains an
>>> import statement which fails (i.e., any import statement that will
>>> raise an ImportError). For example:
>>>
>>> import notamodule
>>>
>>> class Foo:
>>> def bar(self):
>>> pass
>>>
>>> # try omni-completing Foo here <C-x><C-o>
>>> # then comment out the above import statement and try again
>>> Fo
>>>
>>>
>>> This bug exists even if the import statement is wrapped within a try,
>>> except clause, e.g.,:
>>>
>>> try:
>>> import notamodule
>>> except ImportError:
>>> pass
>>
>> I don't think this can be solved. The completion relies on parsing the
>> file by Python, thus when parsing fails somehow it doesn't know what to
>> complete.
>>
>> Aaron, is there something that can be done to avoid this problem?
>
> Hmm, it may be solvable if I step through each import one at a time
> and ignore the errors.
> I'll stick this in my queue of pythoncomplete issues and maybe try to
> get a new version out after Christmas time. Although, if anyone feels
> frisky, patches are always welcome :)

To add additional information on this omni-completion bug, I
discovered that this bug only affects omni-completion when editing the
file doing the failed import. For example take these two files:

--

#!/usr/bin/env python
# foo.py

import notamodule

class Foo:
def bar(self):
pass

# Can't do an omni-complete against Foo here

--

#!/usr/bin/env python
# spam.py

import foo

# You can, however, omni-complete foo.Foo here, even foo.Foo.bar

--

I'm not sure if this helps but it seemed worth reporting.

Best,
Chris

Chris Lasher

unread,
May 15, 2009, 2:31:27 AM5/15/09
to Aaron Griffin, Bram Moolenaar, vim_dev

I came across this question on Stack Overflow and was reminded of this issue:
http://stackoverflow.com/questions/199180/

Any workarounds, yet? I, myself, haven't dug into the code responsible for Python completion, so I'm wondering if anyone else has.

Chris

Chris Lasher

unread,
May 15, 2009, 11:16:00 AM5/15/09
to Aaron Griffin, Bram Moolenaar, vim_dev
On Fri, May 15, 2009 at 10:57 AM, Aaron Griffin <aaronm...@gmail.com> wrote:
I'm confident that simply wrapping the imports in try blocks (in
pythoncomplete.vim) should solve this just fine.

More to the point, however - why are you running into a case like
this? Shouldn't you have a try block around imports that you expect to
fail?

Sure, I do, but as I stated in the original post, wrapping failed imports in try blocks still kills omni-completion, curiously.

Chris
Reply all
Reply to author
Forward
0 new messages