TypeError when combining ABCMeta with __init_subclass__ in Python 3.6

I am trying to use python 3.6 new __init_subclass__ ( PEP 487 ) function with abc module. It does not seem to work. The following code:

 from abc import ABCMeta class Initifier: def __init_subclass__(cls, x=None, **kwargs): super().__init_subclass__(**kwargs) print('got x', x) class Abstracted(metaclass=ABCMeta): pass class Thingy(Abstracted, Initifier, x=1): pass thingy = Thingy() 

gives at startup the following:

 Traceback (most recent call last): File "<filename>", line 10, in <module> class Thingy(Abstracted, Initifier, x=1): TypeError: __new__() got an unexpected keyword argument 'x' 

Everything works fine if Abstracted doesn't use the ABCMeta metaclass.

This error is fairly robust, for example, the following code still does not work with a similar type error (presumably because the metaclass' __new__ works during class creation, while the parent class __new__ does not work until the object).

 from abc import ABCMeta class Initifier: def __new__(cls, name, bases, dct, x=None, **kwargs): return super().__new__(cls, name, bases, dct, **kwargs) def __init_subclass__(cls, x=None, **kwargs): super().__init_subclass__(**kwargs) print('got x', x) class Abstracted(metaclass=ABCMeta): pass class Thingy(Initifier, Abstracted, x=1): pass thingy = Thingy() 

Can anyone confirm that this is a bug in the Python 3.6 abc module and / or __init_subclass__ ? (I could use __init_subclass__ incorrectly.) Does anyone have a workaround?

+8
python
source share
1 answer

This is a mistake in abc.ABCMeta , due to the wart in the __init_subclass__ design. I recommend reporting this.

Quite often, every existing metaclass should now pass unexpected keyword arguments to super().__new__ , so type.__new__ can pass them to __init_subclass__ , but ABCMeta and probably tons of other metaclasses do not yet. abc.ABCMeta.__new__ throttles on the keyword argument x instead of passing it, throwing an exception that you see.

Attempting to use the __init_subclass__ keyword arguments with a metaclass that has not been updated for the new design will not work. You will have to wait until the metaclasses that you use are fixed.

+6
source share

All Articles