Overloading __instancecheck__ method of metaclass
#4039
Unanswered
nicola-gigante
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi!
I need to overwrite the
__instancecheck__method for a C++ class exported usingpybind11. AFAIK this needs to be done on the class' metaclass, but I'm not familiar with the concept.Can I do that with
pybind11?My initial attempt:
This does not work when I load the module:
If I had to write this in Python,
PippoMetawould have to derive fromtype. So maybe that's the missing bit, but how to define thepy::class_to derive fromtype?Edit:
So looking at the
pybind11implementation it seems that registered classes must havepybind11special default metaclass. So I tried to make my metaclass inherit from that, but this triggers anassertinpybind11:The output:
Looking at the failed assertion, it seems that
pybind11expects any registered class to have at least a registered base class. That's whyget_type_infoonPippoMeta's base class returnsnullptrand the assert triggers. Is my conjecture right?Note: cross-posted with StackOverflow
Beta Was this translation helpful? Give feedback.
All reactions