Utilities to deal with abstract base classes and virtual subclasses.
New in version 0.2.
On Python 2.6 this class is actually abc.ABCMeta, on versions lower than that it’s simply a dummy. Which makes implementing abstract classes easier if you want to support Python versions which don’t support class.__subclasscheck__() and class.__instancecheck__().
A metaclass which allows you to easily define abstract super classes, simply inherit from this metaclass and set the virtual_superclasses attribute to an iterable:
>>> from brownie.abstract import ABCMeta, VirtualSubclassMeta
>>>
>>> class VirtualBaseClass(object):
... __metaclass__ = ABCMeta
>>>
>>> class VirtualSubclass(object):
... __metaclass__ = VirtualSubclassMeta
...
... virtual_superclasses = (VirtualBaseClass, )
>>>
>>> issubclass(VirtualSubclass, VirtualBaseClass)
True
A metaclass for abstract base classes which are also virtual subclasses.
Simply set virtual_subclasses to an iterable of classes your class is supposed to virtually inherit from:
>>> from brownie.abstract import ABCMeta, AbstractClassMeta, \
... VirtualSubclassMeta
>>> class Foo(object):
... __metaclass__ = ABCMeta
>>>
>>> class Bar(object):
... __metaclass__ = AbstractClassMeta
...
... virtual_superclasses = (Foo, )
>>>
>>> class Baz(object):
... __metaclass__ = VirtualSubclassMeta
...
... virtual_superclasses = (Bar, )
>>>
>>> issubclass(Baz, Foo)
True
>>> issubclass(Baz, Bar)
True
Note
All classes could use AbstractClassMeta as __metaclass__ and the result would be the same, the usage here is just to demonstrate the specific problem which is solved.