def generate_immutable_abc(supercls, mutable_subclass):
class ABCImmutableMeta(ABCMeta):
def __subclasscheck__(cls, subclass):
if not issubclass(subclass, supercls):
return False
return not issubclass(subclass, mutable_subclass)
class ABCImmutable(with_metaclass(ABCImmutableMeta, object)):
pass
# dirty hack to assert issubclass(ABCImmutable, supercls).
supercls._abc_cache.add(ABCImmutable)
return ABCImmutable
python类with_metaclass()的实例源码
def __new__(generator_cls, ABC):
MetaMagicType = create_metaclass(
BasicMetaMagicType,
generator_cls,
)
MagicType = create_class(
with_metaclass(MetaMagicType, BasicMagicType),
generator_cls,
)
MagicType.generator_cls = generator_cls
MagicType.main_cls = ABC
MagicType.partial_cls = None
return MagicType
def test_inherit_2(self):
class Hi(TableBase):
id = HashKeyField()
class Table(with_metaclass(TableMeta, Hi)):
pass
class User(Table, Hi):
name = RangeKeyField(data_type=NUMBER)
self._check_rangekey(User)
def test_with_metaclass_and_dict(self):
"""
Issue #91 (for newdict instead of newobject)
"""
from future.utils import with_metaclass
class MetaClass(type):
pass
class TestClass(with_metaclass(MetaClass, dict)):
pass
def test_with_metaclass_and_str(self):
"""
Issue #91 (for newstr instead of newobject)
"""
from future.utils import with_metaclass
class MetaClass(type):
pass
class TestClass(with_metaclass(MetaClass, str)):
pass
def test_with_metaclass_and_int(self):
"""
Issue #91 (for newint instead of newobject)
"""
from future.utils import with_metaclass
class MetaClass(type):
pass
class TestClass(with_metaclass(MetaClass, int)):
pass
def test_with_metaclass_and_list(self):
"""
Issue #91 (for newdict instead of newobject)
"""
from future.utils import with_metaclass
class MetaClass(type):
pass
class TestClass(with_metaclass(MetaClass, list)):
pass
def test_with_metaclass_and_object(self):
"""
Issue #91
"""
from future.utils import with_metaclass
class MetaClass(type):
pass
class TestClass(with_metaclass(MetaClass, object)):
pass