扩展模拟的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.
相关文章