[Python-ideas] A way out of Meta-hell (was: A (meta)class algebra)

Nick Coghlan ncoghlan at gmail.com
Tue Feb 17 13:38:16 CET 2015


On 17 February 2015 at 02:19, Martin Teichmann <lkb.teichmann at gmail.com> wrote:
> Hi list,
>
>> Again, Python already has a metaclass that everyone is supposed to
>> use, namely `type`.
>
> well, there is a problem out here: people are indeed using other
> metaclasses. Even the standard library, like ABC or enums.
>
> And there is a problem here in python: once you do multiple inheritance,
> and two classes have different metaclasses, there is no way of
> programmatically determining an appropriate metaclass.

Outside the simple cases (which PEP 422 could theoretically handle),
metaclasses tend to be contravariant.

EnumMeta instances deliberately don't support some things a normal
type instance (i.e. class object) will support (like inheriting from
them). The same is true for ABCMeta instances (e.g. you often can't
instantiate them). And yet:

>>> from abc import ABCMeta
>>> issubclass(ABCMeta, type)
True
>>> from enum import EnumMeta
>>> issubclass(EnumMeta, type)
True

You're asking us to assume covariant metaclasses in a subset of cases,
while retaining the assumption of contravariance otherwise. That isn't
going to happen - we expect type designers to declare metaclass
covariance by creating an explicit subclass and using it where
appropriate..

PEP 422 bypasses the default assumption of metaclass contravariance by
operating directly on type instances, without touching any other part
of the metaclass machinery.

Regards,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list