Author stevenk
Recipients eric.snow, kushal.das, michael.foord, rbcollins, stevenk
Date 2016-09-12.03:23:03
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
This is in fact, working entirely as expected.

If we define a property that raises an AttributeError, then __getattr__() will be called, since the descriptor protocol says that an attribute error is a missing descriptor.

>>> class Foo(object):
...   def __getattr__(self, attr):
...     return 42
...   @property
...   def bacon(self):
...     print(1)
...     return int.lala
>>> Foo().bacon

If we then follow a similar pattern using mock:

>>> from unittest import mock
>>> a_mock = mock.MagicMock()
>>> def foo():
...   print(1)
...   raise AttributeError()
>>> no_attribute = mock.PropertyMock(side_effect=foo)
>>> type(a_mock).property = no_attribute
<MagicMock name='' id='139971099507232'>

You can see that the method is called, since we print one, but then what is going on is that MagicMock.__getattr__ is called, which has the behavior to return a new mock, like so:

>>> a_mock.b
<MagicMock name='mock.b' id='139971099646776'>
Date User Action Args
2016-09-12 03:23:03stevenksetrecipients: + stevenk, rbcollins, michael.foord, eric.snow, kushal.das
2016-09-12 03:23:03stevenksetmessageid: <>
2016-09-12 03:23:03stevenklinkissue21254 messages
2016-09-12 03:23:03stevenkcreate