I applied a different answer to this question, but after the context manager, the corrected function was not the same as before.
My fixed function looks like this:
def f(foo=True): pass
In my test, I did the following:
with patch.object(f, 'func_defaults', (False,)):
When f called after (not in) the context manager, the default value completely disappeared, and did not return to the previous value. Calling f without arguments gave a TypeError error TypeError: f() takes exactly 1 argument (0 given)
Instead, I simply did this before my test:
f.func_defaults = (False,)
And this is after my test:
f.func_defaults = (True,)
source share