Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotated + strawberry.lazy + deferred annotations results in UnresolvedFieldTypeError: Could not resolve the type of ... errors #3491

Closed
huonw opened this issue May 8, 2024 · 2 comments · Fixed by #3507
Labels
bug Something isn't working

Comments

@huonw
Copy link
Contributor

huonw commented May 8, 2024

Describe the Bug

When using Annotated[..., strawberry.lazy(...)], one seems to get spurious errors in a file with from __future__ import annotations:

from __future__ import annotations
import strawberry
from typing import Annotated

@strawberry.type
class Query:
    a: Annotated["datetime", strawberry.lazy("datetime")]

schema = strawberry.Schema(Query)

Running that like python example.py with strawberry 0.277.3 gives errors like:

Traceback (most recent call last):
  File "/tmp/tmp.VSRhsBYbmq/example.py", line 9, in <module>
    schema = strawberry.Schema(Query)
  File "/tmp/tmp.VSRhsBYbmq/.venv/lib/python3.10/site-packages/strawberry/schema/schema.py", line 162, in __init__
    raise error.__cause__ from None
  File "/tmp/tmp.VSRhsBYbmq/.venv/lib/python3.10/site-packages/graphql/type/definition.py", line 808, in fields
    fields = resolve_thunk(self._fields)
  File "/tmp/tmp.VSRhsBYbmq/.venv/lib/python3.10/site-packages/graphql/type/definition.py", line 300, in resolve_thunk
    return thunk() if callable(thunk) else thunk
  File "/tmp/tmp.VSRhsBYbmq/.venv/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 525, in <lambda>
    fields=lambda: self.get_graphql_fields(object_type),
  File "/tmp/tmp.VSRhsBYbmq/.venv/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 382, in get_graphql_fields
    return _get_thunk_mapping(
  File "/tmp/tmp.VSRhsBYbmq/.venv/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 132, in _get_thunk_mapping
    raise UnresolvedFieldTypeError(type_definition, field)
strawberry.exceptions.unresolved_field_type.UnresolvedFieldTypeError: Could not resolve the type of 'a'. Check that the class is accessible from the global module scope.

Standalone reproducer (run like bash ./script.sh):

# Setup:
cd $(mktemp -d)

python -m venv .venv

.venv/bin/python -m pip install graphql-core==3.2.3 python-dateutil==2.9.0.post0 six==1.16.0 typing_extensions==4.11.0 strawberry-graphql==0.227.3

cat > example.py <<EOF
from __future__ import annotations
import strawberry
from typing import Annotated

@strawberry.type
class Query:
    a: Annotated["datetime", strawberry.lazy("datetime")]

schema = strawberry.Schema(Query)
EOF

# BUG: strawberry.exceptions.unresolved_field_type.UnresolvedFieldTypeError: Could not resolve the type of 'a'. Check that the class is accessible from the global module scope.
.venv/bin/python example.py

System Information

  • Operating system: macOS
  • Strawberry version (if applicable): 0.227.3

Additional Context

Thank you for strawberry! 🍓

This seems a bit similar to:

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
@huonw huonw added the bug Something isn't working label May 8, 2024
@huonw huonw changed the title Annotated + strawberry.lazy + from __future__ import annotations results in UnresolvedFieldTypeError: Could not resolve the type of ... errors Annotated + strawberry.lazy + deferred annotations results in UnresolvedFieldTypeError: Could not resolve the type of ... errors May 9, 2024
@huonw
Copy link
Contributor Author

huonw commented May 9, 2024

Ah, the same applies to "manually" deferred annotations (i.e. an explicit string):

import strawberry
from typing import Annotated

@strawberry.type
class Query:
    a: """Annotated["datetime", strawberry.lazy("datetime")]"""

schema = strawberry.Schema(Query)

@patrick91
Copy link
Member

I think we'll fix this next week with @bellini666 😊

patrick91 pushed a commit that referenced this issue May 22, 2024
* fix: Fix Annotated + strawberry.lazy + deferred annotations

Fix #3491

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants