diff --git a/flytekit/core/type_engine.py b/flytekit/core/type_engine.py index ed52ee8bf5..edb7bea65f 100644 --- a/flytekit/core/type_engine.py +++ b/flytekit/core/type_engine.py @@ -505,6 +505,9 @@ def _fix_val_int(self, t: typing.Type, val: typing.Any) -> typing.Any: if get_origin(t) is typing.Union and type(None) in get_args(t): # Handle optional type. e.g. Optional[int], Optional[dataclass] + # Marshmallow doesn't support union type, so the type here is always an optional type. + # https://github.com/marshmallow-code/marshmallow/issues/1191#issuecomment-480831796 + # Note: Typing.Union[None, int] is also an optional type, but Marshmallow does not support it. return self._fix_val_int(get_args(t)[0], val) if dataclasses.is_dataclass(t): diff --git a/tests/flytekit/unit/core/test_type_engine.py b/tests/flytekit/unit/core/test_type_engine.py index e37b30834b..0fe2513908 100644 --- a/tests/flytekit/unit/core/test_type_engine.py +++ b/tests/flytekit/unit/core/test_type_engine.py @@ -149,7 +149,7 @@ def test_list_of_dataclass_getting_python_value(): @dataclass() class Bar(object): v: typing.Union[int, None] - w: typing.Union[None, str] + w: typing.Optional[str] x: float y: str z: typing.Dict[str, bool]