Message 287966 - Python tracker (original) (raw)

I believe I've found a bug (or, at least, critical shortcoming) in the way that python 3.6's init_subclass interacts with abc.ABCMeta (and, presumably, most other metaclasses in the standard library). In short, if a class subclasses both an abstract class and a class-that-uses-init_subclass, and the init_subclass uses keyword arguments, then this will often lead to TypeErrors (because the metaclass gets confused by the keyword arguments to new that were meant for init_subclass).

Here's an example of the failure. This 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()

raises this TypeError when run:

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

See http://stackoverflow.com/questions/42281697/typeerror-when-combining-abcmeta-with-init-subclass-in-python-3-6 for further discussion.