Mock out dependency of a dependency

246 views Asked by At

Imagine the following scenario: You have a python package named 'foo' that depends on 'bar'. The package 'bar' itself depends on another python package, let's say shapely, that depends on a C library that cannot be installed on readthedocs.io. Therefore, 'bar' mocks out shapely by

class Mock(MagicMock):
    @classmethod
    def __getattr__(cls, name):
            return MagicMock()

MOCK_MODULES = ['shapely', 'shapely.wkt', 'shapely.wkb', 'shapely.geometry', 'shapely.ops']
sys.modules.update((mod_name, Mock()) for mod_name in MOCK_MODULES)

in the conf.py file.

Now, I would like to just mock out the dependency shapely from 'bar', but still install 'bar' on RTD in order to let build the docs of 'foo' properly. How can I tell readthedocs.io to mock up 'bar's dependency shapely?

1

There are 1 answers

5
phd On BEST ANSWER

Check for environment variable READTHEDOCS:

import os
if 'READTHEDOCS' in os.environ:
    class Mock(MagicMock):
        @classmethod
        def __getattr__(cls, name):
                return MagicMock()

    MOCK_MODULES = ['shapely', 'shapely.wkt', 'shapely.wkb', 'shapely.geometry', 'shapely.ops']
    sys.modules.update((mod_name, Mock()) for mod_name in MOCK_MODULES)