Skip to main content

TypeEngine

Core Extensible TypeEngine of Flytekit. This should be used to extend the capabilities of FlyteKits type system. Users can implement their own TypeTransformers and register them with the TypeEngine. This will allow special handling of user objects

Attributes

AttributeTypeDescription
lazy_import_lockthreading.LockA threading.Lock instance used to prevent race conditions during the lazy loading of transformers.

Methods


register()

@classmethod
def register(
transformer: [TypeTransformer](typetransformer.md?sid=flyte_types__type_engine_typetransformer),
additional_types: Optional[typing.List[Type]] = None
)

This should be used for all types that respond with the right type annotation when you use type(...) function

Parameters

NameTypeDescription
transformer[TypeTransformer](typetransformer.md?sid=flyte_types__type_engine_typetransformer)The TypeTransformer instance to register for handling specific Python types.
additional_typesOptional[typing.List[Type]] = NoneAn optional list of additional Python types that this transformer should also handle.

register_restricted_type()

@classmethod
def register_restricted_type(
name: str,
type: Type[T]
)

Registers a type as restricted, meaning it cannot be directly used in certain contexts, and associates it with a RestrictedTypeTransformer.

Parameters

NameTypeDescription
namestrThe name to associate with the restricted type.
typeType[T]The Python type to register as restricted.

register_additional_type()

@classmethod
def register_additional_type(
transformer: [TypeTransformer](typetransformer.md?sid=flyte_types__type_engine_typetransformer)[T],
additional_type: Type[T],
override: bool = False
)

Registers an additional Python type to be handled by an existing TypeTransformer, optionally overriding a previous registration.

Parameters

NameTypeDescription
transformer[TypeTransformer](typetransformer.md?sid=flyte_types__type_engine_typetransformer)[T]The TypeTransformer instance that will handle the additional type.
additional_typeType[T]The Python type to associate with the provided transformer.
overridebool = FalseA boolean indicating whether to override an existing transformer registration for the additional_type.

get_transformer()

@classmethod
def get_transformer(
python_type: Type
) - > [TypeTransformer](typetransformer.md?sid=flyte_types__type_engine_typetransformer)

Implements a recursive search for the transformer.

Parameters

NameTypeDescription
python_typeTypeThe Python type for which to retrieve a transformer.

Returns

TypeDescription
[TypeTransformer](typetransformer.md?sid=flyte_types__type_engine_typetransformer)The TypeTransformer instance capable of handling the python_type.

lazy_import_transformers()

@classmethod
def lazy_import_transformers()

Loads transformers from plugins and other sources only when they are needed, preventing unnecessary imports and potential circular dependencies.


to_literal_type()

@classmethod
def to_literal_type(
python_type: Type[T]
) - > LiteralType

Converts a python type into a flyte specific LiteralType

Parameters

NameTypeDescription
python_typeType[T]The Python type to convert.

Returns

TypeDescription
LiteralTypeThe Flyte LiteralType representation of the given Python type.

to_literal_checks()

@classmethod
def to_literal_checks(
python_val: typing.Any,
python_type: Type[T],
expected: LiteralType
)

Performs type checks and validations before converting a Python value to a Flyte literal, ensuring type compatibility and handling unsupported types like untyped tuples.

Parameters

NameTypeDescription
python_valtyping.AnyThe Python value to check.
python_typeType[T]The expected Python type of the value.
expectedLiteralTypeThe expected Flyte LiteralType.

to_literal()

@classmethod
def to_literal(
python_val: typing.Any,
python_type: Type[T],
expected: types_pb2.LiteralType
) - > literals_pb2.Literal

Converts a Python value of a specified Python type into a Flyte Literal representation, applying type assertions and handling offloaded literals.

Parameters

NameTypeDescription
python_valtyping.AnyThe Python value to convert.
python_typeType[T]The Python type of the value.
expectedtypes_pb2.LiteralTypeThe expected Flyte LiteralType for the conversion.

Returns

TypeDescription
literals_pb2.LiteralThe Flyte Literal representation of the Python value.

unwrap_offloaded_literal()

@classmethod
def unwrap_offloaded_literal(
lv: literals_pb2.Literal
) - > literals_pb2.Literal

Retrieves the actual Literal content from an offloaded literal by downloading it from its URI.

Parameters

NameTypeDescription
lvliterals_pb2.LiteralThe Literal object, potentially containing offloaded metadata.

