gpt4 book ai didi

python - 为阅读文档扩展 scipy.stats.rv_continuous 时出现元类错误

转载 作者:行者123 更新时间:2023-11-28 18:40:06 25 4
gpt4 key购买 nike

在我的 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.

关于python - 为阅读文档扩展 scipy.stats.rv_continuous 时出现元类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27325165/

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