-
Notifications
You must be signed in to change notification settings - Fork 34
Derive macro for rich enums #208
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
base: main
Are you sure you want to change the base?
Conversation
Also Fixes tuple implementation to allow 1-tuples.
This allows manual stub information to be entered in a manner that follows simple enums. Add an example rich enum to the pure example.
This looks great @evonreis. I just tried it out and it nearly works as I'd expected with a couple of small issues if you're interested in feedback.
|
I quickly hacked together a couple of commits which work. I'm not very familiar with the codebase so there might be better ways to achieve the same outcome. https://github.com/jagregory/pyo3-stub-gen/tree/rich_enum
|
@jagregory your proposed transform looks great!
Some more things that should be supported by the generated typing:
|
Thank you @jagregory and @flying-sheep for looking this over. I will address all of the issues you've brought up. |
This allows variants to be correctly returned from functions that have a return type of the enum. Avoids explicit setting of a base class in the macro by creating an unqualified TypeInfo struct from the enum python class name This avoids having to double-specify the enum python class name when manually defining the stubs for a rich enum.
@jagregory, I've pushed a commit for the making the variant classes derived from the enum class in the stub file. I build a |
Shows derivation of variant classes from enum class.
PyO3 creates a __new__() method for constructing enum variants, For tuple variants it creates __len__() and __getitem__() methods as well. These now have stubs in the generated file. __new__() was added in a previous commit. This commit modifies the stub to match [pyo3(constructor = ... )] attributes on variants that can change the signature of __new__(). Generation of the MethodInfo structs for these variant methods are moved out of generate/class.rs and into a new file, variant_methods.rs
@jagregory I cherry-picked your commit for a variant
@flying-sheep I don't understand your testing suggestion, but certainly more tests are needed. I think your other points are now addressed. |
This looks awesome, thank you! Regarding testing, I meant that the example code I linked has But I think this project doesn’t have this kind of tests yet, so what you did is perfect! |
@flying-sheep I understand your idea better now. I tried to write something, but assert_type() was introduced in py3.11 seemingly, and pyo3-stub-gen passes tests in py3.9. Getting assert_type() to work while still supporting 3.9 is beyond my python knowledge. |
No worries, the exact tests you added before are perfect! |
Not available in 3.9, the minimum python version and the default used for testing.
What's remaining on this to be able to merge? |
Add new attribute macro
gen_stub_pyclass_rich_enum
that creates internal classes for the variants of rich enums, that is, enums with variants that have fields.Also creates in type_info.rs PyRichEnumInfo and VariantInfo for manual creation of stub information for rich enums.
Both of these are handled by 'ClassDef', which can now have internal classes and will write properly indented internal classes to the stub file.
This PR also fixes the implementation on tuples so that tuples of length one are supported.