简体   繁体   中英

sphinx autodoc skips classes inherited from mock

I'm using sphinx to document my python project and I have several Qt/PySide widget subclasses. So when i run sphinx, i need to mock PySide as the machine running the doc script doesn't have qt or pyside installed. autodoc fails to do anything with these subclasses.

I've tried adding an autodoc-skip-member function that checks to see if the object is an instance of mock and returns false, but still doesn't get documented. Not mocking and installing pyside resolves the issue.

def skip(app, what, name, obj, skip, options):
    if isinstance(obj, unittest.mock.Mock):
        print('not skipping {0}'.format(name))
        return False
    return skip

def setup(app):
    app.connect("autodoc-skip-member", skip)

Is there a setting on autodoc i can use to get this to fly?

So the main solution to getting sphinx to fly without needing the extra extensions is to mock them like this:

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


MOCKS = ['bar']
sys.modules.update((mod_name, Mock()) for mod_name in MOCKS)

an example here and the sphinx autodoc module uses a similar approach . However, if you attempt to subclass these mocked imports, you inherit the instatiated class, not the class itself because the module is being set to an instance. Changing to this worked for me:

sys.modules.update((mod_name, Mock) for mod_name in MOCKS)

Notice its just the class Mock and not an instance Mock() . I'm not sure what implications this has down the line, but for its working as expected

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM