There is no way to return False
from issubclass when class is derived from class with __subclashook__
implementation. I modified code from:
python subclasscheck & subclasshook
I only added '(Sized)' to both class definitions:
from abc import ABCMeta
class Sized(metaclass=ABCMeta):
@classmethod
def __subclasshook__(cls, C):
if cls is Sized:
if any("__len__" in B.__dict__ for B in C.__mro__):
return True
return NotImplemented
class A(Sized):
pass
class B(Sized):
def __len__(self):
return 0
print(issubclass(A, Sized)) # True - should be False
print(issubclass(B, Sized)) # True
Is there any way to return False
in this case? Or maybe I'm doing something wrong?