<#3253 Fix `dataclass` deserialization for fields ...
# flyte-github
c
#3253 Fix
dataclass
deserialization for fields with Optional types
Pull request opened by redartera ## Tracking issue N/A ## Why are the changes needed? Dataclass deserialization was failing when dataclass fields contained Optional types. The
generate_attribute_list_from_dataclass_json_mixin
function did not properly handle the
anyOf
property in JSON schemas, which is used to represent Optional types. This caused deserialization to fail for dataclasses with fields like
Optional[dict]
,
Optional[dict[str, float]]
, etc. ## What changes were proposed in this pull request? 1. Refactored JSON schema property handling: Extracted the property handling logic into a new helper function
_handle_json_schema_property
that properly processes Optional types represented as
anyOf
in JSON schemas. 2. Added proper Optional type support: The function now correctly handles Optional fields by recursively processing the union types and constructing the appropriate Python type annotations. 3. Added validation: Prevents nested Optional types which are not supported for dataclass JSON deserialization (as far as this PR goes) ## How was this patch tested? Added integration test
test_execute_wf_out_dataclass_with_optional
and test workflow
dataclass_with_optional_wf.py
that verifies dataclasses with optional fields can be properly serialized, executed remotely, and deserialized. This integration test would fail without the change made to
generate_attribute_list_from_dataclass_json_mixin
### Screenshots This is how the integration test fails without the proposed fix [Screenshot 2025-05-26 at 6 27 00 PM](https://private-user-images.githubusercontent.com/120470035/447698490-761c594d-4143-4521-9959-813675051305.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NDg2ODUzNDQsIm5iZiI6MTc0ODY4NTA0NCwicGF0aCI6Ii8xMjA0NzAwMzUvNDQ3Njk4NDkwLTc2MWM1OTRkLTQxNDMtNDUyMS05OTU5LTgxMzY3NTA1MTMwNS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwNTMxJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDUzMVQwOTUwNDRaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1hZWQ4NmEzNTUzZDM4N2JkOTJmOTIzNTllODcxNGJmMmRiNTdhZDRlY2FjN2VmODQyNmYyYzNlM2NlMzc4NGZkJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.jmrww39ix0YQ6ix-j3bl0nKtBV4H8ikGW8LirOm8VV8) ## Check all the applicable boxes • I updated the documentation accordingly. • All new and existing tests passed. • All commits are signed-off. https://bito.ai#summarystart| ### Summary by Bito This pull request refines and enhances dataclass deserialization for Optional types, improving the handling of JSON schema properties. It introduces new integration tests to validate proper serialization and deserialization functionality for workflows using these dataclasses. flyteorg/flytekit