Metadata-Version: 2.4
Name: stubgen-pyx
Version: 0.1.2
Summary: Generates `.pyi` files from Cython modules in a given package directory.
Author: Jonathan Townsend
License: MIT License
        
        Copyright (c) 2025 Jonathan Townsend
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/jon-edward/stubgen-pyx
Keywords: cython,stubgen,type stubs,pyi,type hints
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: Cython>=3.0.11
Requires-Dist: setuptools>=75.0.0
Provides-Extra: test
Requires-Dist: pytest>=7.4.0; extra == "test"
Dynamic: license-file

# stubgen-pyx

Automatic stub file generation for Cython extensions.

## Installation

```bash
pip install stubgen-pyx
```

## Usage

Generate stubs from the command line:

```bash
stubgen-pyx /path/to/package
```

Or use the Python API:

```python
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:

```cython
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:

```python
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:

```python
__cimport_types__ = [SomeType, AnotherType]
```

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