# Buggy code
def union(name: str, types: typing.Tuple[typing.Type], *, description=None):
    """Creates a new named Union type.

    Example usages:

    >>> strawberry.union(
    >>>     "Name",
    >>>     (A, B),
    >>> )

    >>> strawberry.union(
    >>>     "Name",
    >>>     (A, B),
    >>> )
    """

    from .type_converter import get_graphql_type_for_annotation

    def _resolve_type(root, info, _type):
        if not hasattr(root, "graphql_type"):
            raise WrongReturnTypeForUnion(info.field_name, str(type(root)))

        if is_generic(type(root)):
            return _find_type_for_generic_union(root)

        if root.graphql_type not in _type.types:
            raise UnallowedReturnTypeForUnion(
                info.field_name, str(type(root)), _type.types
            )

        return root.graphql_type

    # TODO: union types don't work with scalar types
    # so we want to return a nice error
    # also we want to make sure we have been passed
    # strawberry types
    graphql_type = GraphQLUnionType(
        name,
        [
            get_graphql_type_for_annotation(type, name, force_optional=True)
            for type in types
        ],
        description=description,
    )
    graphql_type.resolve_type = _resolve_type

    # This is currently a temporary solution, this is ok for now
    # But in future we might want to change this so that it works
    # properly with mypy, but there's no way to return a type like NewType does
    # so we return this class instance as it allows us to reuse the rest of
    # our code without doing too many changes

    class X:
        def __init__(self, graphql_type):
            self.graphql_type = graphql_type

        def __call__(self):
            raise ValueError("Cannot use union type directly")

    return X(graphql_type)


# Traceback (AttributeError)
Traceback (most recent call last):
File "/home/ignormies/.local/share/virtualenvs/gql-bf-XGX4szKA-py3.8/lib/python3.8/site-packages/graphql/type/definition.py", line 735, in fields
fields = resolve_thunk(self._fields)
File "/home/ignormies/.local/share/virtualenvs/gql-bf-XGX4szKA-py3.8/lib/python3.8/site-packages/graphql/type/definition.py", line 293, in resolve_thunk
return thunk() if callable(thunk) else thunk
File "/home/ignormies/.local/share/virtualenvs/gql-bf-XGX4szKA-py3.8/lib/python3.8/site-packages/strawberry/type.py", line 133, in <lambda>
lambda types_replacement_map=None: _get_fields(wrapped, types_replacement_map),
File "/home/ignormies/.local/share/virtualenvs/gql-bf-XGX4szKA-py3.8/lib/python3.8/site-packages/strawberry/type.py", line 76, in _get_fields
fields[field_name] = field(
File "/home/ignormies/.local/share/virtualenvs/gql-bf-XGX4szKA-py3.8/lib/python3.8/site-packages/strawberry/field.py", line 315, in field
return field(wrap)
File "/home/ignormies/.local/share/virtualenvs/gql-bf-XGX4szKA-py3.8/lib/python3.8/site-packages/strawberry/field.py", line 171, in __call__
setattr(wrap, IS_STRAWBERRY_FIELD, True)
AttributeError: 'method' object has no attribute '_is_strawberry_field'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/ignormies/.config/JetBrains/PyCharm2020.1/scratches/scratch_7.py", line 30, in <module>
schema = strawberry.Schema(query=Query)
File "/home/ignormies/.local/share/virtualenvs/gql-bf-XGX4szKA-py3.8/lib/python3.8/site-packages/strawberry/schema.py", line 25, in __init__
super().__init__(
File "/home/ignormies/.local/share/virtualenvs/gql-bf-XGX4szKA-py3.8/lib/python3.8/site-packages/graphql/type/schema.py", line 205, in __init__
collect_referenced_types(query)
File "/home/ignormies/.local/share/virtualenvs/gql-bf-XGX4szKA-py3.8/lib/python3.8/site-packages/graphql/type/schema.py", line 382, in collect_referenced_types
for field in named_type.fields.values():
File "/usr/lib/python3.8/functools.py", line 967, in __get__
val = self.func(instance)
File "/home/ignormies/.local/share/virtualenvs/gql-bf-XGX4szKA-py3.8/lib/python3.8/site-packages/graphql/type/definition.py", line 737, in fields
raise TypeError(f"{self.name} fields cannot be resolved. {error}")
TypeError: Query fields cannot be resolved. 'method' object has no attribute '_is_strawberry_field'

Process finished with exit code 1

# Fixed code
def union(name: str, types: Tuple[Type], *, description=None):
    """Creates a new named Union type.

    Example usages:

    >>> strawberry.union(
    >>>     "Name",
    >>>     (A, B),
    >>> )

    >>> strawberry.union(
    >>>     "Name",
    >>>     (A, B),
    >>> )
    """

    union_definition = UnionDefinition(name=name, description=description, types=types)

    # This is currently a temporary solution, this is ok for now
    # But in future we might want to change this so that it works
    # properly with mypy, but there's no way to return a type like NewType does
    # so we return this class instance as it allows us to reuse the rest of
    # our code without doing too many changes

    def _call(self):
        raise ValueError("Cannot use union type directly")

    union_class = type(
        name,
        (),
        {"_union_definition": union_definition, "__call__": _call},
    )

    return union_class()