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

Code before:

            if isinstance(obj, dict):
                sorted_dict = sorted(obj.items(), key=lambda pair: str(pair[0]))  # 2

                text = ", ".join(
                    fmt.format(key, _to_str(value))  # 2
                    for key, value in sorted_dict
                    if value is not None  # 3
                )

                return text if is_root else "{" + text + "}"  # 1

            # not a dictionary
            return str(obj)

        return f"{type(self).__name__}({_to_str(self._describe(), True)})"

    @classmethod
    def _load_wrapper(cls, load_func: Callable[[Self], _DO]) -> Callable[[Self], _DO]:
        @wraps(load_func)
        def load(self: Self) -> _DO:
            self._logger.debug("Loading %s", str(self))

            try:
                return load_func(self)
            except DatasetError:
                raise
            except Exception as exc:
                # This exception handling is by design as the composed data sets
                # can throw any type of exception.
                message = (
                    f"Failed while loading data from data set {str(self)}.\n{str(exc)}"
                )
                raise DatasetError(message) from exc

        load.__annotations__["return"] = load_func.__annotations__.get("return")
        load.__loadwrapped__ = True  # type: ignore[attr-defined]
        return load

    @classmethod
    def _save_wrapper(
        cls, save_func: Callable[[Self, _DI], None]
    ) -> Callable[[Self, _DI], None]:
        @wraps(save_func)
        def save(self: Self, data: _DI) -> None:
            if data is None:
                raise DatasetError("Saving 'None' to a 'Dataset' is not allowed")

            try:
                self._logger.debug("Saving %s", str(self))
                save_func(self, data)

Review comment (refactor):
Why is the type here `Self` and not `AbstractDataset`? 

Code after:
            """

            fmt = "{}={}" if is_root else "'{}': {}"  # 1

            if isinstance(obj, dict):
                sorted_dict = sorted(obj.items(), key=lambda pair: str(pair[0]))  # 2

                text = ", ".join(
                    fmt.format(key, _to_str(value))  # 2
                    for key, value in sorted_dict
                    if value is not None  # 3
                )

                return text if is_root else "{" + text + "}"  # 1

            # not a dictionary
            return str(obj)

        return f"{type(self).__name__}({_to_str(self._describe(), True)})"

    @classmethod
    def _load_wrapper(cls, load_func: Callable[[Self], _DO]) -> Callable[[Self], _DO]:
        """Decorate `load_func` with logging and error handling code."""

        @wraps(load_func)
        def load(self: Self) -> _DO:
            self._logger.debug("Loading %s", str(self))

            try:
                return load_func(self)
            except DatasetError:
                raise
            except Exception as exc:
                # This exception handling is by design as the composed data sets
                # can throw any type of exception.
                message = (
                    f"Failed while loading data from data set {str(self)}.\n{str(exc)}"
                )
                raise DatasetError(message) from exc

        load.__annotations__["return"] = load_func.__annotations__.get("return")
        load.__loadwrapped__ = True  # type: ignore[attr-defined]
        return load

    @classmethod
    def _save_wrapper(
        cls, save_func: Callable[[Self, _DI], None]
    ) -> Callable[[Self, _DI], None]:
        """Decorate `save_func` with logging and error handling code."""

        @wraps(save_func)