Skip to content

Commit 014c6ff

Browse files
committed
fix: UnionType imported in python versions < 3.10
1 parent ff1d781 commit 014c6ff

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

graphene_pydantic/converters.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import sys
88
import typing as T
99
import uuid
10-
from types import UnionType
1110
from typing import Type, get_origin
1211

1312
import graphene
@@ -33,6 +32,10 @@
3332
from .registry import Registry
3433
from .util import construct_union_class_name, evaluate_forward_ref
3534

35+
PYTHON10 = sys.version_info >= (3, 10)
36+
if PYTHON10:
37+
from types import UnionType
38+
3639
GRAPHENE2 = graphene.VERSION[0] < 3
3740

3841
try:
@@ -114,10 +117,15 @@ def convert_pydantic_field(
114117
"""
115118
declared_type = getattr(field, "annotation", None)
116119

117-
# Convert Python 11 UnionType to T.Union
118-
is_union_type = (
119-
get_origin(declared_type) is T.Union or get_origin(declared_type) is UnionType
120-
)
120+
# Convert Python 10 UnionType to T.Union
121+
if PYTHON10:
122+
is_union_type = (
123+
get_origin(declared_type) is T.Union
124+
or get_origin(declared_type) is UnionType
125+
)
126+
else:
127+
is_union_type = get_origin(declared_type) is T.Union
128+
121129
if is_union_type:
122130
declared_type = T.Union[declared_type.__args__]
123131

@@ -195,9 +203,10 @@ def find_graphene_type(
195203
throwing an error if we don't know what to map it to.
196204
"""
197205

198-
# Convert Python 11 UnionType to T.Union
199-
if isinstance(type_, UnionType):
200-
type_ = T.Union[type_.__args__]
206+
# Convert Python 10 UnionType to T.Union
207+
if PYTHON10:
208+
if isinstance(type_, UnionType):
209+
type_ = T.Union[type_.__args__]
201210

202211
if type_ == uuid.UUID:
203212
return UUID

graphene_pydantic/inputobjecttype.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import typing as T
2-
from types import UnionType
32

43
import graphene
54
import pydantic
@@ -46,7 +45,8 @@ def construct_fields(
4645
fields = {}
4746
for name, field in fields_to_convert:
4847
# Graphql does not accept union as input. Refer https://github.com/graphql/graphql-spec/issues/488
49-
if isinstance(getattr(field, "annotation", None), UnionType):
48+
annotation = getattr(field, "annotation", None)
49+
if isinstance(annotation, str) or isinstance(annotation, int):
5050
union_types = field.annotation.__args__
5151
if type(None) not in union_types or len(union_types) > 2:
5252
continue
@@ -80,8 +80,8 @@ def __init_subclass_with_meta__(
8080
_meta=None,
8181
**options,
8282
):
83-
assert model and issubclass(
84-
model, pydantic.BaseModel
83+
assert (
84+
model and issubclass(model, pydantic.BaseModel)
8585
), f'You need to pass a valid Pydantic model in {cls.__name__}.Meta, received "{model}"'
8686

8787
assert isinstance(

graphene_pydantic/objecttype.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import pydantic
55
from graphene.types.objecttype import ObjectTypeOptions
66
from graphene.types.utils import yank_fields_from_attrs
7-
from graphql import GraphQLResolveInfo
87

98
from .converters import convert_pydantic_field
109
from .inputobjecttype import PydanticInputObjectType
@@ -72,8 +71,8 @@ def __init_subclass_with_meta__(
7271
_meta=None,
7372
**options,
7473
):
75-
assert model and issubclass(
76-
model, pydantic.BaseModel
74+
assert (
75+
model and issubclass(model, pydantic.BaseModel)
7776
), f'You need to pass a valid Pydantic model in {cls.__name__}.Meta, received "{model}"'
7877

7978
assert isinstance(
@@ -159,7 +158,7 @@ def resolve_placeholders(cls):
159158
meta.fields.update(fields_to_update)
160159

161160
@classmethod
162-
def is_type_of(cls, root, _: GraphQLResolveInfo) -> bool:
161+
def is_type_of(cls, root, info) -> bool:
163162
if isinstance(root, PydanticInputObjectType):
164-
return type(root._meta.model) is type(cls._meta.model)
163+
return type(root._meta.model) is type(cls._meta.model) # noqa: E721
165164
return isinstance(root, cls._meta.model)

0 commit comments

Comments
 (0)