Question

I try do this:

import unittest

def decorator(cls):
    class Decorator(cls):
        def __init__(self, *args, **kwargs):
            super(Decorator, self).__init__(*args, **kwargs)

    return Decorator

@decorator
class myClass(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(myClass, self).__init__(*args, **kwargs)
        self.test = 'test'

    def test_test(self):
        pass


myClass().run()

But I get recursion in MyClass.__init__. Are there any ways to avoid this?

Was it helpful?

Solution

You cannot use super(myClass, self) within a decorated class in this way.

myClass is looked up as a global, and the global myClass is rebound to Decorator, so you are telling Python to look in the class MRO for __init__ starting from Decorator which is myClass, which calls super(myClass, self).__init__(), looking up myClass as a global, which is bound to Decorator, etc.

The easiest work-around is to not use super() here:

@decorator
class myClass(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        unittest.TestCase.__init__(self, *args, **kwargs)
        self.test = 'test'

This is one of the reasons why in Python 3 the argument-less version of super() was introduced, giving methods a __class__ cell instead.

You could jump through some (very tricky) hoops to re-compile the myClass.__init__() method to give it a myClass closure bound to the original undecorated class object instead, but for a unittest, I would not bother.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top