Skip to content

Commit

Permalink
fix: list[str] is not interpreted as List[String!]! instead as List[S…
Browse files Browse the repository at this point in the history
…tring!]
  • Loading branch information
mak626 committed Oct 10, 2024
1 parent ae5a77b commit 9d24474
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
17 changes: 11 additions & 6 deletions graphene_pydantic/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import enum
import inspect
import sys
import types
import typing as T
import uuid
from typing import Type, get_origin
Expand All @@ -28,16 +29,16 @@
from graphene.types.datetime import Date, DateTime, Time
from pydantic import BaseModel
from pydantic.fields import FieldInfo
from pydantic_core import PydanticUndefined
from pydantic.networks import EmailStr
from pydantic_core import PydanticUndefined

from .registry import Placeholder, Registry
from .util import construct_union_class_name, evaluate_forward_ref

PYTHON10 = sys.version_info >= (3, 10)
PYTHON9 = sys.version_info >= (3, 9)
if PYTHON10:
from types import UnionType
from types import NoneType, UnionType
if PYTHON9:
from typing import Annotated

Expand Down Expand Up @@ -310,7 +311,7 @@ def convert_generic_python_type(
registry: Registry,
parent_type: T.Type = None,
model: T.Type[BaseModel] = None,
) -> T.Union[Type[T.Union[BaseType, List]], Placeholder]:
) -> T.Union[Type[T.Union[BaseType, List]], List, Placeholder]:
"""
Convert annotated Python generic types into the most appropriate Graphene
Field type -- e.g., turn `typing.Union` into a Graphene Union.
Expand Down Expand Up @@ -348,10 +349,14 @@ def convert_generic_python_type(
# Of course, we can only return a homogeneous type here, so we pick the
# first of the wrapped types
inner_type = inner_types[0]
inner_gql_type = find_graphene_type(
inner_types[0], field, registry, parent_type=parent_type, model=model
)
is_required = not (
isinstance(inner_type, types.UnionType) and NoneType in inner_type.__args__
)
return List(
find_graphene_type(
inner_type, field, registry, parent_type=parent_type, model=model
)
graphene.NonNull(inner_gql_type) if is_required else inner_gql_type,
)
elif origin in (T.Dict, T.Mapping, collections.OrderedDict, dict) or issubclass(
origin, collections.abc.Mapping
Expand Down
10 changes: 10 additions & 0 deletions tests/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,20 @@ def test_decimal(monkeypatch):
def test_iterables():
field = _convert_field_from_spec("attr", (T.List[int], [1, 2]))
assert isinstance(field.type.of_type, graphene.types.List)
assert isinstance(field.type.of_type.of_type, graphene.types.NonNull)
assert field.type.of_type.of_type.of_type == graphene.types.Int

field = _convert_field_from_spec("attr", (T.List[int | None], [1, 2]))
assert isinstance(field.type.of_type, graphene.types.List)
assert field.type.of_type.of_type == graphene.types.Int

field = _convert_field_from_spec("attr", (list, [1, 2]))
assert field.type.of_type == graphene.types.List

field = _convert_field_from_spec("attr", (T.Set[int], {1, 2}))
assert isinstance(field.type.of_type, graphene.types.List)
assert isinstance(field.type.of_type.of_type, graphene.types.NonNull)
assert field.type.of_type.of_type.of_type == graphene.types.Int

field = _convert_field_from_spec("attr", (set, {1, 2}))
assert field.type.of_type == graphene.types.List
Expand All @@ -168,6 +176,8 @@ def test_iterables():

field = _convert_field_from_spec("attr", (T.Tuple[int, ...], (1, 2.2)))
assert isinstance(field.type.of_type, graphene.types.List)
assert isinstance(field.type.of_type.of_type, graphene.types.NonNull)
assert field.type.of_type.of_type.of_type == graphene.types.Int

field = _convert_field_from_spec("attr", (tuple, (1, 2)))
assert field.type.of_type == graphene.types.List
Expand Down

0 comments on commit 9d24474

Please sign in to comment.