How to get autocompletion for python code?

I want to get auto completion for python code (vscode I am using).

For this line

p = figure(title="Simple line example", x_axis_label='x', y_axis_label='y')

There is no auto completion on p.line.

Checking the source, it seems there is no line function defined on p. The way bokeh does is through reflection on __getattr__.

Given that, how to get autocompletion? Does bokeh have some sort of types-bokeh package for typing use?

@pinkfrog sorry I have no idea what wierdness VSCode has going on. Contrary to what you suggest, the glyph methods are in fact just normal methods (including with type annotations) defined the figure class as you can see here (they are brought in via figure inheriting GlyphAPI)

In any case, tab-completion works fine in IPython, for example:

and also in the standard interpreter:

Since this seems like a VSCode-specific issue you might have better luck on a VSCode-specific help forum.

@pinkfrog If I define this in VSCode explicitly:

p: figure = figure()

then auto-completion works.

Without the hint, VSCode determines that p is a type Model which is technically true, since Model is in fact a superclass of figure. Perhaps Pylance (the tool VSCode uses for auto-completion) is getting confused by the fact that Model has a metaclass as well. I’m not sure, but if so there is nothing we can do about that on our end. And given the fact that Python itself and IPython get things correct, I would say it actually seems like a bug in Pylance.

Well, it’s not just having a metaclass, since this attempt at making a minimal reproducer similar to the Bokeh class hierarchy works fine:

But I really don’t know what it is that is different about the actual codebase that Pylance is tripping on. Again, it seems like a Pylance bug, regardless.

Thanks @Bryan .

Adding the figure type annotation really helps!

Btw, I also tested with PyCharm. It is somehow confused (with the one without type annotation), it gives a list of definition candidates, see picture.

All the glyph methods have a lot of optional parameters. I guess PyCharm explicitly enumerates all of the combinations. I am not sure how useful that is, maybe there is some way to turn that off.