def apply_patch()

in arctic_inference/patching.py [0:0]


    def apply_patch(cls):
        """
        Patches the target class or module by replacing its attributes with
        those defined on the ArcticPatch subclass. Attributes are directly
        assigned to the target, and classmethods are re-bound to the target
        class before assignment.

        Raises:
            TypeError: If the ArcticPatch subclass is not defined with a target
                class or module.
            ValueError: If an attribute is already patched on the target.
        """
        if cls is ArcticPatch or not issubclass(cls, ArcticPatch):
            raise TypeError("apply_patch() must be called on a subclass of "
                            "ArcticPatch")

        target = cls._arctic_patch_target

        if "_arctic_patches" not in target.__dict__:
            target._arctic_patches = {}

        for name, attr in cls.__dict__.items():

            # Skip special names and the '_arctic_patch_target' itself
            if name in ("_arctic_patch_target", "__dict__", "__weakref__",
                        "__module__", "__doc__", "__parameters__",):
                continue

            # Check if the attribute has already been patched
            if name in target._arctic_patches:
                patch = target._arctic_patches[name]
                raise ValueError(f"{target.__name__}.{name} is already "
                                 f"patched by {patch.__name__}")
            target._arctic_patches[name] = cls

            # If classmethod, re-bind it to the target
            if isinstance(attr, MethodType):
                attr = MethodType(attr.__func__, target)

            # Patch the target with the new attribute
            replace = hasattr(target, name)
            setattr(target, name, attr)
            action = "replaced" if replace else "added"
            logger.info(f"{cls.__name__} {action} {target.__name__}.{name}")