-
Notifications
You must be signed in to change notification settings - Fork 44
Generate comparison method stubs for #[pyclass(eq, ord)]
#268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This fixes issue #233 by automatically generating stub methods for comparison operations (__eq__, __lt__, __le__, __gt__, __ge__) when pyclass attributes eq and ord are used. Changes: - Add Eq and Ord variants to Attr enum - Update attribute parsing to recognize eq/ord attributes - Add comparison method generation in PyClassInfo - Modify pyclass proc macro to submit PyMethodsInfo for comparison methods - Make struct fields public to allow manual construction - Add test case with ComparableStruct to verify functionality 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
#[pyclass(eq, ord)]
This redesigns the comparison method generation to follow proper separation of concerns, fixing the architectural issues from the previous implementation. Changes: - Revert pub fields in derive crate structs to maintain encapsulation - Move comparison method generation from derive side to stub generation side - Add has_eq/has_ord flags to PyClassInfo for metadata only - Implement comparison method generation in ClassDef with proper architecture - Update exception macro to include new PyClassInfo fields - Maintain proper type information (object instead of typing.Any) This ensures derive crate only handles metadata collection while stub generation side handles actual method generation, creating a cleaner and more maintainable architecture. Fixes #233 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
cb5a542
to
37d8943
Compare
🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements automatic generation of Python stub methods for comparison operations when PyO3 classes use the eq
and ord
attributes in their #[pyclass]
decorator, addressing issue #233.
- Adds support for detecting
eq
andord
attributes in#[pyclass]
decorators - Automatically generates stub methods for
__eq__
,__lt__
,__le__
,__gt__
, and__ge__
operations - Maintains architectural separation between derive-time metadata collection and generation-time stub creation
Reviewed Changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 6 comments.
Show a summary per file
File | Description |
---|---|
pyo3-stub-gen/src/type_info.rs | Adds has_eq and has_ord boolean fields to PyClassInfo struct |
pyo3-stub-gen/src/lib.rs | Updates documentation example to include new comparison fields |
pyo3-stub-gen/src/generate/class.rs | Implements comparison method generation logic with add_eq_method() and add_ord_methods() |
pyo3-stub-gen/src/exception.rs | Updates exception macro to include default values for new fields |
pyo3-stub-gen-derive/src/gen_stub/pyclass.rs | Adds parsing and processing for eq and ord attributes in derive phase |
pyo3-stub-gen-derive/src/gen_stub/attr.rs | Adds Eq and Ord variants to Attr enum and parsing logic |
pyo3-stub-gen-derive/src/gen_stub.rs | Updates documentation example with new fields |
examples/pure/tests/test_python.py | Adds comprehensive tests for comparison operations |
examples/pure/src/lib.rs | Adds ComparableStruct test class with #[pyclass(eq, ord)] |
examples/pure/pure.pyi | Shows generated stub output with comparison methods |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Summary
Fixes #233: Automatically generates Python stub methods for comparison operations (
__eq__
,__lt__
,__le__
,__gt__
,__ge__
) when PyO3 classes use theeq
andord
attributes in their#[pyclass]
decorator.Problem
When using
#[pyclass(eq, ord)]
in PyO3, the Rust compiler automatically generates comparison methods for the class. However, pyo3-stub-gen was not generating corresponding Python stub methods, leading to missing type hints for these operations in IDEs and type checkers.Solution
Implemented a three-phase solution that follows the existing architecture pattern:
1. Derive Phase (Metadata Collection)
Eq
andOrd
variants to theAttr
enum inpyo3-stub-gen-derive/src/gen_stub/attr.rs
eq
andord
attributes in#[pyclass]
decoratorsPyClassInfo
to includehas_eq
andhas_ord
boolean flags2. Runtime Phase (Type Information)
PyClassInfo
inpyo3-stub-gen/src/type_info.rs
with comparison flags3. Generation Phase (Stub Creation)
pyo3-stub-gen/src/generate/class.rs
add_eq_method()
to generate__eq__(self, other: object) -> bool
add_ord_methods()
to generate__lt__
,__le__
,__gt__
,__ge__
methodsClassDef::from
to automatically add methods when flags are setTest Coverage
Added comprehensive test coverage in
examples/pure/
:ComparableStruct
with#[pyclass(eq, ord)]
Generated Stubs
For a class like:
The generator now produces:
Architecture
The implementation maintains proper separation of concerns:
eq
/ord
presence