Repository: Avaiga/taipy
File: taipy/core/taipy.py (Python)

Code before:


def get_cycles() -> List[Cycle]:
    """Return the list of all existing cycles.

    Returns:
        The list of all cycles.
    """
    return _CycleManagerFactory._build_manager()._get_all()


def can_create(config: Union[ScenarioConfig, DataNodeConfig]) -> Reasons:
    """Indicate if a config can be created.

    This function checks if the given scenario or data node config can be created.

    Returns:
        True if the given config can be created. False otherwise.
    """
    if isinstance(config, ScenarioConfig):
        return _ScenarioManagerFactory._build_manager()._can_create(config)
    elif isinstance(config, DataNodeConfig):
        return _DataManagerFactory._build_manager()._can_create(config)
    else:
        config_id = getattr(config, "id", None) or str(config)
        return Reasons(config_id)._add_reason(config_id, _build_config_can_not_create_reason(config_id))


def create_scenario(
    config: ScenarioConfig,
    creation_date: Optional[datetime] = None,
    name: Optional[str] = None,
) -> Scenario:
    """Create and return a new scenario based on a scenario configuration.

    This function checks and locks the configuration, manages application's version,
    and creates a new scenario from the scenario configuration provided.

    If the scenario belongs to a cycle, the cycle (corresponding to the _creation_date_
    and the configuration frequency attribute) is created if it does not exist yet.

    Parameters:
        config (ScenarioConfig^): The scenario configuration used to create a new scenario.
        creation_date (Optional[datetime.datetime]): The creation date of the scenario.
            If None, the current date time is used.
        name (Optional[str]): The displayable name of the scenario.

    Returns:
        The new scenario.

    Raises:

Review comment (bug):
If it is not a `ScenarioConfig` or a `DataNodeConfig`, the reason to return is that `config` has an incorrect type. There is no need to look for a `config_id`. No?

```suggestion
        return Reasons(str(config))._add_reason(str(config), _build_config_can_not_create_reason(config_id))
```

What do you think?

Code after:


def get_cycles() -> List[Cycle]:
    """Return the list of all existing cycles.

    Returns:
        The list of all cycles.
    """
    return _CycleManagerFactory._build_manager()._get_all()


def can_create(config: Optional[Union[ScenarioConfig, DataNodeConfig]] = None) -> Reasons:
    """Indicate if a config can be created. The config should be a scenario or data node config.

    If no config is provided, the function indicates if any scenario or data node config can be created.

    Returns:
        True if the given config can be created. False otherwise.
    """
    if isinstance(config, DataNodeConfig):
        return _DataManagerFactory._build_manager()._can_create(config)

    return _ScenarioManagerFactory._build_manager()._can_create(config)


def create_scenario(
    config: ScenarioConfig,
    creation_date: Optional[datetime] = None,
    name: Optional[str] = None,
) -> Scenario:
    """Create and return a new scenario based on a scenario configuration.

    This function checks and locks the configuration, manages application's version,
    and creates a new scenario from the scenario configuration provided.

    If the scenario belongs to a cycle, the cycle (corresponding to the _creation_date_
    and the configuration frequency attribute) is created if it does not exist yet.

    Parameters:
        config (ScenarioConfig^): The scenario configuration used to create a new scenario.
        creation_date (Optional[datetime.datetime]): The creation date of the scenario.
            If None, the current date time is used.
        name (Optional[str]): The displayable name of the scenario.

    Returns:
        The new scenario.

    Raises:
        SystemExit: If the configuration check returns some errors.

    """