This project has moved and is read-only. For the latest updates, please go here.

Code completion for generated classes


Dear PTVS team,

we have a python class wrapping an external library.
At run-time, this class loads the external library and generates its methods dynamically (using ast).
As such, these methods are "invisible" to PTVS.

In PTVS, is there a way to achieve code completion for these methods or dynamically generated objects in general? For example a data format or an API in which they could be added to the Completion DB?

Petr Wolf


Zooba wrote Jun 13, 2014 at 7:39 PM

If you know the code ahead of time, you can provide Python files like this:
if False:
    def func(a, b, c):
        return str()

    func(int(), float(), list())
This will make func appear as func(int, float, list) -> str, though Go To Definition will be broken (then again, it's not working now...)

You could also put these definitions in a separate file and have
if False:
    import _ide_definitions
This way you don't have to parse or distribute the file when running.

Zooba wrote Jun 13, 2014 at 7:40 PM

(Note that this works because we don't try and figure out whether an if condition is true or false - we just assume that all conditions will run at some point. I believe most IDEs behave like this, and there are certainly projects out there already using it for better completions.)

Zooba wrote Jun 13, 2014 at 7:46 PM

Oh, there are two other ways this can be done, but neither is as simple as the above method:

If you include a .pyd then we will load it and rely on dir() and the inspect module to find out its contents. This means you can dynamically generate the members and we will store them in the completion DB. This is only going to work for installed packages though, and the end result is basically identical to the if False method.

The other alternative involves writing a C# DLL that injects itself into our process. Our analysis engine is extensible and you can implement special object types in C#. We've done this with IronPython to support types that come from .NET and with Django to support completions in templates. It isn't an easy process, however, so you'd have to be really keen to go this route. We're happy to help though, and to make changes to PTVS to make it easier to extend.

PetrWolf wrote Jun 13, 2014 at 8:11 PM

Oh wow! Thanks for the quick feedback, Zooba!
I think we'll explore all these options and see what works the best.