Postponed evaluation `from __future__ import annotations` failed with DataClassTransformer
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
# 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.
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.
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.
Thank you for opening your first issue here! 🛠
@samhita-alla I am interested in doing this issue So kindly assign me this issue Thank You : )
@Kota-Karthik, are you working on creating a PR for this issue?
Hello 👋, this issue has been inactive for over 9 months. To help maintain a clean and focused backlog, we'll be marking this issue as stale and will engage on it to decide if it is still applicable. Thank you for your contribution and understanding! 🙏
"Hello 👋, this issue has been inactive for over 90 days. To help maintain a clean and focused backlog, we'll be marking this issue as stale and will close the issue if we detect no activity in the next 7 days. Thank you for your contribution and understanding! 🙏"