How do you verify duck-typed interfaces in python?
<pre><code>class ITestType(object):
  """ Sample interface type """

  __metaclass__ = ABCMeta

  @abstractmethod
  def requiredCall(self):
    return

class TestType1(object):
  """ Valid type? """
  def requiredCall(self):
    pass

class TestType2(ITestType):
  """ Valid type """
  def requiredCall(self):
    pass

class TestType3(ITestType):
  """ Invalid type """
  pass
</code></pre>

<p>In the example above issubclass(TypeType*, ITestType) will return true for 2 and false for 1 and 3.</p>

<p>Is there an alternative way to use issubclass, or an alternative method for interface testing that will allow 1 <em>and</em> 2 to pass, but reject 3?</p>

<p>It'd be extremely helpful for me to be able to use duck typing rather than explicitly binding classes to abstract types, but also allow object checking when duck-typed objects pass through particular interfaces.</p>

<p>Yes, I'm aware that python people don't like interfaces, and the standard methodology is "find it when it fails and wrap everything in exceptions" but also completely irrelevant to my question. No, I cannot simply not use interfaces in this project. </p>

<p>edit:</p>

<p>Perfect! For anyone else who finds this question, here's the an example of how to use subclasshook:</p>

<pre><code>class ITestType(object):
  """ Sample interface type """

  __metaclass__ = ABCMeta

  @abstractmethod
  def requiredCall(self):
    return

  @classmethod
  def __subclasshook__(cls, C):
    required = ["requiredCall"]
    rtn = True
    for r in required:
      if not any(r in B.__dict__ for B in C.__mro__):
        rtn = NotImplemented
    return rtn
</code></pre>
