https://dagster.io/ logo
#announcements
Title
# announcements
a

Amanda Crawford

08/28/2019, 4:42 PM
Hi all, I trying to create a custom type that uses the Union type so that I can accept a mixed list with two types. I get an error when trying to create a custom dagster type and am not sure if this is the right approach. Any pointers or suggestions?https://docs.python.org/3/library/typing.html#typing.Union
a

alex

08/28/2019, 4:56 PM
you want to share what you are trying here http://collabedit.com/gvtpt (or similar tool if you have a preferred one)
a

Amanda Crawford

08/28/2019, 5:02 PM
Yes, I will send you the link in a few minutes
Sorry, didn't realize that was a direct link but I posted a snippet of what I am trying to achieve
a

alex

08/28/2019, 5:48 PM
ya i think this is just something we don't support yet but probably should
in the mean time some options are: • make a new custom type representing the union. Have it defer to the
type_check
functions of the inner types • use
Any
• use a common ancestor type ie
DataFrame
incase that collabedit link goes down
Copy code
# workaround for creating a custom union type
def custom_union_type(name, types):
    class _CustomUnion(RuntimeType):
        def __init__(self):
            super(_CustomUnion, self).__init__(
                name=name,
                key=name,
            )

        def type_check(self, value):
            failures = []
            for type in types:
                try:
                    type.type_check(value)
                    # passed type check
                    return TypeCheck('Valid for Union {name}. Passed as {tname}'.format(name=name, tname=type.name))
                except Failure as fail:
                    failures.append(fail)

            raise Failure(
                'Value is not a valid type for Union type {name}. Failed all type checks: {fail}'.format(
                    name=self.name
                    fail="\n".join(fail.message)
                )
            )
a

Amanda Crawford

08/28/2019, 6:25 PM
Great thanks! I don't know why I didn't think to do that