views:

223

answers:

2

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.

+1  A: 

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).

Seth
+1  A: 

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)

Fabio Zadrozny