I'm trying to create a metaclass but when I assign it to another class I receive the error TypeError: __init_subclass__() takes no keyword arguments
but I don't implement any __init_subclass__
. Why is this function being called?
class Meta(type):
def __new__(cls, name, bases, dct):
return super().__new__(cls, name, bases, dct)
class MyClass(meta=Meta):
pass
Change meta
to metaclass
. Any keyword arguments passed to the signature of your class are passed to its parent's __init_subclass__
method. Since you entered meta
instead of metaclass
this meta
kwarg is passed to its parent's (object
) __init_subclass__
method:
>>> object.__init_subclass__(meta=5)
TypeError: __init_subclass__() takes no keyword arguments
A similar error would be raised if you actually implemented a __init_subclass__
but made a typo:
class Parent:
def __init_subclass__(cls, handler=None):
super().__init_subclass__()
cls.handler = handler
class CorrectChild(Parent, handler=5):
pass
class TypoChild(Parent, handle=5):
# TypeError: __init_subclass__() got an unexpected keyword argument 'handle'
pass
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With