flyteorg / flyte

Scalable and flexible workflow orchestration platform that seamlessly unifies data, ML and analytics stacks.
https://flyte.org
Apache License 2.0
5.4k stars 580 forks source link

Postponed evaluation `from __future__ import annotations` failed with DataClassTransformer #4004

Open mickjermsurawong-stripe opened 1 year ago

mickjermsurawong-stripe commented 1 year ago

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.

welcome[bot] commented 1 year ago

Thank you for opening your first issue here! 🛠

Kota-Karthik commented 11 months ago

@samhita-alla I am interested in doing this issue So kindly assign me this issue Thank You : )

samhita-alla commented 11 months ago

@Kota-Karthik, are you working on creating a PR for this issue?

github-actions[bot] commented 1 month ago

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! 🙏