<#4004 Postponed evaluation `from __future__ impor...
# flyte-github
a
#4004 Postponed evaluation `from __future__ import annotations` failed with DataClassTransformer Issue created by mickjermsurawong-stripe Problem: Task taking input of
dataclasses
object with
List[T]
will fail deserialization when the object definition is in postponed evaluation mode. The error is
ValueError: Only generic univariate typing.List[T] type is supported.
failing code Repro We can reproduce the error in a simpler setup similar to the existing type engine test
Copy code
# Postponed evaluation
from __future__ import annotations

# Class definition
from dataclasses import dataclass
from dataclasses_json import dataclass_json
from typing import List

@dataclass_json
@dataclass
class MyDataClass:
    foo_list: List[str]

# Serialization similar to tests 
from flytekit.core.context_manager import FlyteContext
from flytekit.core.type_engine import DataclassTransformer

ctx = FlyteContext.current_context()
tf = DataclassTransformer()
lt = tf.get_literal_type(MyDataClass)
o = MyDataClass(["a", "b"])
lv = tf.to_literal(ctx, o, MyDataClass, lt)
ot = tf.to_python_value(ctx, lv=lv, expected_python_type=MyDataClass)
Cause: When postponed evaluation is enabled, the type from dataclass fields is a raw string
"List[str]"
, and not
typing
. The current code trying to get subtype
T
in
List[T]
thus fails.
Copy code
import dataclasses
f = dataclasses.fields(MyDataClass)[0]
f.type, type(f.type) # ('List[str]', str)
If
from __future__ import annotations
is removed before the dataclass definition, deserialization
to_python_value
above works without error. This is because the field type is of
typing
as expected by the code.
Copy code
import dataclasses
f = dataclasses.fields(MyDataClass)[0]
f.type, type(f.type) # (typing.List[str], typing._GenericAlias)
Proposal: either make documentation clearer, or handle the type mapping of raw string in this postponed evaluation mode. flyteorg/flyte