22

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.

Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
  • You mean, apart from adding the type annotations whose nonexistence is one of the upsides of dynamic languages? –  Jun 02 '11 at 20:05
  • @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 Livni Jun 03 '11 at 05:54
  • The function arguments are generic and duck-typed, what would you expect it to return? – Cat Plus Plus Jul 24 '11 at 10:45
  • @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 Oct 22 '15 at 01:03

5 Answers5

22

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

Community
  • 1
  • 1
Fabio Zadrozny
  • 24,814
  • 4
  • 66
  • 78
  • It's slightly less elegant than a decoration or comment as it can actually crash your program if `a_list` isn't really a `list`. I would still like to see a PIP solving it elegantly in the future... – Jonathan Livni Sep 28 '11 at 10:45
  • 1
    Technically, you're right, but it doesn't work across function calls and thus isn't very useful. This answer isn't bad, but I don't think it answers the question. – Mechanical snail Oct 20 '11 at 03:50
  • I'll be monitoring for other answers, so if anyone has another idea, go ahead and post them – Jonathan Livni Oct 20 '11 at 07:59
  • @Jonathan I think it's a good thing in most cases if it crashes the program because it means the original assumption about its run-time type was incorrect, which would possibly lead to more serious bugs anyway (see Fail-fast on Wikipedia). Note that PyDev is smart enough to understand `isinstance` with multiple types so if you know that it could be multiple types at design type, you should include those types so that your program should never fail if the variables are of the expected types. Ex: Use `assert isinstance(my_var, (int, str))` if you know it could be either an `int` or `str`. – user193130 Jan 29 '14 at 19:39
  • @Fabio: Does PyDev support some sort of "list of type x". such as that in PyCharm? http://www.jetbrains.com/pycharm/webhelp/type-hinting-in-pycharm.html#d232466e486 Any plans for supporting it, if not already? – Zoran Pavlovic Oct 13 '14 at 19:32
  • There are plans, but it's still not supported. – Fabio Zadrozny Oct 14 '14 at 13:17
5

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

eakst7
  • 641
  • 6
  • 12
1

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)

Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
0

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.

Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
Zoran Pavlovic
  • 1,166
  • 2
  • 23
  • 38
  • 1
    This method has too many downsides, e.g. you can't leave it in the code for future editing, you're bound to forget these lines in the code before commits... – Jonathan Livni Nov 14 '12 at 12:32
0

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.

Zompa
  • 448
  • 4
  • 8