Xinzhou Liu
05/01/2023, 11:48 PMLiteralType
of a Literal
?Kevin Su
05/02/2023, 12:19 AMdetermining thecould you tell me about your use casesof aLiteralType
?Literal
Xinzhou Liu
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]
)
type(lv)
does not return LiteralType
of lv
Kevin Su
05/02/2023, 6:01 AM<http://TypeEngine.to|TypeEngine.to>_literal_type(TypeEngine.guess_python_type(result.val))
to get literal typeXinzhou Liu
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:
Kevin Su
05/02/2023, 6:09 AMXinzhou Liu
05/02/2023, 6:10 AMKevin Su
05/02/2023, 6:28 AMflyte_obj.interface.outputs["o0"].type
o0
means first outputXinzhou Liu
05/02/2023, 6:29 AM