Steve Kowalik added the comment:

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
1
42

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
>>> a_mock.property
1
<MagicMock name='mock.property' 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'>

----------
nosy: +stevenk

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue21254>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to