.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto/core/type_system/enums.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_core_type_system_enums.py: Using Enum types ------------------------- Sometimes you may want to restrict the set of inputs / outputs to a finite set of acceptable values. This is commonly achieved using Enum types in programming languages. Since version 0.15.0, Flyte supports Enum Types with string values. You can create a python Enum type and pass it to a task or return from a task. Flyte will automatically convert this to and limit the inputs etc to a finite set of values. UX: flytectl will allow only the finite set of values to be acceptable and (in progress) UI will provide a drop-down for the values. **Caveat:** Only string values are supported as valid enum values. The first value in the list is assumed as the default and the Enum types are not optional. So when defining enums, design them well to always make the first value as a valid default. .. GENERATED FROM PYTHON SOURCE LINES 19-25 .. code-block:: default from enum import Enum from typing import Tuple from flytekit import task, workflow .. GENERATED FROM PYTHON SOURCE LINES 26-33 Enums are natively supported in flyte's type system. Enum values can only be of type string. At runtime they are represented using their string values. .. note:: ENUM Values can only be string. Other languages will receive enum as a string. .. GENERATED FROM PYTHON SOURCE LINES 33-39 .. code-block:: default class Color(Enum): RED = "red" GREEN = "green" BLUE = "blue" .. GENERATED FROM PYTHON SOURCE LINES 40-41 Enums can be used as a regular type .. GENERATED FROM PYTHON SOURCE LINES 41-46 .. code-block:: default @task def enum_stringify(c: Color) -> str: return c.value .. GENERATED FROM PYTHON SOURCE LINES 47-48 Their values can be accepted as string .. GENERATED FROM PYTHON SOURCE LINES 48-61 .. code-block:: default @task def string_to_enum(c: str) -> Color: return Color(c) @workflow def enum_wf(c: Color = Color.RED) -> Tuple[Color, str]: v = enum_stringify(c=c) return string_to_enum(c=v), v if __name__ == "__main__": print(enum_wf()) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_auto_core_type_system_enums.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: enums.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: enums.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_