Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pydev Code Completion for everything

In many cases (such as function parameters) Pydev doesn't statically know the type of a variable. Therefore code completion (after . or when using ctrl+space) doesn't work.

In most cases, you know what type will be in run-time as you are designing the software. Is there a way to hint Pydev to code completing it correctly?

I guess this may require a specific Pydev feature, or perhaps even a new Python PIP.

This is actually seems to be a generic problem with all dynamic languages...

UPDATE:
Perhaps an example is in place for clarification:

def some_func(a_list, an_object):
    a_list.app        # Here I would not get code completion for append

An example of something that could work, if Pydev (or a PIP) would support it:

from someobj import SomeObject
def some_func(a_list, an_object):
    # typecast: a_list=list
    # typecast: an_object=SomeObject
    a_list.app        # Now code completion would show append

I'm not endorsing this specific method - it's just an example of a system that could work. Again, of course this should not be mandatory - but sometimes the lack of the possibility to hint the type is annoying.

like image 959
Jonathan Livni Avatar asked Jun 02 '11 18:06

Jonathan Livni


3 Answers

[Edit]

Since PyDev 2.8.0, it can use docstrings and comments to discover the type of objects.

See: http://pydev.org/manual_adv_type_hints.html for details on the supported formats.

[Before PyDev 2.8.0]

Previously, it only supported assert isinstance calls (and this still works):

assert isinstance(a_list, list) 

PyDev will be able to recognize it and properly provide code-completion for it (note that you can run Python without the assertions later on if you find it's making your code slower: What does Python optimization (-O or PYTHONOPTIMIZE) do? )

like image 123
Fabio Zadrozny Avatar answered Sep 27 '22 20:09

Fabio Zadrozny


As of PyDev 2.8.0 can use Sphinx or Epydoc comments for code completion: http://pydev.org/manual_adv_type_hints.html

like image 22
eakst7 Avatar answered Sep 27 '22 21:09

eakst7


If you use PyCharm, you can pick either epydoc or sphinx docstring style and specify types of parameters and function return values according to that style (further discussion)

like image 31
Jonathan Livni Avatar answered Sep 27 '22 20:09

Jonathan Livni