Question

Is there a way to help PyDev code completion by telling it the type of a variable?

With PDT, you can use PHPDoc-like syntax for such purpose:

/* @var $my_var MyClass */
$my_var = myFunction();
// PDT is able to figure out that $my_var is a MyClass object.

But till now, I cannot figure out how to do the same in python.

Was it helpful?

Solution

Nope (see the docs). It looks like PyDev does completion of imported stuff and language keywords.

It doesn't seem like this would come up a lot though. The variable in question seems like it would only be unknown to pydev if it were passed in as a function argument with no default value.And, if you have a function operating on your own class, it seems like that should be a class member (so autocomplete would already work).

OTHER TIPS

The assert trick does not seem to work for me with PyDev 2.2.2 ; it is still supposed to ?

However another trick I tried and that work is the following :

class Foo(object):
    def __init__(self, bar):
       self.bar = bar
       # Tricking PyDev
       if (not self.bar):
          self.bar = Bar()
          raise Exception("Bar should not be null")

In all cases, it looks pretty hacky, and I would love a cleaner way to do things (documentation, annotation, whatever)

Actually, you can if you do an assert isinstance()

E.g.:

a = function()
assert isinstance(a, MyClass)
a. <- would get the proper completions

Note that Pydev does analyze the return of the functions, so, it's possible that it knows that on a number of cases.

Also, that shouldn't have runtime penalties if you use python -O (which will remove the asserts)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top