Jim L.
Jim L.

Reputation: 6529

In PyDev, how can I get autocompletion for an instance returned by a method?

When I use a method of Class-A to return an instance of Class-B, PyDev will not offer me auto-completion for the instance of Class-B. Is there a way to make this work so I don't potentially mistype a method name or forget an argument? Otherwise, PyDev loses much of its value!

Upvotes: 3

Views: 1134

Answers (3)

Christophe Roussy
Christophe Roussy

Reputation: 17039

You can use Sphinx doc comments:

def get_date(...):
    """:rtype date"""
    ...
    return date

For more see: http://www.pydev.org/manual_adv_type_hints.html

Upvotes: 0

Ryan
Ryan

Reputation: 80

Asserting isInstance breaks the "Better to ask forgiveness than permission" paradigm in python.

Pydev understands specific decorators in docstrings for type hinting.

Here's a set of examples: http://pydev.sourceforge.net/manual_adv_type_hints.html

class Foo(object):
    def method(self):
        pass

    def otherMethod(self):
        pass

def returnFoo():
    return Foo()

"""@type fooInstance: Foo"""
fooInstance = returnFoo()

I haven't had much luck with the return type (using the epydoc syntax) but haven't tried much, but whatever the object is assigned to can be declared by the type you are expecting as in the example above.

Upvotes: 2

g.d.d.c
g.d.d.c

Reputation: 48028

I wonder if you're using some combination of classes / containers that hinders pydev's ability to predict your return value's type. This super-simplistic example works on my system and I get full code completion on inst:

class A(object):
  def __init__(self, params = None):
    self.myBs = [B() for _ in range(10)]

  def getB(self):
    return self.myBs[5]

class B(object):
  def foo(self):
    pass

inst = A().getB()
# Auto-complete broken.  PyDev thinks inst is a list.
assert isinstance(inst, B)
# Auto-complete working again.

After additional detail, the assert statement is necessary to trigger PyDev's autocomplete functionality.

Upvotes: 5

Related Questions