Repository: kedro-org/kedro
File: kedro/io/core.py (Python)

Code before:
            except (DatasetError, FileNotFoundError, NotADirectoryError):
                raise
            except Exception as exc:
                message = f"Failed while saving data to dataset {self!s}.\n{exc!s}"
                raise DatasetError(message) from exc

        save.__annotations__["data"] = save_func.__annotations__.get("data", Any)
        save.__annotations__["return"] = save_func.__annotations__.get("return")
        save.__savewrapped__ = True  # type: ignore[attr-defined]
        return save

    def __init_subclass__(cls, **kwargs: Any) -> None:
        """Customizes the behavior of subclasses of AbstractDataset during
        their creation. This method is automatically invoked when a subclass
        of AbstractDataset is defined.


        Decorates the `load` and `save` methods provided by the class.
        If `_load` or `_save` are defined, alias them as a prerequisite.

        """

        # Save the original __init__ method of the subclass
        init_func: Callable = cls.__init__

        def init_decorator(previous_init: Callable) -> Callable:
            """A decorator that wraps the original __init__ of the subclass.

            It ensures that after the original __init__ executes, the `__post_init__`
            method of the instance is called with the arguments used to initialize
            the object.
            """

            def new_init(self, *args, **kwargs) -> None:  # type: ignore[no-untyped-def]
                """The decorated __init__ method.

                Executes the original __init__, then calls __post_init__ with the arguments
                used to initialize the instance.
                """

                # Call the original __init__ method
                previous_init(self, *args, **kwargs)
                if type(self) is cls:
                    # Capture and process the arguments passed to the original __init__
                    call_args = getcallargs(init_func, self, *args, **kwargs)
                    # Call the custom post-initialization method to save captured arguments
                    self.__post_init__(call_args)

            return new_init

        # Replace the subclass's __init__ with the decorated version

Review comment (suggestion):
I believe you can drop the padding of an extra function, this decorator will not be used in the code as a decorator, but rather you will just assign the function it returns to be the `cls.__init__`, so only the `new_init` function is required.

Code after:
        Decorates the `load` and `save` methods provided by the class.
        If `_load` or `_save` are defined, alias them as a prerequisite.
        """

        # Save the original __init__ method of the subclass
        init_func: Callable = cls.__init__

        @wraps(init_func)
        def new_init(self, *args, **kwargs) -> None:  # type: ignore[no-untyped-def]
            """Executes the original __init__, then save the arguments used
            to initialize the instance.
            """
            # Call the original __init__ method
            init_func(self, *args, **kwargs)
            # Capture and save the arguments passed to the original __init__
            self._init_args = getcallargs(init_func, self, *args, **kwargs)

        # Replace the subclass's __init__ with the new_init
        # A hook for subclasses to capture initialization arguments and save them
        # in the AbstractDataset._init_args field
        cls.__init__ = new_init  # type: ignore[method-assign]

        super().__init_subclass__(**kwargs)

        if hasattr(cls, "_load") and not cls._load.__qualname__.startswith("Abstract"):
            cls.load = cls._load  # type: ignore[method-assign]

        if hasattr(cls, "_save") and not cls._save.__qualname__.startswith("Abstract"):
            cls.save = cls._save  # type: ignore[method-assign]

        if hasattr(cls, "load") and not cls.load.__qualname__.startswith("Abstract"):
            cls.load = cls._load_wrapper(  # type: ignore[assignment]
                cls.load
                if not getattr(cls.load, "__loadwrapped__", False)
                else cls.load.__wrapped__  # type: ignore[attr-defined]
            )

        if hasattr(cls, "save") and not cls.save.__qualname__.startswith("Abstract"):
            cls.save = cls._save_wrapper(  # type: ignore[assignment]
                cls.save
                if not getattr(cls.save, "__savewrapped__", False)
                else cls.save.__wrapped__  # type: ignore[attr-defined]
            )

    def _pretty_repr(self, object_description: dict[str, Any]) -> str:
        str_keys = []
        for arg_name, arg_descr in object_description.items():
            if arg_descr is not None:
                descr = pprint.pformat(
                    arg_descr,
                    sort_dicts=False,