Skip to content

Commit

Permalink
Fix Get Literal Type Error for Attribute Access Compile in Flyteprope…
Browse files Browse the repository at this point in the history
…ller (#2749)

Signed-off-by: Future-Outlier <eric901201@gmail.com>
  • Loading branch information
Future-Outlier committed Sep 12, 2024
1 parent 7f54171 commit c06ef30
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion flytekit/core/type_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,13 @@ def get_literal_type(self, t: Type[T]) -> LiteralType:
# Recursively construct the dataclass_type which contains the literal type of each field
literal_type = {}

hints = typing.get_type_hints(t)
# Get the type of each field from dataclass
for field in t.__dataclass_fields__.values(): # type: ignore
try:
literal_type[field.name] = TypeEngine.to_literal_type(field.type)
name = field.name
python_type = hints.get(name, field.type)
literal_type[name] = TypeEngine.to_literal_type(python_type)
except Exception as e:
logger.warning(
"Field {} of type {} cannot be converted to a literal type. Error: {}".format(
Expand Down
16 changes: 16 additions & 0 deletions tests/flytekit/unit/core/test_type_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2860,9 +2860,25 @@ class MyDataClass:
assert literal_type is not None

invalid_json_str = "{ unbalanced_braces"

with pytest.raises(Exception):
Literal(scalar=Scalar(generic=_json_format.Parse(invalid_json_str, _struct.Struct())))

@dataclass
class Fruit(DataClassJSONMixin):
name: str

@dataclass
class NestedFruit(DataClassJSONMixin):
sub_fruit: Fruit
name: str

literal_type = de.get_literal_type(NestedFruit)
dataclass_type = literal_type.structure.dataclass_type
assert dataclass_type["sub_fruit"].simple == SimpleType.STRUCT
assert dataclass_type["sub_fruit"].structure.dataclass_type["name"].simple == SimpleType.STRING
assert dataclass_type["name"].simple == SimpleType.STRING


def test_DataclassTransformer_to_literal():
@dataclass
Expand Down

0 comments on commit c06ef30

Please sign in to comment.