Sphinx-DOC:自动模块:模拟导入

Sphinx-doc :automodule: with Mock imports(Sphinx-DOC:自动模块:模拟导入)
本文介绍了Sphinx-DOC:自动模块:模拟导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据this答案,将sphinx-doc:Automodule:与Mock-ed out模块结合使用。具体地说,我正在使用Mock进行在ReadTheDocs上不可用的PyQt5模块导入。 奇怪的是,我发现从模拟模块的类继承的任何类都不包括在生成的文档中。似乎由于某种原因,狮身人面像无法看到它们。

我稍微自定义的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

推荐答案

我最终通过不使用MockforQt对象解决了这个问题。在我的应用程序中,有一个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

这篇关于Sphinx-DOC:自动模块:模拟导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)