autodoc a class that extends a mocked class

你说的曾经没有我的故事 提交于 2019-12-04 07:12:31
whoWho

Using the apporach posted here: Sphinx-doc :automodule: with Mock imports

I just changed this line:

sys.modules[mod_name] = mock.Mock()

to:

sys.modules[mod_name] = mock.Mock(class_that_is_extended=object)

and removed 'de.xyz.class_that_is_extended' from MOCK_MODULES

I met the same problem, and my solution was to return object directly from Mock on attribute access.

from unittest.mock import MagicMock

MOCK_MODULES = [
    # modules to mock
    'kivy.uix.floatlayout',
]

MOCK_CLASSES = [
    # classes you are inheriting from
    "FloatLayout",
]


class Mock(MagicMock):
    @classmethod
    def __getattr__(cls, name):
        if name in MOCK_CLASSES:
            return object
        return MagicMock()


sys.modules.update((mod_name, Mock()) for mod_name in MOCK_MODULES)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!