davidhalter / jedi

Awesome autocompletion, static analysis and refactoring library for python
http://jedi.readthedocs.io
Other
5.78k stars 507 forks source link

Autocomplete for chained methods #2023

Open niklassemmler opened 2 weeks ago

niklassemmler commented 2 weeks ago

Just today I figured out that Jedi has been powering my ipython/jupyter/vim experience all along. Thank you :)

To the question. Is it possible to autocomplete chained methods, e.g., for a builder pattern?

On Jedi 0.19.1 I can not get the autocomplete for add_y to work in either ipython or jupyter for the following example:

from typing import Self

class Builder:
    def __init__(self):
        self.x = 0
        self.y = 0

    def add_x(self: Self, x: int) -> Self:
        self.x = x
        return self

    def add_y(self: Self, y: int) -> Self:
        self.y = y
        return self

b = Builder()
b.add_x(2).add_y(5)
#^ this autcompletes
#         ^ this doesn't

I have experimented with -> "Builder": as well with the same result.

Other issues:

Versions

davidhalter commented 2 weeks ago

Self is probably not supported. I'm pretty sure you could work around that by either removing the annotation or returning -> Builder, but that also sounds like a bad idea.

PeterJCLaw commented 2 weeks ago

Removing the Self annotations (both on self and the return) and either adding -> Builder or leaving that empty feel like they should work here, both in ipython and in editors. If that's not working then perhaps something else is going on?

davidhalter commented 2 weeks ago

I agree. Sorry for not reading the issue description properly.

niklassemmler commented 2 weeks ago

I tried again with "Builder" and autocomplete did work. Just not in the more complicated example I have. So I must have mixed sth up when evaluating this earlier. My apologies. Still good to know that Self is not supported.

davidhalter commented 2 weeks ago

I'm reopening, because AFAIK Self is not supported. @PeterJCLaw It might be that I'm wrong here, not 100% sure.

PeterJCLaw commented 2 weeks ago

I believe Self is not supported yet.