简体   繁体   English

sphinx autodoc跳过从模拟继承的类

[英]sphinx autodoc skips classes inherited from mock

I'm using sphinx to document my python project and I have several Qt/PySide widget subclasses. 我正在使用sphinx记录我的python项目,并且我有几个Qt / PySide小部件子类。 So when i run sphinx, i need to mock PySide as the machine running the doc script doesn't have qt or pyside installed. 因此,当我运行sphinx时,我需要模拟PySide,因为运行doc脚本的计算机未安装qt或pyside。 autodoc fails to do anything with these subclasses. autodoc无法对这些子类执行任何操作。

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. 我尝试添加一个autodoc-skip-member函数,该函数检查对象是否为模拟实例并返回false,但仍未得到记录。 Not mocking and installing pyside resolves the issue. 不模拟并安装pyside可解决此问题。

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? 我可以使用autodoc上的设置来使它运行吗?

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 . 这里的例子和sphinx autodoc模块使用了类似的方法 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() . 注意它只是类Mock而不是实例Mock() I'm not sure what implications this has down the line, but for its working as expected 我不确定这有什么影响,但可以按预期工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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