How can I create .pyi
files from cython source code (.pyx
)?
Asked
Active
Viewed 1,010 times
10

tamuhey
- 2,904
- 3
- 21
- 50
-
3I think there's been a few ([1](https://github.com/cython/cython/issues/3150) [2](https://github.com/antocuni/capnpy/issues/41) [3](https://github.com/cython/cython/issues/1826)) people asking for this over the last few years, but nobody's actually done it yet... It's possible that someone may have a better answer, but I think the current status is "no, but it would probably be possible to integrate this with Cython if someone wants to write the code" – DavidW Sep 29 '19 at 13:37
-
2This project does it: https://pypi.org/project/cythonbuilder/ – Iyad Ahmed Mar 05 '23 at 19:18
-
@IyadAhmed I think cythonbuilder generates HTML files, doesn't it? – Winand Mar 15 '23 at 13:53
-
1I tried it and it generated .pyi files ^^, maybe it had an option for HTML but didn't @Winand encounter it ^^ – Iyad Ahmed Mar 16 '23 at 20:07
-
1@IyadAhmed you are right. `cybuilder build` command generates pyi by default. Maybe you can write an answer to this unanswered question?:) – Winand Mar 17 '23 at 07:23
-
Ok will try to do – Iyad Ahmed Mar 17 '23 at 11:33
1 Answers
2
You can use the cythonbuilder package for that, https://pypi.org/project/cythonbuilder/
- Install cythonbuilder:
pip install cythonbuilder
- Use the command
cythonbuilder.exe build -y
to automatically build all .pyx file in current working directory and generate .pyi files.

Iyad Ahmed
- 80
- 2
- 12
-
1CythonBuilder did not fit naturally into my setup.py files. It needed some modification to support some cython types. All the parsing code is located in: https://github.com/mikehuls/cythonbuilder/blob/main/cythonbuilder/pyigenerator.py It does not support all cython syntax but it was workable with a few updates. Eventually, I created my own parser with pyparsing. It is a bit overkill for creating stub files but allows for modifications. It supported most of cython stub related syntax. https://github.com/RaubCamaioni/CythonPEG/blob/main/cython_peg.py – Mountain Jul 30 '23 at 21:37
-