Catch all methods in Python classes

Yesterday blog post was about unit testing and in my recent ventures in looking at how to mock complex classes with some form of logic to mimic real third party library logic, I came across this feature, which I think is pretty cool.

My initial search led to this demonstration code, which also shows/demonstrates how to achieve pretty much the same thing in many other programming languages.

So how would I use it?

Consider the following class, borrowed (but adapted) from the project to demonstrate an approach to IaC unit testing discussed yesterday:

class MockS3Client:
    def __init__(self, list_bucket_response: dict=dict()):
        self.list_buckets_response = list_bucket_response
    def list_buckets(self):
        return self.list_buckets_response
    def _bucket_name_exists(self, name: str)->bool:
        for bucket in self.list_buckets_response['Buckets']:
            if name == bucket['Name']:
                return True
        return False
    def create_bucket(self, *args, **kwargs):
        if 'Bucket' in kwargs:
            if self._bucket_name_exists(name=kwargs['Bucket']) is False:
                return {
                    'Location': 'test_location_response'
        raise Exception('Call Failed')
    def __getattr__(self, name):
        def method(*args, **kwargs):
            raise Exception('Method "{}" not yet implemented in unit tests'.format(name))
        return method

Below is a sample session in how this class behaves when a call is made to a still unimplemented method:

>>> client = MockS3Client()
>>> client.list_buckets()
>>> client.list_objects(Bucket='test-123')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 20, in method
Exception: Method "list_objects" not yet implemented in unit tests

This definitely helps debugging build errors in larger projects.