import pytest


@pytest.fixture
def observe(monkeypatch):
    def patch(module, func):
        original_func = getattr(module, func)

        def wrapper(*args, **kwargs):
            result = original_func(*args, **kwargs)
            self.calls[self.last_call] = (args, kwargs, result)
            self.last_call += 1
            return result

        self = wrapper
        self.calls = {}
        self.last_call = 0
        monkeypatch.setattr(module, func, wrapper)
        return wrapper
    return patch

import pytest


@pytest.fixture
def observe(monkeypatch):
    """
    Wrap a function so its call history can be inspected.

    Example:

    # foo.py
    def func(bar):
        return 2 * bar

    # test.py
    import pytest
    import foo

    def test_func(observe):
        observer = observe(foo, "func")
        assert foo.func(3) == 6
        assert foo.func(-5) == -10
        assert len(observer.calls) == 2
    """
    class ObserverFactory:
        def __init__(self, module, func):
            self.original_func = getattr(module, func)
            self.calls = []
            monkeypatch.setattr(module, func, self)

        def __call__(self, *args, **kwargs):
            result = self.original_func(*args, **kwargs)
            self.calls.append((args, kwargs, result))
            return result
    return ObserverFactory

Clean up test helper, add example
Clean up test helper, add example

  import pytest
  
  
  @pytest.fixture
  def observe(monkeypatch):
-     def patch(module, func):
-         original_func = getattr(module, func)
+     """
+     Wrap a function so its call history can be inspected.
  
+     Example:
+ 
+     # foo.py
+     def func(bar):
+         return 2 * bar
+ 
+     # test.py
+     import pytest
+     import foo
+ 
+     def test_func(observe):
+         observer = observe(foo, "func")
+         assert foo.func(3) == 6
+         assert foo.func(-5) == -10
+         assert len(observer.calls) == 2
+     """
+     class ObserverFactory:
+         def __init__(self, module, func):
+             self.original_func = getattr(module, func)
+             self.calls = []
+             monkeypatch.setattr(module, func, self)
+ 
-         def wrapper(*args, **kwargs):
+         def __call__(self, *args, **kwargs):
-             result = original_func(*args, **kwargs)
+             result = self.original_func(*args, **kwargs)
-             self.calls[self.last_call] = (args, kwargs, result)
+             self.calls.append((args, kwargs, result))
-             self.last_call += 1
              return result
+     return ObserverFactory
  
-         self = wrapper
-         self.calls = {}
-         self.last_call = 0
-         monkeypatch.setattr(module, func, wrapper)
-         return wrapper
-     return patch
- 
Clean up test helper, add example
## Code Before:
import pytest


@pytest.fixture
def observe(monkeypatch):
    def patch(module, func):
        original_func = getattr(module, func)

        def wrapper(*args, **kwargs):
            result = original_func(*args, **kwargs)
            self.calls[self.last_call] = (args, kwargs, result)
            self.last_call += 1
            return result

        self = wrapper
        self.calls = {}
        self.last_call = 0
        monkeypatch.setattr(module, func, wrapper)
        return wrapper
    return patch

## Instruction:
Clean up test helper, add example
## Code After:
import pytest


@pytest.fixture
def observe(monkeypatch):
    """
    Wrap a function so its call history can be inspected.

    Example:

    # foo.py
    def func(bar):
        return 2 * bar

    # test.py
    import pytest
    import foo

    def test_func(observe):
        observer = observe(foo, "func")
        assert foo.func(3) == 6
        assert foo.func(-5) == -10
        assert len(observer.calls) == 2
    """
    class ObserverFactory:
        def __init__(self, module, func):
            self.original_func = getattr(module, func)
            self.calls = []
            monkeypatch.setattr(module, func, self)

        def __call__(self, *args, **kwargs):
            result = self.original_func(*args, **kwargs)
            self.calls.append((args, kwargs, result))
            return result
    return ObserverFactory
