Coming from gh-101688: Implement types.get_orig_bases by Gobot1234 · Pull Request #101827 · python/cpython · GitHub, I’m interested to hear some opinions on adding a new potentially read-only slot for the orig_class attribute which is currently set on an instantiated types.GenericAlias instance to allow better introspection of statements like list[int](). Currently it only sets the attribute if it’s available but I think it would be very nice to have on every generic type.
Could you add a bit more detail on what this would look like, maybe even an example implementation?
I am pretty sure we wouldn’t want to add anything to PyObject literally: that would mean increased memory usage from every single Python object, which would be very costly. On the other hand, adding something just to the C struct for GenericAlias objects should be fine. And adding something to PyTypeObject might be OK, but we’ll have to consider it carefully.
I really do mean adding a field and a PyMemberDef to object, I’m not very familiar with C so any implementation I do come up with probably won’t work. Your concern is by far the largest one I would have adding this. If you have any idea though as to how adding a field to GenericAlias or PyTypeObject would mean that we could introspect the original list I’d be very happy to hear them.
My only other idea would be just adding it to things that support subscription to produce a GenericAlias automatically somehow but how that would work is completely beyond my knowledge and would be full of edge cases but that would certainly help to reduce the memory overhead.