Support mixing with ABCMeta - #928
Merged
Merged
Conversation
ofek
force-pushed
the
metaclass-cooperative
branch
2 times, most recently
from
November 14, 2025 07:46
4289aee to
50d97ae
Compare
ofek
force-pushed
the
metaclass-cooperative
branch
from
November 14, 2025 08:01
50d97ae to
d091f65
Compare
ofek
marked this pull request as ready for review
November 14, 2025 08:05
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #894
After adding support for subclassing
StructMetain #890, users could define metaclasses like:and then use them for abstract
Structbases:At runtime this exposed two related problems:
Original failure (user issue):
issubclass(ConcreteIntStruct, IntegerStructBase)andisinstance(obj, IntegerStructBase)crashed with:This comes from the
__instancecheck__/__subclasscheck__methods ofABCMeta, which assume the ABC machinery has been initialized (_abc_init/update_abstractmethodshave run and_abc_implis present). BecauseStructMeta_new_innerbuilt the type viaPyType_Type.tp_newwithout calling any ABC helper, the ABC state was never initialized, so the cache object_abc_implwas missing.Follow-up failure when trying to just call
__new__:An attempt to delegate to the "next" metaclass in the MRO (a C equivalent of
super(StructMeta, mcls).__new__(...)) resulted in:This comes from CPython's new safety check, which rejects calling a base type's
__new__when the static base has a differenttp_new. This has been deprecated since 3.12 but is now officially unsupported starting in 3.14.So mixing
StructMetaandABCMetagave you either a crash in ABC’s internals or aTypeErrorfrom the metaclass construction path.Taking inspiration from that new safety check, I think it's infeasible to support mixing with arbitrary metaclasses and now we have special logic for ABCMeta.