扩展模拟的scipy.stats.rv_Continue以读取文档时出现元类错误

Metaclass error when extending scipy.stats.rv_continuous mocked for Read the Docs(扩展模拟的scipy.stats.rv_Continue以读取文档时出现元类错误)
本文介绍了扩展模拟的scipy.stats.rv_Continue以读取文档时出现元类错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Python项目中,我像这样扩展scipy.stats.rv_continuous

class GenlogisticGen(LmomDistrMixin, scipy.stats.rv_continuous):
    ...

我正尝试在Read the Docs上生成文档,但遇到生成错误:

class GenlogisticGen(LmomDistrMixin, scipy.stats.rv_continuous):
TypeError: metaclass conflict: the metaclass of a derived class must
be a (non-strict) subclass of the metaclasses of all its bases

请注意,我是根据Read the Docs FAQ模拟scipy.stats模块。

我猜想,通过模仿基类,有些地方出了问题。但是什么?

推荐答案

我今天也遇到了同样的问题,但使用的是一个与Qt相关的类。问题是ReadTheDocs建议的Mock类具有与预期不同的元类。以下是对问题和解决方案的描述,其中您需要的是从object继承的基类:

# =============================================================================
# Part 1. Set up the mock (you would put this in conf.py for Sphinx/autodoc).
# =============================================================================

import os
import sys
from unittest.mock import MagicMock


class Mock(MagicMock):
    """
    Mock class that gives whatever attribute it's asked for, as per
    https://docs.readthedocs.io/en/latest/faq.html. Intended to be used when
    you can't genuinely install/import a module because ReadTheDocs (RTD)
    doesn't allow the installation of modules with C (rather than pure Python)
    code.
    """
    @classmethod
    def __getattr__(cls, name: str):
        return MagicMock()


class SimpleClass(object):
    """
    Dummy base class to replace a :class:`Mock` version; see
    ``FIX_THE_PROBLEM`` below.
    """
    pass


MOCK_MODULES = [
    # Things that ReadTheDocs won't install, but we want:
    'PyQt5',
    'PyQt5.QtCore',
    'PyQt5.QtGui',
    'PyQt5.QtNetwork',
    'PyQt5.QtWidgets',
]

ON_READTHEDOCS = os.environ.get('READTHEDOCS') == 'True'  # the normal test
ON_READTHEDOCS = True  # for testing!
if ON_READTHEDOCS:
    # Insert copies of our Mock class for modules we want to fake.
    sys.modules.update((mod_name, Mock()) for mod_name in MOCK_MODULES)


MODULE_MEMBERS_TO_MAKE_SIMPLE_CLASS = (
    ('PyQt5.QtCore', 'QAbstractListModel'),
    ('PyQt5.QtCore', 'QAbstractTableModel'),
    # etc.
)


FIX_THE_PROBLEM = False  # to see the problem, or True to fix it!
if FIX_THE_PROBLEM:
    for module_name, class_name in MODULE_MEMBERS_TO_MAKE_SIMPLE_CLASS:
        setattr(sys.modules[module_name], class_name, SimpleClass)


# =============================================================================
# Part 2. Simulate some user code.
# =============================================================================

from PyQt5.QtCore import QAbstractListModel

print(QAbstractListModel)
# For real:                 <class 'PyQt5.QtCore.QAbstractListModel'>
# If ON_READTHEDOCS:        <MagicMock id='139789117901176'>
# If FIX_THE_PROBLEM too:   <class '__main__.SimpleClass'>

print(type(QAbstractListModel))
# For real:                 <class 'sip.wrappertype'>
# If ON_READTHEDOCS:        <class 'unittest.mock.MagicMock'>
# If FIX_THE_PROBLEM too:   <class 'type'>


class MyRandomMixin(object):
    pass


class MyDerived(QAbstractListModel, MyRandomMixin):
    pass

# For real: it's happy.
# If ON_READTHEDOCS: will not create MyDerived; will crash with:
#   TypeError: metaclass conflict: the metaclass of a derived class must be a
#   (non-strict) subclass of the metaclasses of all its bases
# If ON_READTHEDOCS and FIX_THE_PROBLEM: happy again.

这篇关于扩展模拟的scipy.stats.rv_Continue以读取文档时出现元类错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

相关文档推荐

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中安全地调用随机文件上的类型?)