Skip to content

omnipy.util.literal_enum_generator

Helpers for generating LiteralEnum source code from value collections.

Use generate_literal_enum_code() to scaffold a LiteralEnum class from a sequence of values or a mapping of names to values.

FUNCTION DESCRIPTION
generate_literal_enum_code

Generate Python source code for a LiteralEnum subclass.

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 Python source code for a LiteralEnum subclass.

PARAMETER DESCRIPTION
values

Sequence of literal values or mapping of attribute names to literal values.

TYPE: ValueType

docstrings

Optional mapping from literal values to attribute docstring paragraphs.

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

include_imports

Whether to include the required Literal and LiteralEnum imports in the generated source.

TYPE: bool DEFAULT: True

class_name

Name of the generated LiteralEnum subclass.

TYPE: str DEFAULT: 'NewLiteralEnum'

RETURNS DESCRIPTION
str

Python source code for the generated LiteralEnum class.

RAISES DESCRIPTION
ValueError

If no values are provided, if class_name is invalid, or if docstrings contains values that are not present in values.

Examples:

A typical call generates a ready-to-paste class definition from a sequence of values or a mapping of names to values.

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 Python source code for a ``LiteralEnum`` subclass.

    Args:
        values: Sequence of literal values or mapping of attribute names to
            literal values.
        docstrings: Optional mapping from literal values to attribute
            docstring paragraphs.
        include_imports: Whether to include the required ``Literal`` and
            ``LiteralEnum`` imports in the generated source.
        class_name: Name of the generated ``LiteralEnum`` subclass.

    Returns:
        Python source code for the generated ``LiteralEnum`` class.

    Raises:
        ValueError: If no values are provided, if ``class_name`` is invalid,
            or if ``docstrings`` contains values that are not present in
            ``values``.

    Examples:
        A typical call generates a ready-to-paste class definition from a
        sequence of values or a mapping of names to values.
    """
    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