Everything works as is should. If you are using __metaclass__
, you are overwriting the class creation process. It looks like your monkeypatch is working for __subclasshook__
but it's only called from the __subclasshook__
function of the ABCMeta
. You can check it with this:
>>> type(Foo)
<class 'abc.ABCMeta'>
To be more explicit: the __subclasshook__
case works by accident in this example, because the metaclass's __subclasscheck__
happens to defer to the class's __subclasshook__
in some situations. The __instancecheck__
protocol of the metaclass does not ever defer to the class's definition of __instancecheck__
, which is why the monkeypatched version of __subclasshook__
does eventually get called, but the monkeypatched version of __instancecheck__
does not get called.
In more details: If you are creating a class with the metaclass, the type of the class will be the metaclass. In this case the ABCMeta
. And the definition of the isinstance()
say the following: 'isinstance(object, class-or-type-or-tuple) -> bool', which means the instance checking will be executed on the given class, type or tuple of classes/types. In this case the isinstance check will be done on the ABCMeta
(ABCMeta.__instancecheck__()
will be called). Because the monkeypatch was applied to the Foo
class and not the ABCMeta
, the __instancecheck__
method of Foo
will never run. But the __instancecheck__
of the ABCMethod
will call the __subclasscheck__
method of itself, and this second method will try the validaion by executing the __subclasshook__
method of created class (in this example Foo
).
In this case you can get the desired behavior if you overwrite the functions of the metaclass like this:
def interface(*attributes):
def decorator(Base):
def checker(Other):
return all(hasattr(Other, a) for a in attributes)
def __subclasshook__(cls, Other):
if checker(Other):
return True
return NotImplemented
def __instancecheck__(cls, Other):
return checker(Other)
Base.__metaclass__.__subclasshook__ = classmethod(__subclasshook__)
Base.__metaclass__.__instancecheck__ = classmethod(__instancecheck__)
return Base
return decorator
And the output with the updated code:
True
True
True
True
Another approach would be to define your own class to serve as the metaclass, and create the kind of __instancecheck__
protocol that you're looking for, so that it does defer to the class's definition of __instancecheck__
when the metaclass's definition hits some failure criteria. Then, set __metaclass__
to be that class inside of Foo
and your existing decorator should work as-is.
More info: A good post about metaclasses