Skip to content

patch_object

autogen.import_utils.patch_object #

patch_object(o, *, missing_modules, dep_target, fail_if_not_patchable=True, except_for=None)
Source code in autogen/import_utils.py
def patch_object(
    o: T,
    *,
    missing_modules: Iterable[str],
    dep_target: str,
    fail_if_not_patchable: bool = True,
    except_for: Optional[Union[str, Iterable[str]]] = None,
) -> T:
    patcher = PatchObject.create(o, missing_modules=missing_modules, dep_target=dep_target)
    if fail_if_not_patchable and patcher is None:
        raise ValueError(f"Cannot patch object of type {type(o)}")

    except_for = except_for if except_for is not None else []
    except_for = [except_for] if isinstance(except_for, str) else except_for

    return patcher.patch(except_for=except_for) if patcher else o