Currently I am writing all my Python code using Neovim and a couple of plugins to provide auto-completion, linting, etc.
For code auto-completion, I use
deoplete as the completion and
engine and deoplete-jedi
as the completion source for Python. They work great most of the time. But,
sometimes I can not get auto-completion for some object instance. For example,
auto-completion for object instances returned by the
in the Pillow does not work. I
decide to take a look.
At first, I think it is an issue of deoplete or deoplete-jedi. But I find that
the Image instances return other methods, e.g.,
work as expected. If it is an issue of deoplete or deoplete-jedi, then
auto-completion should not work anymore. Maybe it is due to the static type
checker Jedi that deoplete-jedi relies on?
To find out the reason, I use the following script to check if Jedi can provide
completion for the Image instances returned from
import jedi source1 = ''' from PIL import Image im = Image.new('test.jpg', (128, 128)) im. ''' script1 = jedi.Script(source1, 4, len('im.'), 'example1.py') print(script1.completions()) source2 = ''' from PIL import Image im = Image.open('test.jpg') im. ''' script2 = jedi.Script(source2, 4, len('im.'), 'example2.py') print(script2.completions())
As expected, the first
print() shows a list of completion results while the
print() shows an empty list.
So this is indeed an issue of Jedi. I filed an
issue on the Jedi GitHub
repo. After a while, I got an reply from the author of this project. The reason
is that Jedi can infer the type returned by
Image.open() method since it is
too dynamic to infer. The author suggested that I may use type
to help Jedi infer the type of the return objects. I tried two ways based on
the type hinting
The first method is to use function annotations. I find the source file of
Image.open() in my system and change the following definition
def open(fp, mode="r") :
def open(fp, mode="r") -> Image:
Now the auto-completion works for instance returned by
The second method is to use type hints in inline comment. But the auto-completion does not work anymore. My test script is:
import jedi source = ''' from PIL import Image im = Image.open('test.png') # type: Image im. ''' script = jedi.Script(source, 4, len('im.'), 'example.py') print(script.completions())
The print function shows an empty list.
So for now, you may stick to function annotations if you want type hints to work for Jedi.
License CC BY-NC-ND 4.0