7

My question is probably stupid and I hope somebody has succeeded in solving this issue.

Sometimes I cannot see right suggestions in auto-completion box (Eclipse 3.5.2, PyDev 1.5.7). For example:

import email
fp = open('my.eml', 'rb')
msg = email.message_from_file(fp)

msg now is a Message object. And functions like get_payload() works fine.

msg.get_payload()

But I don't get get_payload() in auto-completion list.

I think PyDev has no idea of what msg is, so it doesn't know what to show.

Maybe I should import something else, not only email module?

Thanks in advance!

kishkin
  • 5,152
  • 1
  • 26
  • 40

3 Answers3

4

I struggled with this question quite a bit too, until I came across this link. I used the second solution suggested in that link, and it works like a charm.

Basically you need to insert assert isinstance(msg, Message) after you get msg from the function call.

stacksia
  • 631
  • 6
  • 10
  • Thank you for the solution. But could not make it work. Did all you said to do. – kishkin Sep 13 '12 at 08:02
  • 1
    Can you post the code you are testing? I have seen [other posts](http://stackoverflow.com/questions/12382193/how-to-declare-variables-type-in-pydev/12409893#12409893) suggesting the same solution, and it seems to have worked for others as well. – stacksia Sep 13 '12 at 15:37
3

Chances are, the current PyDev build hasn't gone to a point to be able to extract from a function (message_from_file() in your case) to know what kind of object it returns in order to provide auto-completion hinting.

See http://sourceforge.net/projects/pydev/forums/forum/293649/topic/3697707.

Edit: I believe there is interest in PyDev to support the new Python 3 function syntax, PEP 3107, which will solve some of your problems ... in the future.

Xavier Ho
  • 17,011
  • 9
  • 48
  • 52
  • 1
    So, I just need to `import __future__`? ;) Ok, we'll wait. Thank you, Xavier! – kishkin May 21 '10 at 10:52
  • About sourceforge link. I get the thing that PyDev cannot get specifications from compiled files. But there are also source .py files, not only .pyc. And I can actually Ctrl+click from `message_from_file` to the `Message`. But there is ambiguity at the second step: PyDev does not know where this `Parser` class came from. I think this causing the issue with autocompletion. – kishkin May 21 '10 at 11:01
  • 1
    The real issue is from the fact that, PyDev cannot resolve what type of objects any function it's returning. It's a very complicated, possibly costly, operation. It's one of the downsides of using a dynamically typed language. Ah well. || And we basically agree. – Xavier Ho May 21 '10 at 11:04
1

I know @type in docstring works. As in:

from collections import deque

def foo(a):
''' code completion sample
@type a: deque
'''
return a.popleft()  # Code completion will work here

I have not been able to find a way to do it inline within code (except in ways mentioned elsewhere where you simply pretend to assign the variable an instance of a type) as in:

from collections import deque

def foo(a):
''' code completion sample '''
if false: a = deque()
return a.popleft()  # Code completion will also work here

But I'm not fond of this method because it probably imposes some performance / code size penalty. I don't know / haven't checked if Python is smart enough to remove this assignment during compile time.

Thanks to SiSoie, here's a link to page explaining possibilities.

Community
  • 1
  • 1
velis
  • 8,747
  • 4
  • 44
  • 64