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

[Feature request] Directly expose tp_bases, tp_mro, and tp_subclasses on PyType in high-level API #4192

Closed
Hooksie opened this issue May 17, 2024 · 4 comments · Fixed by #4197

Comments

@Hooksie
Copy link
Contributor

Hooksie commented May 17, 2024

Please note, am I generally unfamiliar with the Python C ffi layer, so please excuse any errors or weird assumptions.

I am prototyping a library in pyo3 that introspects type hierarchy when defining new types. In pure Python, i would do this through either __bases__ or __mro__ (and in some rarer cases, __subclasses__()).

Since these all exist within the slots of the PyTypeObject, I wanted to be able to access these directly. Instead, it appears I need to call (for example) cls.getattr("__bases__") and then downcast to a tuple for safe code. Alternatively, I can use as_type_ptr() but end up with unsafe code (and I'm still not sure this is a good idea).

Ideally, it would be nice to have PyType::bases (accessing tp_bases), PyType::mro (accessing tp_mro), and PyType::subclasses (accessing tp_subclasses) available as higher-level APIs.

@Hooksie Hooksie changed the title [Feature request] Directly expose tp_bases, tp_mro, and tp_subclasses on PyType [Feature request] Directly expose tp_bases, tp_mro, and tp_subclasses on PyType in high-level APIs May 17, 2024
@Hooksie Hooksie changed the title [Feature request] Directly expose tp_bases, tp_mro, and tp_subclasses on PyType in high-level APIs [Feature request] Directly expose tp_bases, tp_mro, and tp_subclasses on PyType in high-level API May 17, 2024
@davidhewitt
Copy link
Member

I have someone at the PyCon US sprints experimenting with this today, so hopefully we can figure out an implementation 😄

@Cheukting
Copy link
Contributor

I have someone at the PyCon US sprints experimenting with this today, so hopefully we can figure out an implementation 😄

That's me :-)

@Cheukting
Copy link
Contributor

According to this (https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_subclasses) seems tp_subclasses is for internal use only. Maybe we should work on __bases__ and __mro__ first.

@Hooksie
Copy link
Contributor Author

Hooksie commented May 21, 2024

@Cheukting I was reading the docs, and noticed the same as well. I think exposing tp_subclasses probably doesnt fall into the same category as the other two here. I think it's reasonable to disregard for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants