derwiki-adroll / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

Can't patch a property on an instance #117

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago

>>> from mock import patch
>>> class Foo(object):
...  @property
...  def foo(self):
...   return 3
... 
>>> foo = Foo()
>>> p = patch.object(foo, 'foo', 4)
>>> p.start()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/compile/mock/mock.py", line 1280, in __enter__
    setattr(self.target, self.attribute, new_attr)
AttributeError: can't set attribute

Original issue reported on code.google.com by fuzzyman on 4 Sep 2011 at 3:07

GoogleCodeExporter commented 9 years ago
One way to fix:

Detect that although we're patching an instance, the object being replaced is 
actually a descriptor from a class. Replace the descriptor with a custom one 
that tracks *which* instances have been patched (and with what) and delegate to 
the real descriptor for any other instance.

Original comment by fuzzyman on 4 Sep 2011 at 4:16