'Can `MagicMock` compare with int in python3?
I am migrating python's version (2->3) of my project. The tests works fine for python2, but complains for python3, the error is like
TypeError: '>' not supported between instances of 'MagicMock' and 'int'
here is a minimal case
# test_mock.py
try:
from mock import MagicMock
except:
from unittest.mock import MagicMock
def test_mock_func():
a = MagicMock()
b = a.value
if b > 100:
assert True
else:
assert True
just run py.test .
These hacks not work
MagicMock.__le__ = some_le_method # just not working
MagicMock.__le__.__func__.__code = some_le_method.__func__.__code__ # wrapper_descriptor does not have attribute __func__
Solution 1:[1]
You should assign the __gt__ inside b or a.value
# self is MagicMock itself
b.__gt__ = lambda self, compare: True
# or
a.value.__gt__ = lambda self, compare: True
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cropse |
