2

I have a compiled a Python extension. The resulting binary mylib.so file can be imported in the Python script and works fine.

Now I am wondering how to write the interface stub file mylib.pyi such, that pylint and the Python language server used in VS Code can use it?

For now the native library is just in the root of my scripts. When putting the mylib.pyi next to it pylint ignores it.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Matthias
  • 1,055
  • 2
  • 14
  • 26

1 Answers1

2

I know this question is a year old but the answer might help the next person. I managed to do this by writing the following setup file:

from distutils.core import setup, Extension
extension = Extension(
    name = 'dummy',
    sources = ...,
    ...
)
    
setup(
    name = 'dummy',
    ext_modules = [extension],
    packages = ['dummy'],
    package_dir = {'dummy' : './stub'},
    package_data = {
        'dummy': ['__init__.pyi', 'py.typed']
    }
)

From what I understand, the stub directory is treated as a separate package and installed alongside the extension. I've only tested it with vscode but I believe all IDEs should be able to parse it. Full code can be found here.

M47
  • 400
  • 2
  • 13