Sphinx-doc:automodule:使用Mock导入

时间:2014-09-21 14:36:26

标签: python python-sphinx

我尝试使用sphinx-doc:automodule:与模拟出来的模块一起按this回答。具体来说,我使用Mock进行PyQt5模块导入,而这些导入在ReadTheDocs上是不可用的。

奇怪的是,我发现任何继承自Mock-ed模块类的类都没有包含在结果文档中。似乎sphinx-doc出于某种原因无法看到它们。

我稍微定制的Mock如下:

from mock import Mock as MagicMock


class Mock(MagicMock):
    __all__ = ['QApplication','pyqtSignal','pyqtSlot','QObject','QAbstractItemModel','QModelIndex','QTabWidget',
        'QWebPage','QTableView','QWebView','QAbstractTableModel','Qt','QWidget','QPushButton','QDoubleSpinBox',
        'QListWidget','QDialog','QSize','QTableWidget','QMainWindow','QTreeWidget',
        'QAbstractItemDelegate','QColor','QGraphicsItemGroup','QGraphicsItem','QGraphicsPathItem',
        'QGraphicsTextItem','QGraphicsRectItem','QGraphicsScene','QGraphicsView',]

    def __init__(self, *args, **kwargs):
        super(Mock, self).__init__()


    @classmethod
    def __getattr__(cls, name):
        if name in ('__file__', '__path__'):
            return os.devnull
        else:
            return Mock

    @classmethod
    def __setattr__(*args, **kwargs):
        pass

    def __setitem__(self, *args, **kwargs):
        return

    def __getitem__(self, *args, **kwargs):
        return Mock

要求__all__允许导入PyQt5类的from x import *样式。

我可以确认将超类更改为object会导致正确记录类,删除模拟(本地生成)也是如此。使用:autoclass:强制文档会产生一行,表明该类继承自Mock

1 个答案:

答案 0 :(得分:0)

我最终解决了这个问题,因为Mock对象没有使用Qt。在我的应用程序中,有一个qt.py包装器文件,用于处理PyQt4PyQt5之间的差异,并允许随后导入它们以供使用(同时忽略Qt命名空间重新排列)。

在这个文件中,我将实际的导入代码包装在ReadTheDocs的测试中,如果检测到,则会返回一系列直接从object继承的虚拟类。在对象具有属性的情况下需要添加,但这仅在代码库中使用一次。它需要保持最新,但它解决了这个问题。

# ReadTheDocs
ON_RTD = os.environ.get('READTHEDOCS', None) == 'True'
if not ON_RTD:

    #... do the normal import here ...

else:

    class QMockObject(object):
        def __init__(self, *args, **kwargs):
            super(QMockObject, self).__init__()

        def __call__(self, *args, **kwargs):
            return None

    class QApplication(QMockObject):
        pass

    class pyqtSignal(QMockObject):
        pass

    class pyqtSlot(QMockObject):
        pass

    class QObject(QMockObject):
        pass

    class QAbstractItemModel(QMockObject):
        pass

    class QModelIndex(QMockObject):
        pass

    class QTabWidget(QMockObject):
        pass

    class QWebPage(QMockObject):
        pass

    class QTableView(QMockObject):
        pass

    class QWebView(QMockObject):
        pass

    class QAbstractTableModel(QMockObject):
        pass

    class Qt(QMockObject):
        DisplayRole = None

    class QWidget(QMockObject):
        pass

    class QPushButton(QMockObject):
        pass

    class QDoubleSpinBox(QMockObject):
        pass

    class QListWidget(QMockObject):
        pass

    class QDialog(QMockObject):
        pass

    class QSize(QMockObject):
        pass

    class QTableWidget(QMockObject):
        pass

    class QMainWindow(QMockObject):
        pass

    class QTreeWidget(QMockObject):
        pass

    class QAbstractItemDelegate(QMockObject):
        pass

    class QColor(QMockObject):
        pass

    class QGraphicsItemGroup(QMockObject):
        pass

    class QGraphicsItem(QMockObject):
        pass

    class QGraphicsPathItem(QMockObject):
        pass

    class QGraphicsTextItem(QMockObject):
        pass

    class QGraphicsRectItem(QMockObject):
        pass

    class QGraphicsScene(QMockObject):
        pass

    class QGraphicsView(QMockObject):
        pass

    app = None