Skip to content
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

feat: support pyclass on tuple enums #4072

Merged
merged 24 commits into from
May 17, 2024
Merged

feat: support pyclass on tuple enums #4072

merged 24 commits into from
May 17, 2024

Conversation

newcomertv
Copy link
Contributor

@newcomertv newcomertv commented Apr 12, 2024

#3748
Based on @mkovaxx 's https://github.com/PyO3/pyo3/pull/3582/files

Adds support for #[pyclass] for enums containing Tuple Variants.

Supports:

  • Pattern matching
  • Accessing by field ._id
  • Accessing by array [id]
  • Len (elements in the tuple)

This introduces an issue that I didn't know how to solve quickly: Since the fields have no identifiers I need to create them inside the derive macro. Since they need to outlive their context where they are created as FnArg requires a &'a Ident I ended up creating a Box::leak based identifier.

I'm not sure how bad this is in the first place since it is only during the build process (code gen) and extremely small.

Addressing the getitem issue the comments on #4135 were very helpful.
Thanks to @carderne for fixing the doc issues, trying to solve the getitem problem and seeing the value in this PR :) - I had a few busy weeks and didn't get to continue working on this PR until now.

I hope I didn't miss any further documentation requirements or am missing any testcases.

Blocking issues:

  • @birkenfield suggested refactoring FnArg to take a Cow instead. I don't think this should be part of this PR.
    If we need this before this PR gets merged I can open a seperate PR. (Though I'm not keen on it). A tracking issue has been opened within Replace &'a Ident with Cow in FnArg Enum #4156
  • @Icxolu I'm not sure if __match_args__ needs to be generated as we already support matching through the subclass generation.

guide/src/class.md Outdated Show resolved Hide resolved
guide/src/class.md Outdated Show resolved Hide resolved
guide/src/class.md Outdated Show resolved Hide resolved
guide/src/class.md Outdated Show resolved Hide resolved
guide/src/class.md Outdated Show resolved Hide resolved
guide/src/class.md Outdated Show resolved Hide resolved
guide/src/class.md Outdated Show resolved Hide resolved
guide/src/class.md Outdated Show resolved Hide resolved
guide/src/class.md Show resolved Hide resolved
pyo3-macros-backend/src/pyclass.rs Outdated Show resolved Hide resolved
pytests/src/enums.rs Show resolved Hide resolved
Copy link
Contributor

@Icxolu Icxolu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for following on this. Don't worry about being busy, happens to all of us, sometimes there are more important things.

This is already looking very good to me. I left a few comments to simplify the code a bit. Regarding using Cow in FnArg, that was not my suggestion, but I agree with that. While leaking would probably be fine, I think using Cow would be the cleaner solution. If you would like open a separate PR for that, that would be fine with me.

Also just giving @carderne a ping, such that we don't duplicate work.

pyo3-macros-backend/src/pymethod.rs Outdated Show resolved Hide resolved
guide/src/class.md Outdated Show resolved Hide resolved
guide/src/class.md Outdated Show resolved Hide resolved
pyo3-macros-backend/src/pyclass.rs Outdated Show resolved Hide resolved
pyo3-macros-backend/src/pyclass.rs Outdated Show resolved Hide resolved
pyo3-macros-backend/src/pyclass.rs Outdated Show resolved Hide resolved
pyo3-macros-backend/src/pyclass.rs Outdated Show resolved Hide resolved
pytests/tests/test_enums.py Outdated Show resolved Hide resolved
pytests/tests/test_enums_match.py Show resolved Hide resolved
pyo3-macros-backend/src/pyclass.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@Icxolu Icxolu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much 🙏 . I think we're getting really close now 🚀.

The slot and __match_args__ handling looks great.

I marked a few spots, where we can reduce some code and some (optional) style nits.
There are two pieces that I would like to address before merge.

For the documentation in the guide I think we should encourage using indexing and tuple match for these tuple variants. This should be a pretty minor change just adapting the examples.

The second point is to also support constructor customization here, which we just landed for struct variants in #4158. This should also be a pretty simple change and I added a note in the corresponding place.

(I hope I'm not putting too much additional work on your table 🙃 )

pyo3-macros-backend/src/pyclass.rs Outdated Show resolved Hide resolved
pyo3-macros-backend/src/pyclass.rs Show resolved Hide resolved
pyo3-macros-backend/src/pyclass.rs Outdated Show resolved Hide resolved
pyo3-macros-backend/src/pyclass.rs Outdated Show resolved Hide resolved
pyo3-macros-backend/src/pyclass.rs Outdated Show resolved Hide resolved
pyo3-macros-backend/src/pyclass.rs Outdated Show resolved Hide resolved
pytests/tests/test_enums_match.py Outdated Show resolved Hide resolved
guide/src/class.md Outdated Show resolved Hide resolved
pytests/src/enums.rs Outdated Show resolved Hide resolved
@newcomertv newcomertv force-pushed the main branch 2 times, most recently from a85b94b to 160ad27 Compare May 14, 2024 06:30
@newcomertv
Copy link
Contributor Author

Am I missing anything else or is this ready for final review?

Copy link
Contributor

@Icxolu Icxolu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me now. clippy-beta is failing right now, but once that's fixed I think this is good to go 🚀 . Thanks again for putting in the effort and pushing this over the line!

@davidhewitt
Copy link
Member

As conclusion is still green I think this can merge even if clippy beta is unhappy.

@carderne
Copy link
Contributor

Nice work @newcomertv, I'm learning a bit going through where I went wrong trying to figure this out.
(And looking forward to using this feature!)

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me too, with just a few suggestions...

guide/src/class.md Outdated Show resolved Hide resolved
pyo3-macros-backend/src/pyclass.rs Outdated Show resolved Hide resolved
pyo3-macros-backend/src/pyclass.rs Outdated Show resolved Hide resolved
@newcomertv
Copy link
Contributor Author

Thanks for the review.

I‘m quite busy right now. I‘ll try to take a look and fix those tomorrow or the day after.

@newcomertv
Copy link
Contributor Author

I hope this fixes the remaining issues. :-)

( gh-pages appears to have failed due to lychee being unable to reach tokio.rs )

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me too, thanks again!

@davidhewitt davidhewitt added this pull request to the merge queue May 17, 2024
Merged via the queue into PyO3:main with commit 88f2f6f May 17, 2024
43 checks passed
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.

5 participants