Skip to content


Subversion checkout URL

You can clone with
Download ZIP
A trivial implementation of an interface in Python, which is friendly to (the valuable parts of) duck-typing.
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.



This library is a trivial implementation of an interface in Python, with the following aspects / features:

  • It fails at import time, not at construction, so you know immediately when you have a problem.
  • It's quite simple (very few LOC) and lenient where it counts
  • It exclusively uses decorators, so...
  • It does not require inheritance (reducing 'forced' subclassing)
  • It does not enforce any typing checks
  • It is intended to 'enhance' duck typing by avoiding common pitfalls (forgot to implement something on your fake duck class, overwrote something fundamental, etc.)


Given a simple interface like:

class DuckInterface(object):

    def quack(self):
        """All Ducks must implement a 'quack' method."""

...the following will raise a MissingRequiredAttribute exception at import time:

class Silent(object):
    # no quack method

This, however works:

class Tree(object):
    def quack(self):
        return "the tree appears to quack."

tree = Tree()

Additionally, if you are interested in using the final method decorator outside of an interface, you can do so using the strict class decorator around any class you want to check:

class BaseClass(object):
    def method(self):
        return "Old functionality!"

# the following will raise an exception at import:

class SubClass(BaseClass):

    def method(self):
        return "New functionality!"

It will also check to make sure your arguments match in your implemented methods. For example:

class Base(object):
    def foo(self, a, b, *args):

class Sub(object):
    def foo(self, a):

...will raise an InvalidMethodSignature error. It checks arguments (and argument names) as well as the presence of args and *kwargs.


Obviously, this does nothing for enforcing interfaces on the calling side. I've contemplated something like:

def get_json(instance):
    # raises an exception if instance does not implement
    # JSONViewInterface
    interfaces.expects(instance, JSONViewInterface)
    return instance.to_json()

...but that's just plain type checking, and the only thing you gain over isinstance is a more explicit exception at runtime. Feedback welcome.

Something went wrong with that request. Please try again.