Skip to content

Commit

Permalink
Add pydantic conversion compatibility with specialized list class
Browse files Browse the repository at this point in the history
- move `_is_list` check before the `_is_generic` check in `StrawberryAnnotation.resolve`.
- change `StrawberryAnnotation._is_list` to check if the `annotation` extends from list and can be considered a list.
  • Loading branch information
tjeerddie committed Jun 29, 2023
1 parent 1b88a84 commit e820f9a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
12 changes: 9 additions & 3 deletions strawberry/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def resolve(self) -> Union[StrawberryType, type]:
evaled_type = self._strip_async_type(evaled_type)
if self._is_lazy_type(evaled_type):
return evaled_type
if self._is_list(evaled_type):
return self.create_list(evaled_type)

if self._is_generic(evaled_type):
if any(is_type_var(type_) for type_ in evaled_type.__args__):
Expand All @@ -114,8 +116,6 @@ def resolve(self) -> Union[StrawberryType, type]:
# a StrawberryType
if self._is_enum(evaled_type):
return self.create_enum(evaled_type)
if self._is_list(evaled_type):
return self.create_list(evaled_type)
elif self._is_optional(evaled_type):
return self.create_optional(evaled_type)
elif self._is_union(evaled_type):
Expand Down Expand Up @@ -250,8 +250,14 @@ def _is_list(cls, annotation: Any) -> bool:
"""Returns True if annotation is a List"""

annotation_origin = getattr(annotation, "__origin__", None)
annotation_mro = getattr(annotation, "__mro__", [])
is_list = any(x is list for x in annotation_mro)

return (annotation_origin in (list, tuple)) or annotation_origin is abc.Sequence
return (
(annotation_origin in (list, tuple))
or annotation_origin is abc.Sequence
or is_list
)

@classmethod
def _is_strawberry_type(cls, evaled_type: Any) -> bool:
Expand Down
36 changes: 35 additions & 1 deletion tests/experimental/pydantic/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
import sys
from enum import Enum
from typing import Any, Dict, List, NewType, Optional, Union, cast
from typing import Any, Dict, List, NewType, Optional, TypeVar, Union, cast

import pytest
from pydantic import BaseConfig, BaseModel, Field, ValidationError
Expand Down Expand Up @@ -1194,3 +1194,37 @@ class Test:

assert test.optional_list == [1, 2, 3]
assert test.optional_str is None


SI = TypeVar("SI", covariant=True) # pragma: no mutate


class SpecialList(List[SI]):
pass


def test_can_convert_pydantic_type_to_strawberry_with_constrained_list():
class WorkModel(BaseModel):
name: str

class workList(SpecialList[SI]):
min_items = 1

class UserModel(BaseModel):
work: workList[WorkModel]

@strawberry.experimental.pydantic.type(WorkModel)
class Work:
name: strawberry.auto

@strawberry.experimental.pydantic.type(UserModel)
class User:
work: strawberry.auto

origin_user = UserModel(
work=[WorkModel(name="developer"), WorkModel(name="tester")]
)

user = User.from_pydantic(origin_user)

assert user == User(work=[Work(name="developer"), Work(name="tester")])

0 comments on commit e820f9a

Please sign in to comment.