Skip to content

gitea.cli.utils.convert

convert

Utility functions for converting data types.

Functions:

gitea.cli.utils.convert.list_str_to_list_int

list_str_to_list_int(
    values: list[str],
) -> list[int] | list[str]

Convert a list of strings to a list of integers if possible.

Parameters:

Name Type Description Default
values list[str]

A list of strings.

required

Returns:

Type Description
list[int] | list[str]

A list of integers if all strings can be converted to integers, otherwise the original list of strings.

Source code in src/gitea/cli/utils/convert.py
def list_str_to_list_int(values: list[str]) -> list[int] | list[str]:
    """Convert a list of strings to a list of integers if possible.

    Args:
        values: A list of strings.

    Returns:
        A list of integers if all strings can be converted to integers, otherwise the original list of strings.

    """
    try:
        return [int(value) for value in values]
    except ValueError:
        return values

gitea.cli.utils.convert.list_str_to_list_int_or_none

list_str_to_list_int_or_none(
    values: list[str] | None,
) -> list[int] | list[str] | None

Convert a list of strings to a list of integers if possible, or return None.

Parameters:

Name Type Description Default
values list[str] | None

A list of strings or None.

required

Returns:

Type Description
list[int] | list[str] | None

A list of integers if all strings can be converted to integers, otherwise the original list of strings, or None if the input is None.

Source code in src/gitea/cli/utils/convert.py
def list_str_to_list_int_or_none(values: list[str] | None) -> list[int] | list[str] | None:
    """Convert a list of strings to a list of integers if possible, or return None.

    Args:
        values: A list of strings or None.

    Returns:
        A list of integers if all strings can be converted to integers, otherwise the original list of strings, or None if the input is None.

    """
    return None if values is None else list_str_to_list_int(values)