-1

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
Tim P
  • 29
  • 6
  • 2
    Hey, to the anonymous close voters, since we are dealing with a newbie perhaps you expand them the courtesy of explaining what motivates your close votes, no? – JL Peyret Nov 21 '19 at 08:42

1 Answers1

3
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, As original incorrect code. You could also patch B.buggy_fn instead which would be even better.

JL Peyret
  • 10,917
  • 2
  • 54
  • 73