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.2.tar.gz (10.3 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.2-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: stubgen_pyx-0.1.2.tar.gz
  • Upload date:
  • Size: 10.3 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.2.tar.gz
Algorithm Hash digest
SHA256 48d5a282ac8997326d7763f0b94eaa631ca8771eb7cc9886c7e873bd90323604
MD5 cf365431f6a12a5f6e94bb5209e70ef1
BLAKE2b-256 3b558017624c0671b025a5c22bb859dab600743b2ba0c45e61031306515b831d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: stubgen_pyx-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.3 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 363c49b48b0f29f5f9c7824501d24cd26f6c3a231cc265c9e2066abdfc25d415
MD5 5054e6f80f8b90de5c13977d2afa5bf1
BLAKE2b-256 e08b7f4d436ebc702ccd72b046f807ce75eeff6c43d475955c643350c5e43e55

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