From e1bcc64dfcde1133e174627f8466c02c817d7aa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20Kir=C3=A1ly?= Date: Wed, 13 Nov 2024 21:02:43 +0100 Subject: [PATCH] [ENH] add `_get_clone_plugins` to allow packages to customize clone plugins (#383) Adds a `_get_clone_plugins` method to `BaseObject` to allow packages to override the `clone` logic with custom plugins. --- skbase/base/_base.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/skbase/base/_base.py b/skbase/base/_base.py index daf9fdca..9f588237 100644 --- a/skbase/base/_base.py +++ b/skbase/base/_base.py @@ -176,11 +176,41 @@ def clone(self): ------ RuntimeError if the clone is non-conforming, due to faulty ``__init__``. """ - self_clone = _clone(self, base_cls=BaseObject) + # get plugins for cloning, if present (empty by default) + clone_plugins = self._get_clone_plugins() + + # clone the object + self_clone = _clone(self, base_cls=BaseObject, clone_plugins=clone_plugins) + + # check the clone, if check_clone is set (False by default) if self.get_config()["check_clone"]: _check_clone(original=self, clone=self_clone) + + # return the clone return self_clone + @classmethod + def _get_clone_plugins(cls): + """Get clone plugins for BaseObject. + + Can be overridden in subclasses to add custom clone plugins. + + If implemented, must return a list of clone plugins for descendants. + + Plugins are loaded ahead of the default plugins, and are used in the order + they are returned. + This allows extenders to override the default behaviours, if desired. + + Returns + ------- + list of str + List of clone plugins for descendants. + Each plugin must inherit from ``BaseCloner`` + in ``skbase.base._clone_plugins``, and implement + the methods ``_check`` and ``_clone``. + """ + return None + @classmethod def _get_init_signature(cls): """Get class init signature.