-
Notifications
You must be signed in to change notification settings - Fork 333
Fixes to dataclass transformer union handling #3248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
import sys | ||
import textwrap | ||
import threading | ||
import types | ||
import typing | ||
from abc import ABC, abstractmethod | ||
from collections import OrderedDict | ||
|
@@ -530,10 +531,7 @@ def assert_type(self, expected_type: Type[DataClassJsonMixin], v: T): | |
# However, FooSchema is created by flytekit and it's not equal to the user-defined dataclass (Foo). | ||
# Therefore, we should iterate all attributes in the dataclass and check the type of value in dataclass matches the expected_type. | ||
|
||
expected_fields_dict = {} | ||
|
||
for f in dataclasses.fields(expected_type): | ||
expected_fields_dict[f.name] = f.type | ||
expected_fields_dict = typing.get_type_hints(expected_type) | ||
|
||
if isinstance(v, dict): | ||
original_dict = v | ||
|
@@ -568,7 +566,13 @@ def assert_type(self, expected_type: Type[DataClassJsonMixin], v: T): | |
original_type = type(v) | ||
if UnionTransformer.is_optional_type(expected_type): | ||
expected_type = UnionTransformer.get_sub_type_in_optional(expected_type) | ||
if original_type != expected_type: | ||
|
||
if UnionTransformer.is_union(expected_type) and UnionTransformer.in_union( | ||
original_type, expected_type | ||
): | ||
pass | ||
|
||
elif original_type != expected_type: | ||
raise TypeTransformerFailedError( | ||
f"Type of Val '{original_type}' is not an instance of {expected_type}" | ||
) | ||
|
@@ -1839,6 +1843,14 @@ class UnionTransformer(AsyncTypeTransformer[T]): | |
def __init__(self): | ||
super().__init__("Typed Union", typing.Union) | ||
|
||
@staticmethod | ||
def is_union(t: Type[Any] | types.UnionType) -> bool: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you are maintaining strict type checking, unfortunately no. As you need to check for I noticed a couple other problems that were due to type mismatches so I'll defer to your preferences here as I don't want to overhaul things. |
||
return _is_union_type(t) | ||
|
||
@staticmethod | ||
def in_union(t: Type[Any], union: types.UnionType) -> bool: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use |
||
return t in typing.get_args(union) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use |
||
|
||
@staticmethod | ||
def is_optional_type(t: Type) -> bool: | ||
return _is_union_type(t) and type(None) in get_args(t) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do something like this instead to make code structure better?