@contextmanager
def patch(namespace, name, function):
    """Patches `namespace`.`name` with `function`."""
    if isinstance(namespace, LazyObject):
        if namespace._wrapped is None:
            namespace._setup()
        namespace = namespace._wrapped
    try:
        original = getattr(namespace, name)
    except __HOLE__:
        original = NotImplemented
    try:
        setattr(namespace, name, function)
        yield
    finally:
        if original is NotImplemented:
            delattr(namespace, name)
        else:
            setattr(namespace, name, original)
dataset/ETHPy150Open MontmereLimited/django-lean/django_lean/utils.py/patch