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

Code before:
    is_sorted: Optional[bool] = False,
    descending: Optional[bool] = False,
    sort_key: Optional[Literal["name", "id", "config_id", "creation_date", "tags"]] = "name",
) -> List[Scenario]:
    """Retrieve a list of existing scenarios filtered by cycle or tag.

    This function allows you to retrieve a list of scenarios based on optional
    filtering criteria. If both a _cycle_ and a _tag_ are provided, the returned
    list contains scenarios that belong to the specified _cycle_ **and** also
    have the specified _tag_.

    Parameters:
         cycle (Optional[Cycle^]): The optional `Cycle^` to filter scenarios by.
         tag (Optional[str]): The optional tag to filter scenarios by.
         is_sorted (Optional[bool]): The option to sort scenarios. The default sorting key is name.
         descending (Optional[bool]): The option to sort scenarios on the sorting key in descending order.
         sort_key (Optional[Literal["name", "id", "creation_date", "tags"]]): The optiononal sort_key to decide upon
         what key scenarios are sorted. The sorting is in increasing order for dates, in alphabetical order for name
         and id, in lexographical order for tags.

    Returns:
        The list of scenarios filtered by cycle or tag and optionally sorted by name, id, creation_date or tags.
        If no filtering criteria are provided, this method returns all existing scenarios.
        If is_sorted is set to true, but an incorrect or no sort_key is provided, then the scenarios are sorted by name.
        If the attribute sort_key of some scenarios are equivalent, a second criteria is used. 
        In this case, these scenarios are sorted by their unique id's.
    """
    scenario_manager = _ScenarioManagerFactory._build_manager()
    if not cycle and not tag:
        scenarios = scenario_manager._get_all()
    elif cycle and not tag:
        scenarios = scenario_manager._get_all_by_cycle(cycle)
    elif not cycle and tag:
        scenarios = scenario_manager._get_all_by_tag(tag)
    elif cycle and tag:
        cycles_scenarios = scenario_manager._get_all_by_cycle(cycle)
        scenarios = [scenario for scenario in cycles_scenarios if scenario.has_tag(tag)]
    else:
        scenarios = []

    if is_sorted:
        if sort_key in ["name", "config_id", "creation_date", "tags"]:
            if sort_key == "tags":
                scenarios.sort(key=lambda x: (tuple(sorted(x.tags)), x.id), reverse=descending)
            else:
                scenarios.sort(key=lambda x: (getattr(x, sort_key), x.id), reverse=descending)
        elif sort_key == "id":
            scenarios.sort(key=lambda x: x.id, reverse=descending)
        else:
            scenarios.sort(key=lambda x: (x.name, x.id), reverse=descending)


Review comment (bug):
```suggestion
        If no filtering criterion is provided, this method returns all existing scenarios.
        If is_sorted is set to true, the scenarios are sorted by sort_key. If an incorrect
        or no sort_key is provided, the scenarios are sorted by name.
```

Code after:
def get_scenarios(
    cycle: Optional[Cycle] = None,
    tag: Optional[str] = None,
    is_sorted: bool = False,
    descending: bool = False,
    sort_key: Literal["name", "id", "config_id", "creation_date", "tags"] = "name",
) -> List[Scenario]:
    """Retrieve a list of existing scenarios filtered by cycle or tag.

    This function allows you to retrieve a list of scenarios based on optional
    filtering criteria. If both a _cycle_ and a _tag_ are provided, the returned
    list contains scenarios that belong to the specified _cycle_ **and** also
    have the specified _tag_.

    Parameters:
         cycle (Optional[Cycle^]): The optional `Cycle^` to filter scenarios by.
         tag (Optional[str]): The optional tag to filter scenarios by.
         is_sorted (bool): The option to sort scenarios. The default sorting key is name.
         descending (bool): The option to sort scenarios on the sorting key in descending order.
         sort_key (Literal["name", "id", "creation_date", "tags"]): The optiononal sort_key to
             decide upon what key scenarios are sorted. The sorting is in increasing order for
             dates, in alphabetical order for name and id, in lexographical order for tags.

    Returns:
        The list of scenarios filtered by cycle or tag and optionally sorted by name, id, creation_date or tags.
            If no filtering criterion is provided, this method returns all existing scenarios.
            If is_sorted is set to true, the scenarios are sorted by sort_key. The scenarios
            are sorted by name if an incorrect or no sort_key is provided.
    """
    scenario_manager = _ScenarioManagerFactory._build_manager()
    if not cycle and not tag:
        scenarios = scenario_manager._get_all()
    elif cycle and not tag:
        scenarios = scenario_manager._get_all_by_cycle(cycle)
    elif not cycle and tag:
        scenarios = scenario_manager._get_all_by_tag(tag)
    elif cycle and tag:
        cycles_scenarios = scenario_manager._get_all_by_cycle(cycle)
        scenarios = [scenario for scenario in cycles_scenarios if scenario.has_tag(tag)]
    else:
        scenarios = []

    if is_sorted:
        scenario_manager._sort_scenarios(scenarios, descending, sort_key)
    return scenarios


def get_primary(cycle: Cycle) -> Optional[Scenario]:
    """Retrieve the primary scenario associated with a cycle.

    Parameters: