10

How can I create .pyi files from cython source code (.pyx)?

tamuhey
  • 2,904
  • 3
  • 21
  • 50
  • 3
    I 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
  • 2
    This 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
  • 1
    I 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 Answers1

2

You can use the cythonbuilder package for that, https://pypi.org/project/cythonbuilder/

  1. Install cythonbuilder: pip install cythonbuilder
  2. Use the command cythonbuilder.exe build -y to automatically build all .pyx file in current working directory and generate .pyi files.

Example cythnobuilder project

Iyad Ahmed
  • 80
  • 2
  • 12
  • 1
    CythonBuilder 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
  • Amazing work! ~~filler text to post comment~~ – Iyad Ahmed Jul 31 '23 at 02:00