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
gh-60074: add new stable API function PyType_FromMetaclass #93012
Conversation
If these changes are acceptable, it would be great if this feature could still be included in Python 3.11. |
That is not possible; we passed the feature freeze with several weeks. You will need to target 3.12. |
Isn't that naming a bit off? |
@erlend-aasland This means that usable stable API interface for libraries like |
@timfel 1�7 (Edit: ultimately converged towards the name |
Ah, right, that was the naming proposal in the old patch. Yes, this does seem pretty long |
You can of course try your case with the 3.11 release manager (Pablo IIRC) :) Quoting PEP-602:
We are now in the 3.11 beta phase. Note that "no new features" are emphasised in bold type. |
I would hope @encukou can make a quick call (or give an opinion). The new API probably comes with no compatibility concerns the only thing would be getting the API itself wrong (and even that can be deprecated). For NumPy, it would also be nice to have something like this, but it did not seem particularly pressing (static types work and our DTypes are de-facto immortal anyway). I would still be
But, things don't need to be perfect and besides those two calls to make, I don't think there is any reason to not just do this. We clearly need this problem to be fixed. |
I have written a separate email to appeal to @pablogsal. |
Could you please add a bunch of tests for this API in |
Yes, I will do this ASAP. Given the valid concern by @timfel that this function sounds like it creates a metatype, I will also change the name of the function to the somewhat long but more descriptive |
(I will answer your email as soon as I can) |
@pablogsal - Finished renaming, and test added. |
(Could somebody push the button to approve the workflows?) |
Please, add a test that checks that we raise if you provide a |
@pablogsal done. |
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
@pablogsal I have made the requested changes; please review again. |
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
I have made the requested changes; please review again. |
Thanks for making the requested changes! @erlend-aasland: please review the changes made to this pull request. |
For posterity, here is cursed code that emulates It calls |
It might be worth it to put a copy of |
Coincidentally, JPype's @Thrameos just chimed in, and they're struggling with the same issue (among others). The JPype workaround looks less cursed, but more repetitive. @Thrameos, Heads up for @vstinner: We'll probably want to put |
@encukou If any of my implementations look useable I would happily gift them to the Python community. Unfortunately my employer (Department of Energy) has declined to sign the Python contribution agreement and barred me from signing it personally. I am permitted to contribute to open source projects (Ie I can submit code to public domain, GPL, or Apache such as I have done with JPype or any project that does not require a signed agreement) , and someone else can incorporate it, but our legal team indicates that number of hours required to review a signed license requires a funded billable account which I lack. Hence, leaving me unable to contribute directly. |
IMO it's best to start with a copy of CPython's own code, anyway. |
TL;DR: Numerous flagship Python packages internally rely on
pybind11
to bridge C++ and Python. This includes SciPy, PyTorch, Tensorflow, JAX, and many others. This PR adds critical functionality needed to eventually move more of them onto thePy_LIMITED_API
and thereby simplify deployment of binary wheels.Context: C++ <-> Python binding tools like pybind11 and nanobind require the ability to instantiate types using a custom metaclass. pybind11 uses them to install custom handling for some type-related operations, and nanobind goes even further by storing binding-related data structures in an enlarged
PyHeapTypeObject
allocated by a metaclass.Unfortunately is not currently possible to dynamically create suitable heap types using the
Py_LIMITED_API
, which means that each extension library using these tools must be compiled many times for redistribution. I believe that it would be useful to the Python community to at least optionally supportPy_LIMITED_API
in these tools, but this requires a small change in CPython.This pull request is another attempt at addressing the issue pointed out in issue #60074. It adds a new stable API function
PyType_FromMetaclass
that mirrors the behavior ofPyType_FromModuleAndSpec
except that it takes an additional metaclass argument.I used this PR to create a proof-of-concept version of nanobind (🎉 .
limited_api
branch) that dynamically create types using the limited API. It worksI have a somewhat audacious request, which is that this change is considered for Python 3.11 despite having recently entered feature freeze. I believe that this change is small enough and of significant utility for the wider Python community (potentially even for other groups working on bindings like SWIG or Cython)