简体   繁体   中英

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?

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 eg wingware's in a while, so maybe they do now).

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.

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.

适当的处理需要Python的类型系统,这将是一个有趣的研究问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM