diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 7173abc..4d1a936 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -17,7 +17,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"] + python-version: ["3.8", "3.9", "3.10", "3.11"] os: [ubuntu-latest, macos-latest, windows-latest] steps: - uses: actions/checkout@v3 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..90dfd60 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,12 @@ +version: '3.5' + +services: + + graphene_federation: + container_name: pydantic + build: + context: . + environment: + - TERM=xterm-256color + volumes: + - ./:/workdir diff --git a/graphene_pydantic/converters.py b/graphene_pydantic/converters.py index 35b9853..1e3297c 100644 --- a/graphene_pydantic/converters.py +++ b/graphene_pydantic/converters.py @@ -24,12 +24,13 @@ UUID, Union, ) +from graphene.types.base import BaseType from graphene.types.datetime import Date, DateTime, Time from pydantic import BaseModel from pydantic.fields import FieldInfo from pydantic_core import PydanticUndefined -from .registry import Registry +from .registry import Placeholder, Registry from .util import construct_union_class_name, evaluate_forward_ref PYTHON10 = sys.version_info >= (3, 10) @@ -175,7 +176,7 @@ def convert_pydantic_type( registry: Registry, parent_type: T.Type = None, model: T.Type[BaseModel] = None, -) -> Type: # noqa: C901 +) -> T.Union[Type[T.Union[BaseType, List]], Placeholder]: # noqa: C901 """ Convert a Pydantic type to a Graphene Field type, including not just the native Python type but any additional metadata (e.g. shape) that Pydantic @@ -197,7 +198,7 @@ def find_graphene_type( registry: Registry, parent_type: T.Type = None, model: T.Type[BaseModel] = None, -) -> Type: # noqa: C901 +) -> T.Union[Type[T.Union[BaseType, List]], Placeholder]: # noqa: C901 """ Map a native Python type to a Graphene-supported Field type, where possible, throwing an error if we don't know what to map it to. @@ -303,10 +304,10 @@ def convert_generic_python_type( registry: Registry, parent_type: T.Type = None, model: T.Type[BaseModel] = None, -) -> Type: # noqa: C901 +) -> T.Union[Type[T.Union[BaseType, List]], Placeholder]: # noqa: C901 """ Convert annotated Python generic types into the most appropriate Graphene - Field type -- e.g. turn `typing.Union` into a Graphene Union. + Field type -- e.g., turn `typing.Union` into a Graphene Union. """ origin = type_.__origin__ if not origin: # pragma: no cover # this really should be impossible @@ -393,7 +394,7 @@ def convert_literal_type( registry: Registry, parent_type: T.Type = None, model: T.Type[BaseModel] = None, -): +) -> T.Union[Type[T.Union[BaseType, List]], Placeholder]: """ Convert an annotated Python Literal type into a Graphene Scalar or Union of Scalars. """ diff --git a/graphene_pydantic/inputobjecttype.py b/graphene_pydantic/inputobjecttype.py index 01b7587..95f444d 100644 --- a/graphene_pydantic/inputobjecttype.py +++ b/graphene_pydantic/inputobjecttype.py @@ -80,8 +80,8 @@ def __init_subclass_with_meta__( _meta=None, **options, ): - assert ( - model and issubclass(model, pydantic.BaseModel) + assert model and issubclass( + model, pydantic.BaseModel ), f'You need to pass a valid Pydantic model in {cls.__name__}.Meta, received "{model}"' assert isinstance( diff --git a/graphene_pydantic/objecttype.py b/graphene_pydantic/objecttype.py index 1f36abd..91f6bbd 100644 --- a/graphene_pydantic/objecttype.py +++ b/graphene_pydantic/objecttype.py @@ -71,8 +71,8 @@ def __init_subclass_with_meta__( _meta=None, **options, ): - assert ( - model and issubclass(model, pydantic.BaseModel) + assert model and issubclass( + model, pydantic.BaseModel ), f'You need to pass a valid Pydantic model in {cls.__name__}.Meta, received "{model}"' assert isinstance( diff --git a/graphene_pydantic/registry.py b/graphene_pydantic/registry.py index 1bc99b8..362f2a2 100644 --- a/graphene_pydantic/registry.py +++ b/graphene_pydantic/registry.py @@ -2,6 +2,7 @@ from collections import defaultdict from typing import Dict, Generic, Optional, Type, TypeVar, Union +from graphene.types.base import BaseType from pydantic import BaseModel from pydantic.fields import FieldInfo @@ -39,7 +40,7 @@ class Registry(Generic[T]): def __init__(self, required_obj_type: ObjectType): self._required_obj_type: ObjectType = required_obj_type - self._registry: Dict[ModelType, Output] = {} + self._registry: Dict[ModelType, Union[Type[BaseType], Placeholder]] = {} self._registry_object_fields: Dict[ ObjectType, Dict[str, FieldInfo] ] = defaultdict(dict) @@ -52,7 +53,9 @@ def register(self, obj_type: ObjectType): ), "Can't register models linked to another Registry" self._registry[obj_type._meta.model] = obj_type - def get_type_for_model(self, model: ModelType) -> Optional[Output]: + def get_type_for_model( + self, model: ModelType + ) -> Union[Type[BaseType], Placeholder]: return self._registry.get(model) def add_placeholder_for_model(self, model: ModelType): diff --git a/pyproject.toml b/pyproject.toml index 4942549..2846d00 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,7 +23,7 @@ classifiers = [ keywords = ["api", "graphql", "protocol", "rest", "relay", "graphene", "pydantic", "model"] [tool.poetry.dependencies] -python = "^3.7" +python = "^3.8" graphene = ">=2.1.8" pydantic = ">=2.0"