Skip to content

Commit 4efba5b

Browse files
committed
Merge pull request #866 from ankostis/master
#865: Use MagickMock class in the FAQ's exemplary code for mocking modul...
2 parents 5a02e0d + 47f990c commit 4efba5b

File tree

1 file changed

+11
-26
lines changed

1 file changed

+11
-26
lines changed

docs/faq.rst

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ If you are still seeing errors because of C library dependencies, please see the
1111
How do I change behavior for Read the Docs?
1212
-------------------------------------------
1313

14-
When RTD builds your project, it sets the `READTHEDOCS` environment variable to the string `True`. So within your Sphinx's conf.py file, you can vary the behavior based on this. For example::
14+
When RTD builds your project, it sets the `READTHEDOCS` environment variable to the string `True`. So within your Sphinx's ``conf.py`` file, you can vary the behavior based on this. For example::
1515

1616
import os
1717
on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
@@ -34,37 +34,22 @@ I get import errors on libraries that depend on C modules
3434

3535
This happens because our build system doesn't have the dependencies for building your project. This happens with things like libevent and mysql, and other python things that depend on C libraries. We can't support installing random C binaries on our system, so there is another way to fix these imports.
3636

37-
You can mock out the imports for these modules in your conf.py with the following snippet::
37+
You can mock out the imports for these modules in your ``conf.py`` with the following snippet::
3838

3939
import sys
40+
from unittest.mock import MagickMock as Mock
4041

41-
class Mock(object):
42-
43-
__all__ = []
44-
45-
def __init__(self, *args, **kwargs):
46-
pass
47-
48-
def __call__(self, *args, **kwargs):
49-
return Mock()
50-
51-
@classmethod
52-
def __getattr__(cls, name):
53-
if name in ('__file__', '__path__'):
54-
return '/dev/null'
55-
elif name[0] == name[0].upper():
56-
mockType = type(name, (), {})
57-
mockType.__module__ = __name__
58-
return mockType
59-
else:
60-
return Mock()
61-
62-
MOCK_MODULES = ['pygtk', 'gtk', 'gobject', 'argparse']
63-
for mod_name in MOCK_MODULES:
64-
sys.modules[mod_name] = Mock()
42+
MOCK_MODULES = ['pygtk', 'gtk', 'gobject', 'argparse', 'numpy', 'pandas']
43+
sys.modules.update((mod_name, Mock()) for mod_name in MOCK_MODULES)
6544

6645
Of course, replacing `MOCK_MODULES` with the modules that you want to mock out.
6746

47+
.. Tip:: The library ``unittest.mock`` was introduced on python 3.3. On earlier versions install the ``mock`` library
48+
from PyPI with (ie ``pip install mock``) and replace the above import with::
49+
50+
from mock import Mock
51+
52+
6853
Can I make search engines only see one version of my docs?
6954
----------------------------------------------------------
7055

0 commit comments

Comments
 (0)