Nick Coghlan added the comment:
Because they're checking for different things:
- types.prepare_class is only checking "How do I call __prepare__?". It only
triggers type resolution at that point if the metaclass hint is an instance of
type, otherwise it skips that process entirely and queries the metaclass hint
- type.__new__ is checking "Can I actually create a new instance of this
metaclass with these bases?". It can only do that if either the metaclass being
instantiated is a subclass of all the bases of the type being defined, or else
such a metaclass exists amongst the bases.
To be clear, your example isn't failing due to the way MyDerived is defined -
it's failing because OtherMetaclass is itself an instance of type, and you're
declaring it (directly or indirectly) as the metaclass of MyDerived, while
inheriting from MyClass, which is an instance of MyMetaclass.
Python tracker <rep...@bugs.python.org>
Python-bugs-list mailing list