strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

`StrawberryAnnotation.create_list` cannot handle empty list type

Open invokermain opened this issue 2 years ago • 0 comments

Describe the Bug

Using a field resolver with return type of list or List (as opposed to say list[int] etc) raising an internal exception:

AttributeError: __args__. Did you mean: '__ror__'?

This is due to the lack of a len check when calling evaled_type.__args__[0] in this function:

class StrawberryAnnotation:

    ... # skipped code

    def create_list(self, evaled_type: Any) -> StrawberryList:
        of_type = StrawberryAnnotation(
            annotation=evaled_type.__args__[0],
            namespace=self.namespace,
        ).resolve()

        return StrawberryList(of_type

I am not sure what the desired behaviour is, maybe either:

  • Treat as list[Any].
  • Raise a more informative exception.

System Information

  • Operating system:
  • Strawberry version (if applicable): 0.111.2

Additional Context

Example Code:

from typing import List

import strawberry


@strawberry.type
class MyItemType:
    my_field: int


def typing_list_resolver() -> List:
    return [MyItemType(2), MyItemType(3)]


def builtin_list_resolver() -> list:
    return [MyItemType(2), MyItemType(3)]


@strawberry.type
class Query:
    my_item: List[MyItemType] = strawberry.field(typing_list_resolver)


@strawberry.type
class Query2:
    my_item: list[MyItemType] = strawberry.field(builtin_list_resolver)


if __name__ == "__main__":

    # raises `AttributeError: __args__. Did you mean: '__ror__'?`
    strawberry.Schema(Query)

    # raises `AttributeError: __args__. Did you mean: '__ror__'?`
    strawberry.Schema(Query2)

invokermain avatar May 10 '22 14:05 invokermain