5

I have tested a variety of Python IDEs for their code completion capabilities. Does one exist that can do code completion in the following case:

def foo(x):
    print x.read()  // remove this line, and type in "print x."

def main():
    n = open("c:\\python27\\test.py");
    foo(n)

The IDE has to figure out that foo is invoked somewhere in the current module with a parameter which was return value from a call to open() which it would have to assume is a call to file.open, and thus, returns a file object. Thus, x. followed by Ctrl+Space would invoke code completion and show that x, as a file object, can support any file object method, including read().

I have found that PyScripter, for example, can do this:

    n = open("c:\\python27\\test.py");
    n. // hit <ctrl+space> after n.

The above code completion scenario works because PyScripter does some special code completion logic to determine that n is a file object, but there doesn't appear to be a way to infer object types or available methods, from static analysis, that will deduce the types of parameters.

Secondly, if nothing like this is available in Python 2.x, in Python 3.x, now that there are static type hints, does any IDE support them?

   def foo(x:'file'):
        print x.read()  // remove this line, and type in "print x." and hit ctrl+space
4
  • The first is impossible. foo() can be called with anything. That it is called with a file once is no guarantee. Type hints solve it, but I don't know of any IDE that supports it yet, but I'm sure that will come. Commented Oct 14, 2011 at 22:52
  • Agree with Lennart, this is one of the tradeoffs of working with a language that is not strongly typed. Commented Oct 14, 2011 at 23:37
  • Function annotations should be used with types, not strings... Commented Oct 15, 2011 at 5:34
  • Thanks, I didn't know what they should be used to hold. Commented Oct 16, 2011 at 1:42

2 Answers 2

8

The only one I think can do (almost) that is PyCharm

But you must give it some hints:

Python2


If you're on Python 3, the IDE will check function annotations like that:

Python3

(The file class was removed from python 3 so I made an example with string)

It's not a free IDE but, IMHO has the best code completion available.

Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly what I was looking for. I should have guessed that maybe someone used function-docstrings for that purpose. They are beautiful things, docstrings. I hope to be able to imitate it's docstring-parsing codecompletion then, in an open source product, but for now, I'll consider purchasing PyCharm, it looks great.
1

WingIDE has such type inference as well, it's not open though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.