Contributors mailing list archives
Re: Overwriting a method in OCA module.by
Opener B.V., Stefan Rijnhart
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.