22
votes

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.

5
You mean, apart from adding the type annotations whose nonexistence is one of the upsides of dynamic languages? - user395760
@delnan - yes! Don't get me wrong, I fully support not having the annotations as mandatory. I was thinking on the lines of being able to add them sporadically, perhaps as comments (as they are not part of the language of course) - Jonathan
The function arguments are generic and duck-typed, what would you expect it to return? - Cat Plus Plus
@delnan - I suspect it is more of a downside to declare variables. Has anyone done a study on this to determine if effort is really saved by not declaring variables? I've seen so many bugs in declaration-less languages. (For the record, declaring variables doesn't make a language less dynamic. Even Javascript has "var"!) - Chris

5 Answers

22
votes

[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? )

5
votes

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

1
votes

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)

0
votes

What I usually do to circumvent this.

def func(my_list_param):
    my_list_param = []
    my_list_param.appe # at this stage I would get code completion.

Just remember to delete the variable initialization when testing or committing. Oh and btw, the response marked as an answer doesn't seem to work for me.

0
votes

If you are using Python >= 3.5 in your code you can specify parameters type in function declarations (26.1. typing — Support for type hints), so you code could be:

from someobj import SomeObject

def some_func(a_list: list, an_object: SomeObject):
    a_list.app           # Code completion works
    an_object.a_method() # Code completion works

This type of syntax, has no effects on execution and does not make any check on the real type passed o the function, but make your code more readable and makes the code completion work.