Import only static class method

I have the following decorator in a base class:

class BaseTests(TestCase):
    @staticmethod
    def check_time(self, fn):
        @wraps(fn)
        def test_wrapper(*args,**kwargs):
            # do checks ...
        return test_wrapper

And the following class inheriting from BaseTests:

from path.base_posting import BaseTests
from path.base_posting.BaseTests import check_time  # THIS LINE DOES NOT WORK!

class SpecificTest(BaseTests):

    @check_time # use the decorator
    def test_post(self):
        # do testing ...

I would like to use the decorator in SpecificTest, as mentioned above, without using BaseTests.check_time, because they have long names in the source code, and I have to use it in many places. Any ideas?

EDIT: I decided to make check_time an independent function in the BaseTests file and just import

from path.base_posting import BaseTests, check_time
+5
source share
1 answer

Simply put

check_time = BaseTests.check_time

in your second module:


from module_paths.base_posting import BaseTests
check_time = BaseTests.check_time

class SpecificTest(BaseTests):

    @check_time # use the decorator
    def test_post(self):
        # do testing ...

You may also need to reconsider creating a check_timestatic method, as it seems like your use case uses it more as a stand-alone function than as a static method.

+9

All Articles