Contributors mailing list archives
Re: Overwriting a method in OCA module.by "Kitti U." <firstname.lastname@example.org> - 15/09/2015 07:35:45
Here is what I want to propose for contributing.md:
Monkey Patch is simply,
- The class that will Patch (replace) directly to the original module A's method. (it is equal to modifying method in module A directly, and still preserve the calling chain)
- Module A as original module
- Module B inherit module A (by calling super())
- Module C (our new module), which also want to inherit Module A (but face overwrite challenge)
i.e., / Module C / account_voucher_monkeypatch.py
i.e., / Module C / account_voucher.py
3. Submit new PR to Odoo, proposing the hook_point to original file in module A (same as what we have done in Monkey Patch)
- MonkeyPatch will protect module B from being out of chain
- MonkeyPatch file will serve as warning message that module is being overwritten, and PR must be raised and fixed.
- MonkeyPatch can be easily removed, once the PR is accepted, as hook_point is incoperated into the core.
On 13-09-15 10:37, Kitti U. wrote: > Dear Stefan, > > Thank you for brought up an interesting topic. Still I do not quite > understand it. > > As I try to understand from the code, the MonkeyPatch class (i.e., > account.voucher.monkeypatch), its duty is to redirect the original > method to our own modified method. Right? > > Though it will help not having to overwrite the method directly to the > underlining Class (i..e, account.voucher), but the affect is the same > as a plain method overwrite. Is it not? > > For example, months later, the original method has been enhanced by > Odoo S.A (core code). Those changes will be ignored, as it the > monkeypatch class will still help redirect it to our modified method > (which now become outdated). > > So, this won't help, is it? What will be the benefit of using it when > compare to overwriting it? Hi Kitti, consider the case where module B overrides the method from module A. Your module C overwrites the method, and as it happens, is loaded in Odoo after module B. Your version does not call super(), which breaks the inheritance of module A from module B, as the override from B is never called. When applying a monkeypatch instead of overwriting the method from your module C, then the inheritance chain in which B calls the method from module A remains intact. B's override is still registered in the ORM as the first entry point of the method and when B calls super(), it ends up calling the monkeypatched method from your module C. You are right in that both methods can cause bugfixes in the original version of the method to be easily overlooked. For the record, the class in which the monkeypatch is applied is pretty much irrelevant. Regards, Stefan.
Post to: mailto:email@example.com