The __repr__
esentation of a Mock object that has been obtained as an attribute (or as the return of __call__
) of another Mock shows what looks like a name
attribute. This pseudoattribute however doesn't seem to exist in the class. For example:
>>> import mock
... x = mock.MagicMock()
... y = x.asdf
... z = y.hjkl
... a = z()
>>> a
<MagicMock name='mock.asdf.hjkl()' id='139706648472600'>
>>> 'name' in a.__dict__
False
In order to get this psuedoattribute, it seems that I can either parse the return of y.__repr__()
or some sort of recursive function like this:
def get_mock_name(some_mock):
if some_mock._mock_name:
return f'{get_mock_name(some_mock._mock_parent)}.{some_mock._mock_name}'
elif some_mock._mock_new_name:
return f'{get_mock_name(some_mock._mock_new_parent)}{some_mock._mock_new_name}'
else:
return 'mock'
While both work, is there any standard and/or builtin way of doing this?
Edit: no, I am not asking how to override __repr__