How to strip decorators from a function in Python

PythonDecorator

Python Problem Overview


Let's say I have the following:

def with_connection(f):
    def decorated(*args, **kwargs):
        f(get_connection(...), *args, **kwargs)
    return decorated

@with_connection
def spam(connection):
    # Do something

I want to test the spam function without going through the hassle of setting up a connection (or whatever the decorator is doing).

Given spam, how do I strip the decorator from it and get the underlying "undecorated" function?

Python Solutions


Solution 1 - Python

There's been a bit of an update for this question. If you're using Python 3, and @functools.wraps you can use __wrapped__ property for decorators from stdlib.

Here's an example from Python Cookbook, 3rd edition, section 9.3 Unwrapping decorators

>>> @somedecorator
>>> def add(x, y):
...     return x + y
...
>>> orig_add = add.__wrapped__
>>> orig_add(3, 4)
7
>>>

If you are trying to unwrap a function from custom decorator, the decorator function needs to use wraps function from functools See discussion in Python Cookbook, 3rd edition, section 9.2 Preserving function metadata when writing decorators

>>> from functools import wraps
>>> def somedecorator(func):
...    @wraps(func)
...    def wrapper(*args, **kwargs):
...       # decorator implementation here
...       # ......
...       return func(*args, **kwargs)
...
...    return wrapper

Solution 2 - Python

In the general case, you can't, because

@with_connection
def spam(connection):
    # Do something

is equivalent to

def spam(connection):
    # Do something

spam = with_connection(spam)

which means that the "original" spam might not even exist anymore. A (not too pretty) hack would be this:

def with_connection(f):
    def decorated(*args, **kwargs):
        f(get_connection(...), *args, **kwargs)
    decorated._original = f
    return decorated

@with_connection
def spam(connection):
    # Do something

spam._original(testcon) # calls the undecorated function

Solution 3 - Python

balpha's solution can be made more generalizable with this meta-decorator:

def include_original(dec):
    def meta_decorator(f):
        decorated = dec(f)
        decorated._original = f
        return decorated
    return meta_decorator

Then you can decorate your decorators with @include_original, and every one will have a testable (undecorated) version tucked away inside it.

@include_original
def shout(f):
    def _():
        string = f()
        return string.upper()
    return _

    

@shout
def function():
    return "hello world"

>>> print function()
HELLO_WORLD
>>> print function._original()
hello world

Solution 4 - Python

Behold, FuglyHackThatWillWorkForYourExampleButICantPromiseAnythingElse:

 orig_spam = spam.func_closure[0].cell_contents

Edit: For functions/methods decorated more than once and with more complicated decorators you can try using the following code. It relies on the fact, that decorated functions are __name__d differently than the original function.

def search_for_orig(decorated, orig_name):
	for obj in (c.cell_contents for c in decorated.__closure__):
		if hasattr(obj, "__name__") and obj.__name__ == orig_name:
			return obj
		if hasattr(obj, "__closure__") and obj.__closure__:
			found = search_for_orig(obj, orig_name)
			if found:
				return found
	return None

 >>> search_for_orig(spam, "spam")
 <function spam at 0x027ACD70>

It's not fool proof though. It will fail if the name of the function returned from a decorator is the same as the decorated one. The order of hasattr() checks is also a heuristic, there are decoration chains that return wrong results in any case.

Solution 5 - Python

You can now use the undecorated package:

>>> from undecorated import undecorated
>>> undecorated(spam)

It goes through the hassle of digging through all the layers of different decorators until it reaches the bottom function and doesn't require changing the original decorators. It works on both Python 2 and Python 3.

Solution 6 - Python

It's good practice to decorate decorators with functools.wraps like so:

import functools

def with_connection(f):
    @functools.wraps(f)
    def decorated(*args, **kwargs):
        f(get_connection(...), *args, **kwargs)
    return decorated

@with_connection
def spam(connection):
    # Do something

As of Python 3.2, this will automatically add a __wrapped__ attribute that lets you retrieve the original, undecorated function:

>>> spam.__wrapped__
<function spam at 0x7fe4e6dfc048>

However, instead of manually accessing the __wrapped__ attribute, it's better to use inspect.unwrap:

>>> inspect.unwrap(spam)
<function spam at 0x7fe4e6dfc048>

Solution 7 - Python

Instead of doing...

def with_connection(f):
    def decorated(*args, **kwargs):
        f(get_connection(...), *args, **kwargs)
    return decorated

@with_connection
def spam(connection):
    # Do something

orig_spam = magic_hack_of_a_function(spam)

You could just do...

def with_connection(f):
    ...

def spam_f(connection):
    ...

spam = with_connection(spam_f)

...which is all the @decorator syntax does - you can then obviously access the original spam_f normally.

Solution 8 - Python

the original function is stored in spam.__closure__[0].cell_contents.
Decorator uses closure to bind original function with extra layer of functionality. The original function must be stored in a closure cell kept by one of the functions in the nested structure of decorator.
Example:

>>> def add(f):
...     def _decorator(*args, **kargs):
...             print('hello_world')
...             return f(*args, **kargs)
...     return _decorator
... 
>>> @add
... def f(msg):
...     print('f ==>', msg)
... 
>>> f('alice')
hello_world
f ==> alice
>>> f.__closure__[0].cell_contents
<function f at 0x7f5d205991e0>
>>> f.__closure__[0].cell_contents('alice')
f ==> alice

this is the core principle of undecorated, you could refer to the source code for more details.

Solution 9 - Python

The usual approach to testing such functions is to make any dependencies, such as get_connection, configurable. Then you can override it with a mock while testing. Basically the same as dependency injection in the Java world but a lot simpler thanks to Pythons dynamic nature.

Code for it might look something like this:

# decorator definition
def with_connection(f):
    def decorated(*args, **kwargs):
        f(with_connection.connection_getter(), *args, **kwargs)
    return decorated

# normal configuration
with_connection.connection_getter = lambda: get_connection(...)

# inside testsuite setup override it
with_connection.connection_getter = lambda: "a mock connection"

Depending on your code you could find a better object than the decorator to stick the factory function on. The issue with having it on the decorator is that you'd have to remember to restore it to the old value in the teardown method.

Solution 10 - Python

Add a do-nothing decorator:

def do_nothing(f):
    return f

After defining or importing with_connection but before you get to the methods that use it as a decorator, add:

if TESTING:
    with_connection = do_nothing

Then if you set the global TESTING to True, you will have replaced with_connection with a do-nothing decorator.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHergeView Question on Stackoverflow
Solution 1 - PythonAlex VolkovView Answer on Stackoverflow
Solution 2 - PythonbalphaView Answer on Stackoverflow
Solution 3 - PythonjcdyerView Answer on Stackoverflow
Solution 4 - PythonWojciech BederskiView Answer on Stackoverflow
Solution 5 - PythonOinView Answer on Stackoverflow
Solution 6 - PythonAran-FeyView Answer on Stackoverflow
Solution 7 - PythondbrView Answer on Stackoverflow
Solution 8 - Pythonlyu.lView Answer on Stackoverflow
Solution 9 - PythonAnts AasmaView Answer on Stackoverflow
Solution 10 - PythonPaulMcGView Answer on Stackoverflow