I have a package that has a dependency that is not pip
installable. In order to be able to build the documentation, I am trying to mock the non-installable package using MagicMock
.
However I stumbled across a problem with multiple inheritance: when one of the parent classes is a mock class I get:
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses
The following example illustrates the issue:
file: class_a.py
class A:
pass
file: code.py
from class_a import A
class B:
pass
class C(A, B):
pass
file: test.py
import sys
from unittest import mock
# inspired by https://stackoverflow.com/a/37363830/1860757
MOCK_MODULES = ['class_a', ]
sys.modules.update((mod_name, mock.MagicMock()) for mod_name in MOCK_MODULES)
import code
code.C()
If I run python3 test.py
I get the above exception.
If I comment the line starting with sys.modules.update
, it all behaves as expected.
Is there a way to mock modules or classes such that multiple inheritance continue working?