[TIP] removing attributes and dictionary keys using mock.patch

Barry Warsaw barry at python.org
Fri Dec 5 11:56:56 PST 2014


On Dec 05, 2014, at 06:19 PM, Michael Foord wrote:

>So, removing an attribute is not possible in the *general* case. In the
>specific case of it being an instance attribute you can remove it from the
>object dictionary. In which case just having patch.dict support key removal
>would work.

Would it be feasible to pretend to delete by something that raises
AttributeError instead?

In some sense unittest.mock almost should be its own thing outside of
unittest.  In the rare cases where you have to monkeypatch something,
unittest.mock comes darn close to providing this useful functionality in a way
that allows you to narrowly monkeypatch and safely unpatch after the task is
done.

-Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20141205/9c0af4d2/attachment.pgp>


More information about the testing-in-python mailing list