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?