thousands-car-79657
05/01/2023, 11:48 PMLiteralType
of a Literal
?glamorous-carpet-83516
05/02/2023, 12:19 AMglamorous-carpet-83516
05/02/2023, 12:20 AMdetermining thecould you tell me about your use casesof aLiteralType
?Literal
thousands-car-79657
05/02/2023, 1:32 AM@dynamic
) locally and need to cast the Literal type into python type using to_python_type
, but it requires LiteralType
. Sample code
def _literal_as_type(lv: Literal, return_type: Optional[type] = None):
"""
Convert a literal to a python value of the given type
"""
if return_type is None:
# infer the python type
return_type = TypeEngine.guess_python_type(type(lv))
return TypeEngine.to_python_value(
FlyteContextManager.current_context(),
lv,
return_type,
)
# usage
if execution_options.local:
# flyte_obj is a dynamic
results = flyte_obj(**inputs)
if isinstance(results, Promise):
if None in execution_options.return_types:
raise ValueError(
"Unable to create python val from literal with "
"None in return_types"
)
results = _literal_as_type(
results.val, execution_options.return_types[0]
)
thousands-car-79657
05/02/2023, 1:34 AMtype(lv)
does not return LiteralType
of lv
glamorous-carpet-83516
05/02/2023, 6:01 AM<http://TypeEngine.to|TypeEngine.to>_literal_type(TypeEngine.guess_python_type(result.val))
to get literal typethousands-car-79657
05/02/2023, 6:06 AMresult.val
be Literal
? but guess_python_type
takes LiteralType
code
def guess_python_type(cls, flyte_type: LiteralType) -> type:
glamorous-carpet-83516
05/02/2023, 6:09 AMthousands-car-79657
05/02/2023, 6:10 AMglamorous-carpet-83516
05/02/2023, 6:28 AMflyte_obj.interface.outputs["o0"].type
o0
means first outputthousands-car-79657
05/02/2023, 6:29 AM