Consider that I have 2 files:
some_module.py
def get_value_1():
return 1
def print_value_1():
print(get_value_1())
and main file main.py
from unittest.mock import patch
import some_module
from some_module import print_value_1, get_value_1
def mocked_value():
return 2
if __name__ == '__main__':
print_value_1() # prints 1
with patch.object(some_module, 'get_value_1', mocked_value):
print_value_1() # prints 2
print(some_module.get_value_1()) # prints 2
print(get_value_1()) # prints 1 - DESIRABLE RESULT IS TO PRINT ALSO 2
As you can see because I explicitly imported the get_value_1 function, the patch is not working on it. I understand basically why, that's because it uses a reference and the reference is imported before the main ran (checked it with calling id() on each invoked function and saw the addresses). Can I somehow hijack also the imported reference?
(It won't be enough to patch it only in main.py, I want it to be patched all over the project, so for example in some other some_other_module.py there will be: from some_module import get_value_1 and when I call get_value_1() it will call the patched function and return the value 2)