This is just a quick tip how to enable auto completion in PyDev for indirectly created objects.
By default, PyDev has no problems in presenting you the possible object attributes and methods on a variable if that variable gets a new object instance assigned directly in the code.
For example, when you first type …
mybook = Book()
… and in the next line you enter …
… PyDev will dutifully present you the statically declared features for the Book class in a popup.
If you however get that Book instance as the result of another method, e.g. …
mybook = bookstore.findBook("foobar")
… PyDev currently seems to be helpless.
Because of Python’s dynamic nature it is of course a bit harder for an IDE to figure out what kind of objects may a method. But PyDev could for example honor the @return / @rtype PyDoc annotations (which you can add to the findBook’s method declaration) but currently it just does not.
To still have auto-completion you have two options:
- Temporarily instantiate a Book object in the code.
mybook = Book() mybook = bookstore.findBook("foobar") mybook. #--> now you have auto completion
This has the drawback, that if you forget to remove the first line before you finish coding an unneccessary Book instance will be created on every execution during runtime.
- Use an assertion to ensure that the mybook variable contains an object of type Book
mybook = bookstore.findBook("foobar") assert isinstance(mybook, Book) mybook. #--> now you have auto completion
5 thoughts on “Leveraging PyDev’s auto-completion for indirectly created objects”
PyDev now (as of 2.8.0) does check @rtype or :rtype (epydoc/sphinx) in docstrings!
assert + pydev = dreamy
Another method is to cast the object back to its type:
mybook = bookstore.findBook(“foobar”)
mybook = book(mybook)
Or more simply:
mybook = book(bookstore.findBook(“foobar”))
Thank you ! Exactly the thing that i need !