garybernhardt/dingus — pull request #21 by chromy (2013-09-06 23:28:31+00:00)
Title: Patch should set __wrapped__ to work with pytest fixtures

Pytest has magic for injecting fixtures:

```python
import pytest
from dingus import patch
import urllib2

@pytest.fixture
def myfixture():
return "some fixture"

def test_a_thing(myfixture):
assert "fixture" in myfixture
# This passes.
```

This breaks when we start to use decorators (specifically dingus.patch):

```python
@patch('urllib2.urlopen')
def test_decorator(myfixture):
assert "fixture" in myfixture
# This is an error, pytest doesn't realise it should inject anything so calls
# test_decorator with no arguments
```

Unless we set the magic \_\_wrapped\_\_ variable on the decorating function:

```python
def test_wrapped_decorator(myfixture):
assert "fixture" in myfixture
old_func = test_wrapped_decorator
test_wrapped_decorator = patch('urllib2.urlopen')(test_wrapped_decorator)
test_wrapped_decorator.__wrapped__ = old_func
# This passes.
```

I know I could use patch as a context manger but I want to patch multiple things which gets kinda ugly with context mangers.

\_\_wrapped\_\_ seems to be (sort of) a real thing, it's used in other places than just pytest and in >3.x it's set for you by functools.wraps so I think it's legitimate to add it.

Hope that's okay! :)