Force implementation of class methods

If you want to make sure that a Subclass implements certain methods of it's parent in Python, you can do this:

 

import pytest
from abc import ABCMeta, abstractmethod


class AbstractSubject(object):
    """A common interface for the real and proxy objects."""

    __metaclass__ = ABCMeta

    @abstractmethod
    def sort(self, reverse=False):
        pass


class RealSubject(AbstractSubject):
    pass


with pytest.raises(TypeError):
    real_subject1 = RealSubject()

When RealSubject doesn't have the sort method implemented, it will throw an error on initialization of RealSubject.