Is it possible to override an internal module function without modifications on the given module?
The following example is the best simplification I can imagine. For the original problem look at the end of the question.
I have a package with the following structure:
a
├── b.py
├── c.py
└── __init__.py
Where b.py is
from c import c_func
def b_func():
print('b.b_func')
return c_func()
and c.py is
def c_func():
print('c.c_func')
return 'return_c'
I want to modify the internal call to c_func() from an external main.py
import a
from a.b import b_func
print('Calling b_func without modification')
solution = b_func()
print(solution)
# Trying to modify the internal function
print('Calling b_func with modification')
old_c_func = a.c.c_func
def new_c_func(*args, **kwargs):
print('do something in new_c_func')
return('return_new_c')
a.c.c_func = new_c_func
solution = b_func()
print(solution)
The previous code outputs the following
Calling b_func without modification
b.b_func
c.c_func
return_c
Calling b_func with modification
b.b_func
c.c_func
return_c
But I would expect
Calling b_func without modification
b.b_func
c.c_func
return_c
Calling b_func with modification
b.b_func
do something in new_c_func
return_new_c_func
The original problem relates to a private function of Scipy but I assume that the answer to my question generalises to the following problem:
import scipy
# Stack calls
# minimize calls scipy.optimize._minimize_trust_ncg
# See: https://github.com/scipy/scipy/blob/2526df72e5d4ca8bad6e2f4b3cbdfbc33e805865/scipy/optimize/_minimize.py#L463
# _minimize_trust_ncg calls scipy.optimize._trustregion._minimize_trust_region
# See: https://github.com/scipy/scipy/blob/2526df72e5d4ca8bad6e2f4b3cbdfbc33e805865/scipy/optimize/_trustregion_ncg.py#L39
from scipy.optimize import minimize
old_minimize_trust_region = scipy.optimize._trustregion._minimize_trust_region
def new_minimize_trust_region(*args, **kwargs):
print('new function')
return old_minimize_trust_region
scipy.optimize._trustregion._minimize_trust_region = new_minimize_trust_region
x0 = [2]
fun = lambda x: x**2 + 42
jac = lambda x: 2*x
hess = lambda x: 2
method = 'trust-ncg'
solution = minimize(fun, x0, method=method, jac=jac, hess=hess)
print(solution)