My favorites | Sign in
Project Home Downloads Issues Source
New issue   Search
  Advanced search   Search tips   Subscriptions
Issue 117: Can't patch a property on an instance
2 people starred this issue and may be notified of changes. Back to list
Status:  Accepted
Owner:  fuzzyman

Sign in to add a comment
Project Member Reported by fuzzyman, Sep 4, 2011

>>> 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/", line 1280, in __enter__
    setattr(, self.attribute, new_attr)
AttributeError: can't set attribute
Sep 4, 2011
Project Member #1 fuzzyman
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.
Sign in to add a comment

Powered by Google Project Hosting