[Python-ideas] Abstract metaclasses?

Erik Bray erik.m.bray at gmail.com
Thu Sep 11 00:15:14 CEST 2014


On Wed, Sep 10, 2014 at 5:41 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
> On 09/10/2014 01:59 PM, Erik Bray wrote:
>>
>>
>> --> import abc
>> --> class Meta(type, metaclass=abc.ABCMeta):
>> ...     @abc.abstractmethod
>> ...     def foo(cls): pass
>> ...
>> --> class A(metaclass=Meta): pass
>> ...
>> --> A
>> <class '__main__.A'>
>
>
> I think this is a bug.  However, if the class were:
>
>   --> class A(metaclass=Meta):
>   ...     def foo(self):
>   ...          pass
>   ...
>
> Then this should succeed, and I don't think your Abstractable type allows
> it.

I don't necessarily agree that that should succeed.  The use of an
abstract meta-class is basically requiring there to be a concrete
*classmethod* of the name "foo", (an unbound instancemethod wouldn't
suffice).  What maybe *should* work, but doesn't with this
implementation is:

class A(metaclass=Meta):
    @classmethod
    def foo(cls):
        pass

That could be fixed reasonably easily by extending the
AbstractableType.__new__ to check for classmethods in the new class's
members, a la ABCMeta.__new__.  I'm not sure how that would be best
handled in CPython though.

Alternatively it could just be required that an abstract metaclass
simply can't be used as a metaclass unless a concrete subclass is
made.  But using @classmethod to override abstract class methods does
make some intuitive sense.

Erik


More information about the Python-ideas mailing list