How can I imrpove completion for Python types generated at runtime?

Answered

I am a developer of Python.NET (bidirectional Python <-> .NET bridge). I'd like to make it possible to get completion for .NET classes after the bridge is established. Typically this is how it works:

# here there's no System module
import clr # this loads .NET into Python

for net_lib in []: # optional
clr.AddReference(net_lib)

# at this point all .NET namespaces are available as modules
# so I can do
from System import Uri

u = Uri("https://jetbrains.com")
print(u.Host)

Ideally, I'd love to be able to autocomplete every module (e.g. .NET namespace), class and member name.

Does PyCharm support completion for classes created at runtime via non-Python-code means at all?

If it does, how can I tell it to do import clr (at least), and hopefully also a few AddReference calls before it would start indexing the environment so it could actually show the completion?

0
1 comment

PyCharm uses static code analysis. So no, it cannot get it from the runtime, unfortunately. There is a similar feature request about the Python Console https://youtrack.jetbrains.com/issue/PY-40588/Add-autocompletions-from-the-running-console-to-the-editor

Feel free to file another feature request if needed.

At the moment, you can write stub files (*.pyi). PyCharm can use those to provide code completion in case there is no Python code that can be used for static code analysis. That's why we bundle typeshed https://github.com/python/typeshed, to have code completion for the code that isn't available for the analysis.

0

Please sign in to leave a comment.