Repository: Avaiga/taipy
File: taipy/common/config/_serializer/_base_serializer.py (Python)

Code before:
from typing import Any, Dict, Optional

from .._config import _Config
from ..common._template_handler import _TemplateHandler
from ..common._validate_id import _validate_id
from ..exceptions.exceptions import InvalidConfigurationType, LoadingError
from ..global_app.global_app_config import GlobalAppConfig
from ..section import Section
from ..unique_section import UniqueSection


class _BaseSerializer(object):
    """Base serializer class for taipy configuration."""

    _GLOBAL_NODE_NAME = "TAIPY"
    _SERIALIZABLE_TYPES = [
        "bool",
        "str",
        "int",
        "float",
        "datetime",
        "timedelta",
        "function",
        "class",
        "SCOPE",
        "FREQUENCY",
        "SECTION",
    ]
    _section_class = {_GLOBAL_NODE_NAME: GlobalAppConfig}
    _registered_types: Dict[str, type] = {}

    @classmethod
    def _register(cls, section):
        cls._section_class[section.name] = section.__class__
        for clazz in section._types_to_register():
            if not hasattr(clazz, "_type_identifier"):
                raise InvalidConfigurationType(f"Type {clazz.__name__} must have a `_type_identifier` method.")
            if not hasattr(clazz, "_stringify"):
                raise InvalidConfigurationType(f"Type {clazz.__name__} must have a `_stringify` method.")
            if not hasattr(clazz, "_pythonify"):
                raise InvalidConfigurationType(f"Type {clazz.__name__} must have a `_pythonify` method.")
            cls._registered_types[clazz._type_identifier()] = clazz

    @classmethod
    @abstractmethod
    def _write(cls, configuration: _Config, filename: str):
        raise NotImplementedError

    @classmethod
    def _str(cls, configuration: _Config):
        config_as_dict = {cls._GLOBAL_NODE_NAME: configuration._global_config._to_dict()}

Review comment (suggestion):
This must be removed and dynamically added from the core package. For instance, it could be 
declared in a `_SUB_TYPES ` variable implemented in the `Section` class, and updated dynamically by the `_inject_section` function (in `common/config/__init__.py`) when registering the Section.

Code after:
from typing import Any, Dict, Optional

from .._config import _Config
from ..common._template_handler import _TemplateHandler
from ..common._validate_id import _validate_id
from ..exceptions.exceptions import InvalidConfigurationType, LoadingError
from ..global_app.global_app_config import GlobalAppConfig
from ..section import Section
from ..unique_section import UniqueSection


class _BaseSerializer(object):
    """Base serializer class for taipy configuration."""

    _GLOBAL_NODE_NAME = "TAIPY"
    _SERIALIZABLE_TYPES = [
        "bool",
        "str",
        "int",
        "float",
        "datetime",
        "timedelta",
        "function",
        "class",
        "SECTION",
    ]
    _section_class = {_GLOBAL_NODE_NAME: GlobalAppConfig}
    _registered_types: Dict[str, type] = {}

    @classmethod
    def _register(cls, section):
        cls._section_class[section.name] = section.__class__
        for clazz in section._types_to_register():
            if not hasattr(clazz, "_type_identifier"):
                raise InvalidConfigurationType(f"Type {clazz.__name__} must have a `_type_identifier` method.")
            if not hasattr(clazz, "_stringify"):
                raise InvalidConfigurationType(f"Type {clazz.__name__} must have a `_stringify` method.")
            if not hasattr(clazz, "_pythonify"):
                raise InvalidConfigurationType(f"Type {clazz.__name__} must have a `_pythonify` method.")
            cls._registered_types[clazz._type_identifier()] = clazz

            if clazz._type_identifier() not in cls._SERIALIZABLE_TYPES:
                cls._SERIALIZABLE_TYPES.append(clazz._type_identifier())

    @classmethod
    @abstractmethod
    def _write(cls, configuration: _Config, filename: str):
        raise NotImplementedError

    @classmethod
    def _str(cls, configuration: _Config):