Skip to content

Commit

Permalink
fix: do not strip the type when using auto (#2990)
Browse files Browse the repository at this point in the history
  • Loading branch information
bellini666 authored Jul 28, 2023
1 parent 5c68c7e commit 3c47ef7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
11 changes: 11 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Release type: patch

This release fixes an issue when trying to use `Annotated[strawberry.auto, ...]`
on python 3.10 or older, which got evident after the fix from 0.196.1.

Previously we were throwing the type away, since it usually is `Any`, but python
3.10 and older will validate that the first argument passed for `Annotated`
is callable (3.11+ does not do that anymore), and `StrawberryAuto` is not.

This changes it to keep that `Any`, which is also what someone would expect
when resolving the annotation using our custom `eval_type` function.
18 changes: 10 additions & 8 deletions strawberry/experimental/pydantic/error_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,12 @@ def wrap(cls: Type) -> Type:
)

existing_fields = getattr(cls, "__annotations__", {})
fields_set = fields_set.union(
{
name
for name, type_ in existing_fields.items()
if isinstance(type_, StrawberryAuto)
}
)
auto_fields_set = {
name
for name, type_ in existing_fields.items()
if isinstance(type_, StrawberryAuto)
}
fields_set |= auto_fields_set

if all_fields:
if fields_set:
Expand Down Expand Up @@ -124,7 +123,10 @@ def wrap(cls: Type) -> Type:
field,
)
for field in extra_fields + private_fields
if not isinstance(field.type, StrawberryAuto)
if (
field.name not in auto_fields_set
and not isinstance(field.type, StrawberryAuto)
)
)

cls = dataclasses.make_dataclass(
Expand Down
2 changes: 1 addition & 1 deletion strawberry/utils/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def eval_type(
remaining_args = [
a for a in args[1:] if not isinstance(a, StrawberryAuto)
]
args = (arg, *remaining_args)
args = (args[0], arg, *remaining_args)
break

# If we have only a StrawberryLazyReference and no more annotations,
Expand Down
8 changes: 8 additions & 0 deletions tests/utils/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ class Foo:
strawberry.lazy("tests.utils.test_typing"),
]
)
assert (
eval_type(
ForwardRef("Annotated[strawberry.auto, 'foobar']"),
{"strawberry": strawberry, "Annotated": Annotated},
None,
)
== Annotated[strawberry.auto, "foobar"]
)


def test_is_classvar():
Expand Down

0 comments on commit 3c47ef7

Please sign in to comment.