Is there a quick way of determining the `LiteralTy...
# ask-the-community
x
Is there a quick way of determining the
LiteralType
of a
Literal
?
k
you could get literal type from interface inputs/outputs
determining the
LiteralType
of a
Literal
?
could you tell me about your use cases
x
Thanks! Sure, the use case is that we want to resolve a Promise (from
@dynamic
) locally and need to cast the Literal type into python type using
to_python_type
, but it requires
LiteralType
. Sample code
Copy 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
k
I see. think you could use
<http://TypeEngine.to|TypeEngine.to>_literal_type(TypeEngine.guess_python_type(result.val))
to get literal type
x
Wouldn’t
result.val
be
Literal
? but
guess_python_type
takes
LiteralType
code
Copy code
def guess_python_type(cls, flyte_type: LiteralType) -> type:
k
sorry. my bad. looking for another way to convert it
x
no worries. thanks for the help
k
how about
flyte_obj.interface.outputs["o0"].type
o0
means first output
x
Looks promising. I’ll try it tomorrow. Thanks