I'm trying to monkeypatch a method on SomeClass
from an imported package:
from somepackage import SomeClass
def newmethod(obj, node, **kwargs):
""" """
SomeClass.oldmethod = newmethod
Where obj
and node
are in the default call signature of SomeClass.oldmethod
:
class SomeClass(object):
def oldmethod(obj, node):
""" """
I'm aware that monkeypatching is not good practice, but we need a workaround while we fix some issues that otherwise can't be tackled. The above approach works FINE, but we'd like to use partial functions to do this. For example:
from functools import partial
newmethod_a = partial(newmethod, foo='a')
newmethod_b = partial(newmethod, foo='b')
The partial function is being called because we need to pass different **kwargs. But when I try to overload now:
SomeClass.oldmethod = newmethod_a
I get an error related to the number of arguments passed, but it's very specific to my problem so pasting it might not be helpful... The error I think is related to the call signature of oldmethod
taking two positional arguments (obj, node
), and my partial functions aren't passing a reference to the obj
and node
correctly. I've tried different constructions like:
newmethod_a = partial(SomeClass.newmethod, foo='a')
I'm sorry that I can't produce a minimal working example. I hoped maybe an expert would just recognize this issue from experience and tell me if what I'm attempting is even possible within the scope of partial
.
Thanks