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

Add #[pyclass(generic)] #4926

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

IvanIsCoding
Copy link
Contributor

@IvanIsCoding IvanIsCoding commented Feb 20, 2025

Closes #4849

Follow up of #4917

  • Add more tests
  • Add changelog entry
  • Update the documentation
  • Reject generic for enums; let's focus only on supporting structs for now
  • Error in Python 3.7 and 3.8 (it just fails to compile for now)
  • Fix the compile error tests regressions; I haven't investigated why they fail yet

edit: it is mostly ready to review now

@mejrs
Copy link
Member

mejrs commented Feb 20, 2025

Assuming we want to go with #[pyclass(generic)] as the syntax, I think this is roughly the best way to insert that method.

As for the error: what this builder does is that it a) emits an extern "C" fn that wraps a rust function, and b) emits code that gives Python a pointer to the function and its name. It does not emit the rust function; you have to add that somewhere. Probably something like this:

#[doc(hidden)]
#[allow(non_snake_case)]
impl #cls {
#default_richcmp
#default_hash
#default_str
}

This should also prevent the user from both writing #[pyclass(generic)] and creating a method named __class_getitem__ (please add a test for this)

@IvanIsCoding IvanIsCoding marked this pull request as ready for review February 22, 2025 19:28
@IvanIsCoding IvanIsCoding changed the title [WIP] Add #[pyclass(generic)] Add #[pyclass(generic)] Feb 22, 2025
@IvanIsCoding
Copy link
Contributor Author

IvanIsCoding commented Feb 22, 2025

I added tests and updated the documentation. I am confident it all works. For compilation failure, I tested users redefining __class_geitem__ and blocked enums for the time being.

I am still not sure how to block #[pyclass(generic)] for Python 3.7 and Python 3.8. I don't know if we should do it, but currently specifying it will cause compilation failures anyway as #4917 is gated for Python 3.9+

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.

Add Rust support for type hinting objects
2 participants