strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

Lazy annotations don't work with `List[]`

Open ddoroshev opened this issue 2 years ago • 4 comments

The problem occurs when using new-style lazy annotations (with from __future__ import annotations) wrapped into a List[]:

# c.py
@strawberry.type
class C:
    id: strawberry.ID

# d.py
from __future__ import annotations

from typing import TYPE_CHECKING, List
from typing_extensions import Annotated

import strawberry

if TYPE_CHECKING:
    from tests.c import C

@strawberry.type
class D:
    id: strawberry.ID

    @strawberry.field
    async def c_list(self) -> List[Annotated[C, strawberry.lazy("tests.c")]]:
        ...

Error:

E strawberry.exceptions.unresolved_field_type.UnresolvedFieldTypeError:
Could not resolve the type of 'c_list'. Check that the class is accessible from the global module scope.

strawberry/schema/schema_converter.py:321: UnresolvedFieldTypeError

However, if you add another field, which is just Annotated[C, ...], everything is fine:

@strawberry.type
class D:
    id: strawberry.ID

    @strawberry.field
    async def c(self) -> Annotated[C, strawberry.lazy("tests.c")]:
        ...

    @strawberry.field
    async def c_list(self) -> List[Annotated[C, strawberry.lazy("tests.c")]]:
        ...

Also, it works as expected with the old-school annotations, List[Annotated["C", ...]]. I added all the necessary test cases in #2895.

Related PR with the feature:

  • #2744

System Information

  • Operating system: macOS 13.4.1 (doesn't actually matter)
  • Strawberry version (if applicable): 0.190.0

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

ddoroshev avatar Jun 27 '23 13:06 ddoroshev

After some testing I have the same issue as well.

Just a workaround for anyone stumbling upon it:

For non lists return Annotated[C, ...] For lists return List[Annotated["C", ...]]

Aponace avatar Aug 02 '23 15:08 Aponace

This is happening to me aswell when we import single types with Annotated works perfectly.

When we use the list before the annotated it comes an error like this one

strawberry.exceptions.unresolved_field_type.UnresolvedFieldTypeError

Ronjea avatar Nov 10 '23 09:11 Ronjea

@Ronjea do you have a reproduction somewhere? 😊

patrick91 avatar Nov 10 '23 09:11 patrick91

Hello @patrick91!

@ddoroshev have this PR open with tests with the reproduction itself https://github.com/strawberry-graphql/strawberry/pull/2895.

I tried to check the StrawberryAnnotation class itself if there is anyway to strip it but I was not able even to find it where the problem 😓 .

This does not work

@strawberry_django.type(A, description="A")
class A:
    id: UUID

    @strawberry.field()
    def b_list(
        self, info: Info
    ) -> list[Annotated[BType, strawberry.lazy("b.graphql.types")]]:
        return [BType()]     

This works

@strawberry_django.type(A, description="A")
class A:
    id: UUID

    @strawberry.field()
    def b(
        self, info: Info
    ) -> Annotated[BType, strawberry.lazy("b.graphql.types")]:
        return BType()

    @strawberry.field()
    def b_list(
        self, info: Info
    ) -> list[Annotated[BType, strawberry.lazy("b.graphql.types")]]:
        return [BType()]     

Ronjea avatar Nov 10 '23 12:11 Ronjea