[TIP] removing attributes and dictionary keys using mock.patch
chris at simplistix.co.uk
Mon Dec 8 03:39:00 PST 2014
On 07/12/2014 21:51, 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.
>> Yeah, my take on it is "anything where delattr works"...
> But how will you know?
Well, if it doesn't work, I'd expect it to blow up with an exception or
not do anything. Either way, I'm one of those people who believes in
writing and verifying a test fails first, in the way I expect, so should
be okay ;-)
>> A whole separate set of decorators/context managers/etc seems like overkill, so I hope that's not your preference ;-)
> A marker object would be better. Like:
> DELETE = sentinel.DELETE
> with patch('foo.bar', attribute=DELETE):
Glad we're in agreement!
> Please create on issue on bugs.python.org, preferably with patch, tests and docs :-)
Hg branch good? I do have commits rights ;-)
How would this get into the Mock release for Python 2?
What version of Python 3 would it land in? 3.5?
Simplistix - Content Management, Batch Processing & Python Consulting
More information about the testing-in-python