Skip to content

dev-0.17#369

Draft
termoshtt wants to merge 4 commits intomainfrom
dev-0.17
Draft

dev-0.17#369
termoshtt wants to merge 4 commits intomainfrom
dev-0.17

Conversation

@termoshtt
Copy link
Copy Markdown
Member

Working branch for 0.17.x development

termoshtt and others added 4 commits November 12, 2025 20:02
## Summary

Adds a new `skip_stub_type` attribute to `#[gen_stub_pyclass]`,
`#[gen_stub_pyclass_enum]`, and `#[gen_stub_pyclass_complex_enum]` that
allows users to skip automatic PyStubType trait implementation
generation. This enables users to provide their own custom PyStubType
implementation when needed.

## Motivation

In some cases, users may want to customize the PyStubType implementation
for their PyO3 classes and enums. Currently, the `gen_stub_*` macros
always generate both:
1. PyStubType trait implementation
2. inventory::submit! block

This PR adds the ability to skip the automatic PyStubType generation
while still generating the inventory submission.

## Changes

### Core Implementation
- Added `PyClassAttr`, `PyEnumAttr`, and `PyComplexEnumAttr` structs to
parse attributes
- Modified all `gen_stub_*` macros to accept and parse attributes
- Added conditional code generation based on `skip_stub_type` flag
- **Optimization**: StubType generation is now skipped entirely when
`skip_stub_type` is enabled (not just the output)

### Supported Macros
- ✅ `#[gen_stub_pyclass(skip_stub_type)]`
- ✅ `#[gen_stub_pyclass_enum(skip_stub_type)]`
- ✅ `#[gen_stub_pyclass_complex_enum(skip_stub_type)]`

### Test Coverage
- Added comprehensive test code in
`examples/pure/src/skip_stub_type_test.rs`
- Test cases for class, simple enum, and complex enum
- All test cases include manual PyStubType implementations

## Usage Examples

### For Classes
```rust
#[gen_stub_pyclass(skip_stub_type)]
#[pyclass]
pub struct CustomStubType {
    #[pyo3(get, set)]
    pub value: i32,
}

// Manually implement PyStubType with custom behavior
impl pyo3_stub_gen::PyStubType for CustomStubType {
    fn type_output() -> pyo3_stub_gen::TypeInfo {
        pyo3_stub_gen::TypeInfo::with_module("CustomStubType", "pure".into())
    }
}
```

### For Simple Enums
```rust
#[gen_stub_pyclass_enum(skip_stub_type)]
#[pyclass]
pub enum CustomEnum {
    #[pyo3(name = "OPTION_A")]
    OptionA,
    #[pyo3(name = "OPTION_B")]
    OptionB,
}

impl pyo3_stub_gen::PyStubType for CustomEnum {
    fn type_output() -> pyo3_stub_gen::TypeInfo {
        pyo3_stub_gen::TypeInfo::with_module("CustomEnum", "pure".into())
    }
}
```

### For Complex Enums
```rust
#[gen_stub_pyclass_complex_enum(skip_stub_type)]
#[pyclass]
pub enum CustomComplexEnum {
    #[pyo3(name = "VARIANT_A")]
    VariantA { value: i32 },
    #[pyo3(name = "VARIANT_B")]
    VariantB(String),
}

impl pyo3_stub_gen::PyStubType for CustomComplexEnum {
    fn type_output() -> pyo3_stub_gen::TypeInfo {
        pyo3_stub_gen::TypeInfo::with_module("CustomComplexEnum", "pure".into())
    }
}
```

## Test Plan

- [x] Build succeeds for all workspace crates
- [x] Stub generation works correctly for all types
- [x] Both `skip_stub_type` enabled and disabled cases work as expected
- [x] Verified optimization: StubType only generated when needed

## Performance Impact

✨ **Optimization**: When `skip_stub_type` is enabled, the
`StubType::from()` call is now completely skipped during macro
expansion, reducing compilation overhead.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude <noreply@anthropic.com>
We don't really need it and it's licensed under LGPL-3.0-only that may
introduce some license risks.

Signed-off-by: tison <wander4096@gmail.com>
Automatic check of #362

---------

Co-authored-by: Claude <noreply@anthropic.com>
@termoshtt termoshtt self-assigned this Nov 25, 2025
@termoshtt termoshtt changed the title Dev 0.17 dev-0.17 Nov 25, 2025
@termoshtt termoshtt removed their assignment Nov 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants