I have a module A which imports buggy function like this:
from .B import buggy_fn as _buggy_fn
Is it possible to replace it outside?
import A
#...? = bug_free_implementation
I have a module A which imports buggy function like this:
from .B import buggy_fn as _buggy_fn
Is it possible to replace it outside?
import A
#...? = bug_free_implementation
import A
def bug_free_implementation():
...
A._buggy_fn = bug_free_implementation
See also monkey patching. Or mock
even though that’s not its purpose. But in any case you have to make sure your mod hits A
before other stuff calls, or imports, A
s original incorrect code. You could also patch B.buggy_fn
instead which would be even better.