strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓
https://strawberry.rocks
MIT License
3.99k stars 531 forks source link

`StrawberryAnnotation.create_list` cannot handle empty list type #1901

Open invokermain opened 2 years ago

invokermain commented 2 years ago

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:

System Information

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)

Upvote & Fund

Fund with Polar

patrick91 commented 2 years ago

we should provide a better error, I don't think we should treat it as list[Any] 😊