My question is similar to this question
Except that it's not my module to execute dosomething()
in thirdpartymodule_b
so I need to monkey patch thirdpartymodule_a
and be effective when it's executed in thirdpartymodule_b
My question is similar to this question
Except that it's not my module to execute dosomething()
in thirdpartymodule_b
so I need to monkey patch thirdpartymodule_a
and be effective when it's executed in thirdpartymodule_b
I don't see any difference between the question you referenced and your situation. In both cases a modules import a name from another module and you want to patch the object referenced by that name.
In that question the module is written by the author, in your question it's a third party module.
I think your problem lies in the fact the the third party already imported the object name, so if you try to replace the object in the module where it resides, the third party module will still use the old one, because it still references the old object.
module_1.py
class AForm(Form):
...
module_2.py
from module_1 import AForm
If you import module_2
and then monkey-patch module_1
replace AForm
with another object
class AFormPatched(Form):
...
import module_1
module_1.AForm = AFormPatched
Your changes will not affect module_2
, because module_2.AForm
still points to the original object.
To solve this:
Option 1. module_2
should look like:
import module_1
module_1.AForm # using AForm in this form
Option 2. Patch also module_2.AForm
:
class AFormPatched(Form):
...
import module_1
module_1.AForm = AFormPatched
import module_2
module_2.AForm = AFormPatched
Or don't patch module_1
you want only module_2
to use the patched version.
Option 3. Patch attributes of the object. If you don't want to or cannot monkey patch all the places where the object name is used, sometimes it works if you don't replace the object, but patch only some of its attributes. It depends on the object and the behavior you are trying to patch:
import third_party_module
third_party_module.AForm.__init__ = ...