Conversation
## 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Working branch for 0.17.x development