views:

471

answers:

2

I'm using Komodo Edit for Python development, and I want to get the best out of the auto complete.

If I do this:

a = A()
a.

I can see a list of members of A.

But if I do this:

a = [A()]
b = a[0]
b.

It does not work. I want to be able to do this:

a = [A()]
b = a[0]
"""b

Type: A
"""
b.

So how can I tell the auto complete that b is of type A?

+2  A: 

I don't think that you'll have much luck with this. The problem is that it's really quite difficult to statically infer the type of variables in Python except in the simplest of cases. Often the type isn't known until run-time and so auto completion isn't possible.

The IDE does some static analysis to work out the obvious and best guesses, but I'll bet it isn't even trying for elements in a container. Although we can work out that b is of type A even small variations to your code can make it unknowable, especially as it's in a mutable container.

Incidentally I've tried this on the full Komodo IDE and it's no better. I hear that Wing IDE has excellent code completion, but I wouldn't be sure it could do any better either.

Scott Griffiths
I understand that automatic type inference is difficult. What I want to do is manual type inference.I'll take a look a look at Wing in the mean time.
Gary van der Merwe
Ah, I understand now. My first thought was the assert that interjay mentions, but it doesn't work for Komodo. The only thing I can get to work is adding `b = A(b)`, but that's horrible, isn't guaranteed to work and has side effects.
Scott Griffiths
+4  A: 

This doesn't really answer your question, but with Wing IDE you can give hints to the type analyzer with assert isinstance(b, A). See here. I haven't found a way to do it with Komodo, though apparently it's possible when writing PHP or JavaScript.

Update:

I've found a way to trick Komodo into doing this:

if 0: b=A()

This works (at least on Komodo 5.2) and has no side effects, but is sure to confuse whoever reads your code.

interjay
What a horrible hack! I'm just jealous that you found it first :) I'd +1 if I hadn't already.
Scott Griffiths
I think I'm going to change to Wing IDE. The assert isinstance(b, A) syntax works nicely.
Gary van der Merwe