1

Similar questions were asked before:

functools.wraps creates a magic method called __wrapped__ in the decorator, which allows to access the wrapped function. When you have multiple decorators, though, this would return an inner decorator, not the original function.

How to avoid or bypass multiple decorators?

Renato Byrro
  • 3,578
  • 19
  • 34

1 Answers1

3

To bypass or avoid multiple decorators and access the inner most function, use this recursive method:

def unwrap(func):
    if not hasattr(func, '__wrapped__'):
        return func

    return unwrap(func.__wrapped__)

In pytest, you can have this function in conftest.py and access throughout your tests with:

# conftest.py
import pytest

@pytest.fixture
def unwrap():
    def unwrapper(func):
        if not hasattr(func, '__wrapped__'):
            return func

        return unwrapper(func.__wrapped__)

    yield unwrapper
# my_unit_test.py
from my_module import decorated_function

def test_my_function(unwrap):
    decorated_function_unwrapped = unwrap(decorated_function)
    assert decorated_function_unwrapped() == 'something'
Renato Byrro
  • 3,578
  • 19
  • 34