Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pydev

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!

like image 260
Jim L. Avatar asked Dec 05 '25 19:12

Jim L.


2 Answers

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.

like image 132
g.d.d.c Avatar answered Dec 08 '25 09:12

g.d.d.c


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.

like image 21
Ryan Avatar answered Dec 08 '25 07:12

Ryan