Overwriting a method from another module

Viewed 64

I am working with jira-python and want to overwrite the existing method Issue.update() method. However, my code is never calling the modification.

from jira import JIRA
from jira.resources import Issue

class Issue(Issue):
    def update(self):
        print("overwritten")
        super(Issue, self).update() # I also tried super().update()

jira = JIRA("...")  # holds a reference to the JIRA instance
issues = jira.search_issues("")  # holds a list of JIRA Issues

for issue in issues:
    // I would expect this call to print "overwritten"
    issue.update()
1 Answers

You can monkey-patch the method with a wrapper like this:

from jira.resources import Issue

def my_update(self):
    print('overwritten')
    original_update(self)

original_update = Issue.update
Issue.update = my_update
Related