polynote icon indicating copy to clipboard operation
polynote copied to clipboard

improve python type suggestion support

Open antonkulaga opened this issue 5 years ago • 4 comments

Even though I am overall happy about Scala type suggestion, the python one is really weak. It does not take into consideration the fields that I have in my type hints

antonkulaga avatar Jan 28 '21 20:01 antonkulaga

@antonkulaga Can you please provide an example of the type hints you're defining and the suggestions you expect? Thanks!

jonathanindig avatar Jan 29 '21 02:01 jonathanindig

@jonathanindig , here is an example: Imaging the following setup

from dataclasses import *
@dataclass
class Bar:
    surname: str
    def show(self):
        print(self.surname)

@dataclass
class Foo:
    bar: Bar
    name: str

test_foo = Foo(bar = Bar("Surname"), name= "name")

When I type the function:

def test(foo: Foo):
    foo.bar.show()
    #often (but not always) when I start typing foo. bar is not autosuggested.
    #and same for foo.bar.show() show is often not autosuggested 
    #even though from @dataclass it is well known what fields and functions are!
    return foo.bar.surname

antonkulaga avatar Feb 21 '21 22:02 antonkulaga

I believe this is a limitation of jedi, the library that we use to provide completions (along with everyone else).

See:

>>> from dataclasses import *
>>> @dataclass
... class Bar:
...     surname: str
...     def show(self):
...         print(self.surname)
...
>>> @dataclass
... class Foo:
...     bar: Bar
...     name: str
...
>>> test_foo = Foo(bar = Bar("Surname"), name= "name")
>>> script = """
... def test(foo: Foo):
...     foo."""
>>> jedi.Interpreter(script, [globals()]).complete(len(script.splitlines()), len(script.splitlines()[-1]))
[<Completion: __annotations__>, <Completion: __class__>, <Completion: __dataclass_fields__>, <Completion: __dataclass_params__>, <Completion: __delattr__>, <Completion: __dict__>, <Completion: __dir__>, <Completion: __doc__>, <Completion: __eq__>, <Completion: __format__>, <Completion: __ge__>, <Completion: __getattribute__>, <Completion: __gt__>, <Completion: __hash__>, <Completion: __init__>, <Completion: __init_subclass__>, <Completion: __le__>, <Completion: __lt__>, <Completion: __module__>, <Completion: __ne__>, <Completion: __new__>, <Completion: __reduce__>, <Completion: __reduce_ex__>, <Completion: __repr__>, <Completion: __setattr__>, <Completion: __sizeof__>, <Completion: __str__>, <Completion: __subclasshook__>, <Completion: __weakref__>]

(note that bar and name are missing from the completions).

Try putting the same code into Jupyter and you'll see the same issue (no completions at foo. inside the test()).

That being said, we do have an opportunity here to do better than Jupyter 😄 , as using jedi.Script does seem to get the proper results. We can look into doing that, though integrating the Script API instead of the Interpreter has its own issues to consider as well...

I have created this issue to track that: https://github.com/polynote/polynote/issues/1050

jonathanindig avatar Feb 23 '21 01:02 jonathanindig

Try putting the same code into Jupyter and you'll see the same issue (no completions at foo. inside the test()).

Well, the key selling point of polynote is beeing better than Jupyter in completions together with better interoperability between languages.

I believe this is a limitation of jedi

Maybe it is possible to open an issue there?

antonkulaga avatar Feb 24 '21 11:02 antonkulaga