Strange classmethod mock behavior

F

Fabio Zadrozny

I'm trying to mock a classmethod in a superclass but when restoring it
a strange behavior happens in subclasses (tested on Python 2.5)

Anyone knows why this happens? (see test case below for details)
Is there any way to restore that original method to have the original behavior?

import unittest

class Test(unittest.TestCase):

def testClassmethod(self):
class Super():
@classmethod
def GetCls(cls):
return cls

class Sub(Super):
pass

self.assertEqual(Sub.GetCls(), Sub)

original = Super.GetCls
#Mock Super.GetCls, and do tests...
Super.GetCls = original #Restore the original classmethod
self.assertEqual(Sub.GetCls(), Sub) #The call to the
classmethod subclass returns the cls as Super and not Sub as expected!

if __name__ == '__main__':
unittest.main()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top