gpt4 book ai didi

python - 狮身人面像文档 :automodule: with Mock imports

转载 作者:行者123 更新时间:2023-11-28 19:20:41 27 4
gpt4 key购买 nike

我正在尝试根据 this 答案将 sphinx-doc :automodule: 与模拟模块结合使用。具体来说,我将 Mock 用于 PyQt5 模块导入,这在 ReadTheDocs 上不可用。

奇怪的是,我发现生成的文档中没有包含任何继承自模拟模块类的类。由于某种原因,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 * 样式导入。

我可以确认将父类(super class)更改为 object 会导致正确记录类,删除 Mock(在本地生成)也是如此。使用 :autoclass: 强制生成文档会导致单行显示该类继承自 Mock

最佳答案

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

关于python - 狮身人面像文档 :automodule: with Mock imports,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25960309/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com