Search code examples
pythonvisual-studio-codepybind11pylance

Pylance: wont find stubs for native library with submodules


Edit: I also posted this question as an issue on pylance-release github repo, which might be better suited to find an answer.

I'm having issues with Visual Studio Code python language server, which cannot find the stubs for a python binding library I am developing (https://github.com/pthom/lg_hello_imgui). I think the issue might be linked to the fact that my library expose several submodules.

I made a minimum reproductible example here: https://github.com/pthom/pylance_test

When using PyCharm or CLion, I can easily navigate to the stubs I defined for my library, and also in its submodules stubs.

However, when using Visual Studio Code, it is less reliable:

  • when using the Jedi language server I can navigate to the functions definition, although it might fail after a few minutes of usage.
  • when using the Pylance language server, it always fails

I'm using a Mac M1. I've had this failure inside OSX, as well as inside windows ARM, with Parallels. Is it linked to my setup?

Here is the installed package structure:

.venv/lib/python3.9/site-packages/lg_hello_imgui/
├── __init__.py
├── _lg_hello_imgui.cpython-39-darwin.so*
├── hello_imgui.pyi
├── imgui.pyi
├── implot.pyi
└── py.typed

_lg_hello_imgui is a native library that include three submodules, defined like this via pybind11:

PYBIND11_MODULE(_lg_hello_imgui, m)
{
    auto module_imgui =  m.def_submodule("imgui");
    py_init_module_imgui(module_imgui);

    auto module_himgui =  m.def_submodule("hello_imgui");
    py_init_module_hello_imgui(module_himgui);

    auto module_implot =  m.def_submodule("implot");
    py_init_module_implot(module_implot);
}

and lg_hello_imgui/__init__.py states:

from lg_hello_imgui._lg_hello_imgui import imgui
from lg_hello_imgui._lg_hello_imgui import hello_imgui
from lg_hello_imgui._lg_hello_imgui import implot

Repo for issue reproduction: https://github.com/pthom/pylance_test (Note: the lg_hello_imgui linked library does not exist as wheel yet, so it may require 1 minute or 2 to build)


Solution

  • I'm answering my own question since I had an answer from the pylance team in the meantime.

    https://peps.python.org/pep-0484/#stub-files specifies that

    Modules and variables imported into the stub are not considered exported from the stub unless the import uses the import ... as ... form or the equivalent from ... import ... as ... form. (UPDATE: To clarify, the intention here is that only names imported using the form X as X will be exported, i.e. the name before and after as must be the same.)

    So I had to input this into the init.pyi file:

    from . import hello_imgui as hello_imgui
    from . import imgui as imgui
    from . import implot as implot