Returns

TypeDescription
literals_pb2.LiteralThe fully unwrapped Literal object.

to_python_value()

@classmethod
def to_python_value(
lv: Literal,
expected_python_type: Type
) - > typing.Any

Converts a Literal value with an expected python type into a python value.

Parameters

NameTypeDescription
lvLiteralThe Flyte Literal value to convert.
expected_python_typeTypeThe expected Python type for the converted value.

Returns

TypeDescription
typing.AnyThe Python value converted from the Flyte Literal.

to_html()

@classmethod
def to_html(
python_val: typing.Any,
expected_python_type: Type[typing.Any]
) - > str

Converts a Python value to an HTML string representation, utilizing the appropriate transformer or a custom renderer if available.

Parameters

NameTypeDescription
python_valtyping.AnyThe Python value to convert to HTML.
expected_python_typeType[typing.Any]The expected Python type of the value, which may include rendering annotations.

Returns

TypeDescription
strAn HTML string representation of the Python value.

named_tuple_to_variable_map()

@classmethod
def named_tuple_to_variable_map(
t: typing.NamedTuple
) - > interface_pb2.VariableMap

Converts a python-native NamedTuple to a flyte-specific VariableMap of named literals.

Parameters

NameTypeDescription
ttyping.NamedTupleThe Python NamedTuple to convert.

Returns

TypeDescription
interface_pb2.VariableMapA Flyte VariableMap representing the structure and types of the NamedTuple.

literal_map_to_kwargs()

@classmethod
def literal_map_to_kwargs(
lm: LiteralMap,
python_types: typing.Optional[typing.Dict[str, type]] = None,
literal_types: typing.Optional[typing.Dict[str, interface_pb2.Variable]] = None
) - > typing.Dict[str, typing.Any]

Given a LiteralMap (usually an input into a task - intermediate), convert to kwargs for the task

Parameters

NameTypeDescription
lmLiteralMapThe Flyte LiteralMap containing the input values.
python_typestyping.Optional[typing.Dict[str, type]] = NoneAn optional dictionary mapping input names to their expected Python types.
literal_typestyping.Optional[typing.Dict[str, interface_pb2.Variable]] = NoneAn optional dictionary mapping input names to their Flyte Variable definitions, used to infer Python types if python_types is not provided.

Returns

TypeDescription
typing.Dict[str, typing.Any]A dictionary of keyword arguments, where keys are string names and values are converted Python objects.

dict_to_literal_map()

@classmethod
def dict_to_literal_map(
d: typing.Dict[str, typing.Any],
type_hints: Optional[typing.Dict[str, type]] = None
) - > LiteralMap

Given a dictionary mapping string keys to python values and a dictionary containing guessed types for such string keys, convert to a LiteralMap.

Parameters

NameTypeDescription
dtyping.Dict[str, typing.Any]The input dictionary with string keys and Python values.
type_hintsOptional[typing.Dict[str, type]] = NoneAn optional dictionary providing explicit Python type hints for the values in d, which take precedence over inferred types.

Returns

TypeDescription
LiteralMapA Flyte LiteralMap representing the input dictionary, with values converted to Flyte Literal objects.

get_available_transformers()

@classmethod
def get_available_transformers() - > typing.KeysView[Type]

Returns all python types for which transformers are available

Returns

TypeDescription
typing.KeysView[Type]A view of the Python types for which transformers are currently registered.

guess_python_types()

@classmethod
def guess_python_types(
flyte_variable_list: typing.List[interface_pb2.VariableEntry]
) - > typing.Dict[str, Type[Any]]

Transforms a list of flyte-specific VariableEntry objects to a dictionary of regular python values.

Parameters

NameTypeDescription
flyte_variable_listtyping.List[interface_pb2.VariableEntry]A list of Flyte VariableEntry objects, each containing a variable name and its LiteralType.

Returns

TypeDescription
typing.Dict[str, Type[Any]]A dictionary mapping variable names to their guessed Python types.

guess_python_type()

@classmethod
def guess_python_type(
flyte_type: LiteralType
) - > Type[T]

Transforms a flyte-specific LiteralType to a regular python value.

Parameters

NameTypeDescription
flyte_typeLiteralTypeThe Flyte LiteralType for which to guess the corresponding Python type.

Returns

TypeDescription
Type[T]The guessed Python type corresponding to the given Flyte LiteralType.