Using mixin methods in C type in Python 3.12 (metaclass tp_new restriction)

I’m working on updating a binary extension for 3.12 and ran in to the new restrictions of not being allowed to use a metaclass with a custom tp_new in a C type created with PyType_From*().

I have some types that are various collections (e.g. Mapping, Sequence, etc.) and I was using the abc.collections abstract classes as a base class so that I didn’t have to implement all of the mixin methods they provide in C. However, now this code is failing in Python 3.12 due to the new restriction since abc.ABCMeta has a custom tp_new.

Is there a different way I can get the mixin methods without using the abstract base classes?

…suggests adding a new type. For example, if you define Spam that inherits from abc.collections.Mapping in C using PyType_From..., change it to BaseSpam, remove the interitance and then call the metaclass to create a new class Spam that inherits from both.

…suggests to manually register the class and add the methods later


# Mixin the methods manually.
ScalarMapContainer.__contains__ = MutableMapping.__contains__
ScalarMapContainer.keys = MutableMapping.keys
ScalarMapContainer.items = MutableMapping.items
ScalarMapContainer.values = MutableMapping.values