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

Uploaded Python 3

File details

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

File metadata

  • Download URL: stubgen_pyx-0.1.4.tar.gz
  • Upload date:
  • Size: 11.1 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.4.tar.gz
Algorithm Hash digest
SHA256 1ee1f0eef3eee13d220d3c1586d861b267811eecbc8b41e6f7814801061c6209
MD5 7304c063b6ec5146de269e4b95b6555f
BLAKE2b-256 2fec7a692de1ace23313690ea56b8106fe865d49e1592c009a0d0c08c5f0285c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: stubgen_pyx-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 11.2 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d176296f9033a48536f930d3f171e7d32cd94b726c2bfb73ed0b107904555160
MD5 4c68c4cb2a2c9b515a94bb6844bc0077
BLAKE2b-256 58aadbfc0a2a607e6cc1c8b1034fab49cccc344d7acda9cb9342e2d83dbf3e06

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