Skip to main content

Generates `.pyi` files from Cython modules in a given package directory.

Project description

stubgen-pyx

Automatic stub file generation for Cython extensions.

Installation

pip install stubgen-pyx

Usage

Generate stubs from the command line:

stubgen-pyx /path/to/package

Or use the Python API:

import stubgen_pyx
stubgen_pyx.stubgen("/path/to/package")

Overview

Cython is a widely-used extension language for Python, but type information for Cython modules is often unavailable to static analysis tools. This package generates .pyi stub files that expose type hints and signatures for Cython code, enabling better IDE support and type checking.

Why not use mypy's stubgen?

While mypy's stubgen can generate stubs for compiled extension modules through runtime introspection, it cannot access Cython-specific metadata embedded in the compiled modules. This results in incomplete or inaccurate type information.

stubgen-pyx is designed specifically for Cython and leverages embedded metadata to produce more accurate and complete stub files.

Example

A Cython module like this:

cdef class TestClass:
    """
    This is a class for testing stub file generation.
    """
    a: int

    def __init__(self):
        """
        A docstring for __init__
        """
        self.a = 1

    cpdef b(self):
        """
        A docstring for b
        """
        return self.a

    def c(self):
        """
        A docstring for c
        """
        return self.a

    cdef d(self):
        """
        A docstring for d (this should be ignored)
        """
        return self.a

Generates the following stub file:

class TestClass:
    """
    This is a class for testing stub file generation.
    """
    def __init__(self) -> None:
        """
        A docstring for __init__
        """
        ...

    def b(self):
        """
        A docstring for b
        """
        ...

    def c(self):
        """
        A docstring for c
        """
        ...

Note that cdef methods (like d) are not included since they're not accessible from Python, and public attributes and cpdef methods are properly exposed.

Limitations

Import resolution for cimported types: When types from cimport-ed modules appear in function signatures or class definitions, their imports are not automatically included in the generated stub file.

Workaround: Define a __cimport_types__ list or tuple at the module level containing the types you want exposed:

__cimport_types__ = [SomeType, AnotherType]

These types will then be properly imported in the generated stub file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stubgen_pyx-0.1.3.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stubgen_pyx-0.1.3-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file stubgen_pyx-0.1.3.tar.gz.

File metadata

  • Download URL: stubgen_pyx-0.1.3.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for stubgen_pyx-0.1.3.tar.gz
Algorithm Hash digest
SHA256 1d53f5d4ba7532967cce47844105c8e9a95ae348784f8c9b6f257fd56412e711
MD5 093848033d312d3156b57bff8e3a543c
BLAKE2b-256 1d33e143bfc44bf73448ad91012f17c2b6ecb1e71d82dfd73222dc851fbbed83

See more details on using hashes here.

File details

Details for the file stubgen_pyx-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: stubgen_pyx-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for stubgen_pyx-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 fb99712a2222468c70c322a87a891f92dee6d2e76b67ef4412567eea61628398
MD5 184dc69890fa998f55fe74cfabb76752
BLAKE2b-256 b69edfeba907f09ae7bb2142b32bbf1a201a28f4fbc02c2bc2538d1652d64507

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page