Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 251
Alfa07 Avatar asked Sep 25 '09 15:09

Alfa07


3 Answers

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).

like image 158
Alex Martelli Avatar answered Oct 31 '22 11:10

Alex Martelli


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.

like image 41
Damien Pollet Avatar answered Oct 31 '22 12:10

Damien Pollet


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

like image 43
Don Stewart Avatar answered Oct 31 '22 11:10

Don Stewart