{%- block union %}
    def set_{{name}}(self, value: Union[{{', '.join(get_union_types(field).split(','))}}]) -> None:
{%- set union_type_0 = get_union_types(field).split(',')[0] -%}
{%- if union_type_0 == 'None' %}
        if isinstance(value, type({{union_type_0}})):
            self.{{name}} = None
{%- elif (union_type_0 in primitive_type_map.values()) %}
        if isinstance(value, {{union_type_0}}):
            self.{{name}} = {{union_type_0}}(value)
{%- elif ((union_type_0 not in primitive_type_map.values()) and (union_type_0 != 'list')) %}
        if isinstance(value, (dict, {{union_type_0}})):
            self.{{name}} = {{union_type_0}}(value)
{%- endif %}
{#  #}
{%- set idx = 1 -%}
{% for i, typ in enumerate(get_union_types(field).split(',')[1:]) %}
{%- if typ == 'None' %}
        elif isinstance(value, type({{typ}})):
            self.{{name}} = None
{%- elif (typ in primitive_type_map.values()) %}
        elif isinstance(value, {{typ}}):
            self.{{name}} = {{typ}}(value)
{%- elif ((typ not in primitive_type_map.values()) and (typ != 'list')) %}
        elif isinstance(value, (dict, {{typ}})):
            self.{{name}} = {{typ}}(value)
{%- elif ((typ not in primitive_type_map.values()) and (typ == 'list')) %}
        elif isinstance(value, list):
            self.{{name}} = []
            for element in value:
                {#- handle tons of types which sucks... -#}
                {%- if field.union_types[idx + i].array_item_type.fieldtype == 'reference' %}
                self.{{name}}.append({{field.union_types[idx + i].array_item_type.reference_name}}(element))
                {%- elif field.union_types[idx + i].array_item_type.fieldtype == 'primitive'%}
                if isinstance(element, {{primitive_type_map[field.union_types[idx + i].array_item_type.avrotype]}}):
                    self.{{name}}.append(element)
                else:
                    raise TypeError(
                        "Type for '{{name}}' should be '{{primitive_type_map[field.union_types[idx + i].array_item_type.fieldtype]}}'"
                    )
                {%- endif %}
{%- endif %}
{%- endfor %}
        else:
            raise TypeError("field '{{name}}' should be in ({{', '.join(get_union_types(field).split(','))}})")

    def get_{{name}}(self) -> Union[{{', '.join(get_union_types(field).split(','))}}]:
        return self.{{name}}
{%- endblock -%}
