Skip to content

omnipy.util.literal_enum_generator

FUNCTION DESCRIPTION
generate_literal_enum_code

Generate code for a LiteralEnum class based on a tuple of string values.

ATTRIBUTE DESCRIPTION
ValueType

ValueType module-attribute

ValueType = (
    Sequence[LiteralEnumInnerTypes]
    | Mapping[str, LiteralEnumInnerTypes]
    | MappingProxyType[str, LiteralEnumInnerTypes]
)

generate_literal_enum_code

generate_literal_enum_code(
    values: ValueType,
    docstrings: Mapping[LiteralEnumInnerTypes, Sequence[str]] | None = None,
    include_imports: bool = True,
    class_name: str = "NewLiteralEnum",
) -> str

Generate code for a LiteralEnum class based on a tuple of string values.

PARAMETER DESCRIPTION
values

Either one of the following: 1. Tuple of objects that define the literal values for the enum, where each value will be converted to a valid attribute name 2. Dict of string keys to values, where the keys are used as attribute names

TYPE: ValueType

docstrings

Optional dictionary mapping values to their corresponding docstrings. If provided, the docstrings will be added to the generated class attributes. The keys should match the values in values. If None, no docstrings are added (default: None)

TYPE: Mapping[LiteralEnumInnerTypes, Sequence[str]] | None DEFAULT: None

include_imports

If True, includes necessary imports for Literal and LiteralEnum (default: True)

TYPE: bool DEFAULT: True

class_name

The name of the generated class (default: 'GeneratedEnum')

TYPE: str DEFAULT: 'NewLiteralEnum'

RETURNS DESCRIPTION
str

A string containing the complete Python code for a LiteralEnum class

RAISES DESCRIPTION
ValueError

If no values are provided or if class_name is not a valid identifier

Example

code = generate_literal_enum_code(('active', 'inactive', 'pending')) print(code) from typing import Literal

from omnipy.util.literal_enum import LiteralEnum

class GeneratedEnum(LiteralEnum[str]): Literals = Literal['active', 'inactive', 'pending'] ACTIVE: Literal['active'] = 'active' INACTIVE: Literal['inactive'] = 'inactive' PENDING: Literal['pending'] = 'pending'

Source code in src/omnipy/util/literal_enum_generator.py
def generate_literal_enum_code(
    values: ValueType,
    docstrings: Mapping[LiteralEnumInnerTypes, Sequence[str]] | None = None,
    include_imports: bool = True,
    class_name: str = 'NewLiteralEnum',
) -> str:
    """
    Generate code for a LiteralEnum class based on a tuple of string values.

    Parameters:
        values: Either one of the following:
            1. Tuple of objects that define the literal values for the enum,
               where each value will be converted to a valid attribute name
            2. Dict of string keys to values, where the keys are used as
               attribute names
        docstrings: Optional dictionary mapping values to their
            corresponding docstrings. If provided, the docstrings will be
            added to the generated class attributes. The keys should match
            the values in `values`. If None, no docstrings are added
            (default: None)
        include_imports: If True, includes necessary imports for Literal and
            LiteralEnum (default: True)
        class_name: The name of the generated class (default:
            'GeneratedEnum')

    Returns:
        A string containing the complete Python code for a LiteralEnum class

    Raises:
        ValueError: If no values are provided or if class_name is not a
            valid identifier

    Example:
        >>> code = generate_literal_enum_code(('active', 'inactive', 'pending'))
        >>> print(code)
        from typing import Literal

        from omnipy.util.literal_enum import LiteralEnum


        class GeneratedEnum(LiteralEnum[str]):
            Literals = Literal['active', 'inactive', 'pending']
            ACTIVE: Literal['active'] = 'active'
            INACTIVE: Literal['inactive'] = 'inactive'
            PENDING: Literal['pending'] = 'pending'
    """
    enum_mappings = _generate_attrib_names(values)
    _check_params(values, docstrings, class_name, enum_mappings)

    lines: list[str] = []

    lines = _build_import_lines(lines, include_imports)
    lines = _build_class_definition_lines(lines, class_name, enum_mappings)
    lines = _build_attribute_definitions(docstrings, enum_mappings, lines)

    code = '\n'.join(lines)
    return code