Alfa07
Alfa07

Reputation: 3404

Python type inference for autocompletion

Is it possible to use Ocaml/Haskell algorithm of type inference to suggest better autocompletions for Python?

The idea is to propose autocompletion for example in the following cases:

class A:
  def m1(self):
    pass
  def m2(self):
    pass

a = A()
a.     <--- suggest here 'm1' and 'm2'
fun1(a)

def fun1(b):
  b.   <--- suggest here 'm1' and 'm2'

Are there any good starting points?

Upvotes: 10

Views: 1225

Answers (4)

Stephen Swensen
Stephen Swensen

Reputation: 22307

The first case is "easy", and I bet JetBrains' PyCharm can do that. I've not used PyCharm, but I do use IDEA, also by JetBrains, for Groovy-based development (a dynamic language), and it has very good, very aggressive auto-completion. The second case would be more difficult, you want the IDE to infer the type of fun1 based on its use. In a language without generics, this may be reasonable. However, in F# / VS2010 at least (which likely would be similar to OCaml/Haskell here), the compiler / IntelliSense infer fun1 to have the signature 'a -> 'a (that is, a function which takes a generic type 'a and returns a generic type 'a) so IntelliSense is very slim.

Upvotes: 1

Damien Pollet
Damien Pollet

Reputation: 6598

You could have a look at ECompletion and OCompletion in Pharo Smalltalk. The compromises will probably different for python, but educated guesses with some conservative type inference work in practice. It also depends if you want completion to replace browsing the code/documentation, or to assist typing and to avoid typos.

I think in Pharo, if the message is an explicit send to a class (SomeClass m) then of course it will propose all messages in that class and its superclasses. Else, it just guesses all methods names in the system that match the typed prefix, and that works well. OCompletion adds a bit of heuristic prioritization based on editing history.

Upvotes: 2

Don Stewart
Don Stewart

Reputation: 137997

A proper treatment would require a type system for Python, which would be (is?) an interesting research problem.

Upvotes: 1

Alex Martelli
Alex Martelli

Reputation: 882691

Excellent discussion, with many pointers, here (a bit dated). I don't believe any "production" editors aggressively try type-inferencing for autocomplete purposes (but I haven't used e.g. wingware's in a while, so maybe they do now).

Upvotes: 9

Related Questions