Design by contract in python- part three
I covered py.contracts and zope.interface, now it’s time to write about abc module from python standard library.
I wanted to write about another library dpcontrancts, but unfortunately, I wasn’t able to download it from PyPi.
When I was reading and reviewing material for previous posts I found out
that there is a way to use python standard library
contracts- I decided to give a try.
After this introduction let’s jump straight to the code:
import abc class ExternalAPIPortMetaclass(object, metaclass=abc.ABCMeta): @abc.abstractmethod def search(self, query, *args, **kwargs): if not isinstance(query, str): raise ValueError('Query should be string') class ExternalAPIPort(ExternalAPIPortMetaclass): def __init__(self, adapter): self.adapter = adapter def search(self, query, *args, **kwargs): super(ExternalAPIPort, self).search(query, *args, **kwargs) return self.adapter.search(query, *args, **kwargs)
What is happening here? I defined
ExternalAPIPortMetaclass as a
metaclass that inherits from
abc.ABCMeta (This code snippet is valid
for python 3). Then I decided to make abstractmethod called
all instances of that metaclass will have to provide such function.
Inside this code, I check whether provided query is a string or not. In
ExternalAPIPort which inherits from previously defined I have to call
ExternalAPIPortMetaclass search method. Thanks to that I can
make a validation of query. Right after that I simply return search
What I don’t like there is fact that I need to add additional line of
super just for checking
contract which can trick others. That’s why I think that metaclasses and
contracts are two different topic besides that they have some pieces in
common (both are designed for telling: here I make contract that you
To sum up this whole series I believe contracts are useful for telling others that I made agreement that this function has to take and return certain value. In python word where is so called duck typing and I don’t think they are necessary in every case but designing by contracts can be helpful as I shown in examples.
Thank you for reading!