I'm using Sphinx to document a project that depends on wxPython, using the autodocs extension so that it will automatically generate pages from our docstrings. The autodocs extension automatically operates on every module you import
, which is fine for our packages but is a problem when we import
a large external library like wxPython. Thus, instead of letting it generate everything from wxPython I'm using the unittest.mock
library module (previously the external package Mock). The most basic setup works fine for most parts of wxPython, but I've run into a situation I can't see an easy way around (likely because of my relative unfamiliarity with mock until this week).
Currently, the end of my conf.py
file has the following:
MOCK_MODULES = ['wx.lib.newevent'] # I've skipped irrelevant entries...
for module_name in MOCK_MODULES:
sys.modules[module_name] = mock.Mock()
For all the wxPython modules but wx.lib.newevent
, this works perfectly. However, here I'm using the newevent.NewCommandEvent()
function[1] to create an event for a particular scenario. In this case, I get a warning on the NewCommandEvent()
call with the note TypeError: 'Mock' object is not iterable
.
While I can see how one would use patching to handle this for building out unit tests (which I will be doing in the next month!), I'm having a hard time seeing how to integrate that at a simple level in my Sphinx configuration.
Edit: I've just tried using MagicMock()
as well; this still produces an error at the same point, though it now produces ValueError: need more than 0 values to unpack
. That seems like a step in the right direction, but I'm still not sure how to handle this short of explicitly setting it up for this one module. Maybe that's the best solution, though?
From the error, it looks like it is actually executing newevent.NewCommandEvent()
, so I assume that somewhere in your code you have a top-level line something like this:
import wx.lib.newevent
...
event, binder = wx.lib.newevent.NewCommandEvent()
When autodoc imports the module, it tries to run this line of code, but since NewCommandEvent
is actually a Mock
object, Python can't bind its output to the (event, binder)
tuple. There are two possible solutions. The first is to change your code to that this is not executed on import, maybe by wrapping it inside if __name__ == '__main__'
. I would recommend this solution because creating objects like this on import can often have preblematic side effects.
The second solution is to tell the Mock
object to return appropriate values thus:
wx.lib.newevent.NewCommandEvent = mock.Mock(return_value=(Mock(), Mock()))
However, if you are doing anything in your code with the returned values you might run into the same problem further down the line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With