Make init_subclass work in Python 3.5

This commit is contained in:
Syrus Akbary 2017-07-12 00:04:37 -07:00
parent e38007868e
commit 563ca23d00

View File

@ -1,19 +1,19 @@
import six is_init_subclass_available = hasattr(object, '__init_subclass__')
if six.PY2: if not is_init_subclass_available:
class InitSubclassMeta(type): class InitSubclassMeta(type):
"""Metaclass that implements PEP 487 protocol""" """Metaclass that implements PEP 487 protocol"""
def __new__(cls, name, bases, ns): def __new__(cls, name, bases, ns, **kwargs):
__init_subclass__ = ns.pop('__init_subclass__', None) __init_subclass__ = ns.pop('__init_subclass__', None)
if __init_subclass__: if __init_subclass__:
__init_subclass__ = classmethod(__init_subclass__) __init_subclass__ = classmethod(__init_subclass__)
ns['__init_subclass__'] = __init_subclass__ ns['__init_subclass__'] = __init_subclass__
return super(InitSubclassMeta, cls).__new__(cls, name, bases, ns) return super(InitSubclassMeta, cls).__new__(cls, name, bases, ns, **kwargs)
def __init__(cls, name, bases, ns): def __init__(cls, name, bases, ns, **kwargs):
super(InitSubclassMeta, cls).__init__(name, bases, ns) super(InitSubclassMeta, cls).__init__(name, bases, ns)
super_class = super(cls, cls) super_class = super(cls, cls)
if hasattr(super_class, '__init_subclass__'): if hasattr(super_class, '__init_subclass__'):
super_class.__init_subclass__.__func__(cls) super_class.__init_subclass__.__func__(cls, **kwargs)
else: else:
InitSubclassMeta = type InitSubclassMeta = type