In foo.py
:
from qux.bar import Baz def some_fn(arg=Baz): # Something involving arg() pass
In test_foo.py
(running under pytest, with pytest-mock):
from .foo import some_fn def test_some_fn(mocker): mocker.patch('foo.Baz') # Oh no, tests don't mock it out right!
It took me a tick to realize that the function definition was being evaluated at import time, including the values for the kwargs. Even though the body, of course, wasn’t being evaluated. I solved it with this change to foo.py
:
from qux.bar import Baz def some_fn(arg=None): if arg is None: arg = Baz # Something involving arg() pass
This is related to the thing where you never write a function signature with {}
or []
in it, because you’ll create an instance at evaluation time that’s shared among all the calls to that function.