Search code examples
pythonmypypython-extensionstypeshed

How to generate .pyi files for a compiled Python extension?


I build a compiled Python extension (.pyd file) with C++ and pybind11. I would like to generate a single Python interface .pyi file for my .pyd file.

There are a few similar questions referring to the mypy stubgen module, however, this one produces a UnicodeError trying to run stubgen Dummy where Dummy is Dummy.pyd file:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x90 in position 2: invalid start byte

Another project, make-stub-files does not work with .pyd files at all, giving the ('not a python file' error.

Are there any tools that could let me generate .pyi files either from the source .cpp files or from compiled .pyd files?

The issue is registered in the mypy GitHub repository.


Solution

  • The issue was registered in the mypy GitHub source repository and fixed in a pull request.

    The reason for the issue was that when mypy was trying to figure out whether a given module is a Python extension it checked for the .so file extension used on Linux, but did not check for the .pyd file extension used on Windows.