I'm importing a library someone else made and I'm wanting to change the way a particular class method works from that library so I've copied the class method to my own file and wanting to replace it at runtime. This seems to work just fine for functions but seems to fall apart for class methods.
a.library.package.library_file.py
class LibraryClass(ParentClass):
@classmethod
def get_cost(cls, time):
return int(time * cls.hourly)
I'm wanting to replace it with this
class LibraryClass(ParentClass):
@classmethod
def get_cost(cls, time):
return 1234
I've tried to just do a normal replace which works just fine for regular functions
import a.library.package.library_file
...
a.library.package.library_file.LibraryClass.get_cost = get_cost
But it doesn't seem to work right at all, the method is called with the wrong arguments at the wrong time and results in a crash. After some research on Google, StackOverflow, and Python I began trying to use the mock classes.
from unittest.mock import patch
@patch.object('a.library.package.library_file.LibraryClass', 'get_cost')
def get_cost(cls, time):
return 1234
The good news is it doesn't crash, bad news is it doesn't do anything, the old code is still there and it's like my code doesn't exist.
I've tried all kinds of other ways to do this such as
import a.library.package.library_file
@patch.object(a.library.package.library_file.LibraryClass, 'get_cost')
...
or
from a.library.package.library_file import LibraryClass
@patch.object(LibraryClass, 'get_cost')
...
but every time the method is never touched. It's like my code doesn't exist and the old code is used.