Skip to content

Commit be7bf62

Browse files
committed
fix: nested union type errors
1 parent 745ab32 commit be7bf62

File tree

1 file changed

+52
-46
lines changed

1 file changed

+52
-46
lines changed

graphene_pydantic/converters.py

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ def _get_field(root, _info):
5151

5252

5353
def convert_pydantic_input_field(
54-
field: FieldInfo,
55-
registry: Registry,
56-
parent_type: T.Type = None,
57-
model: T.Type[BaseModel] = None,
58-
**field_kwargs,
54+
field: FieldInfo,
55+
registry: Registry,
56+
parent_type: T.Type = None,
57+
model: T.Type[BaseModel] = None,
58+
**field_kwargs,
5959
) -> InputField:
6060
"""
6161
Convert a Pydantic model field into a Graphene type field that we can add
@@ -80,20 +80,18 @@ def convert_pydantic_input_field(
8080

8181

8282
def convert_pydantic_field(
83-
name: str,
84-
field: FieldInfo,
85-
registry: Registry,
86-
parent_type: T.Type = None,
87-
model: T.Type[BaseModel] = None,
88-
**field_kwargs,
83+
name: str,
84+
field: FieldInfo,
85+
registry: Registry,
86+
parent_type: T.Type = None,
87+
model: T.Type[BaseModel] = None,
88+
**field_kwargs,
8989
) -> Field:
9090
"""
9191
Convert a Pydantic model field into a Graphene type field that we can add
9292
to the generated Graphene data model type.
9393
"""
9494
declared_type = getattr(field, "annotation", None)
95-
if isinstance(declared_type, UnionType):
96-
declared_type = T.Union[declared_type.__args__]
9795
field_kwargs.setdefault(
9896
"type" if GRAPHENE2 else "type_",
9997
convert_pydantic_type(
@@ -102,12 +100,15 @@ def convert_pydantic_field(
102100
)
103101
field_kwargs.setdefault(
104102
"required",
105-
field.is_required() or (
106-
type(field.default) is not PydanticUndefined and
107-
getattr(declared_type, '_name', '') != 'Optional'
108-
)
103+
field.is_required()
104+
or (
105+
type(field.default) is not PydanticUndefined
106+
and getattr(declared_type, "_name", "") != "Optional"
107+
),
108+
)
109+
field_kwargs.setdefault(
110+
"default_value", None if field.default is PydanticUndefined else field.default
109111
)
110-
field_kwargs.setdefault("default_value", None if field.default is PydanticUndefined else field.default)
111112
if field.alias:
112113
field_kwargs.setdefault("name", field.alias)
113114
# TODO: find a better way to get a field's description. Some ideas include:
@@ -131,11 +132,11 @@ def convert_pydantic_field(
131132

132133

133134
def convert_pydantic_type(
134-
type_: T.Type,
135-
field: FieldInfo,
136-
registry: Registry,
137-
parent_type: T.Type = None,
138-
model: T.Type[BaseModel] = None,
135+
type_: T.Type,
136+
field: FieldInfo,
137+
registry: Registry,
138+
parent_type: T.Type = None,
139+
model: T.Type[BaseModel] = None,
139140
) -> BaseType: # noqa: C901
140141
"""
141142
Convert a Pydantic type to a Graphene Field type, including not just the
@@ -145,24 +146,29 @@ def convert_pydantic_type(
145146
graphene_type = find_graphene_type(
146147
type_, field, registry, parent_type=parent_type, model=model
147148
)
148-
field_type = getattr(field.annotation, '__origin__', None)
149+
field_type = getattr(field.annotation, "__origin__", None)
149150
if field_type == map: # SHAPE_MAPPING
150151
raise ConversionError("Don't know how to handle mappings in Graphene.")
151152

152153
return graphene_type
153154

154155

155156
def find_graphene_type(
156-
type_: T.Type,
157-
field: FieldInfo,
158-
registry: Registry,
159-
parent_type: T.Type = None,
160-
model: T.Type[BaseModel] = None,
157+
type_: T.Type,
158+
field: FieldInfo,
159+
registry: Registry,
160+
parent_type: T.Type = None,
161+
model: T.Type[BaseModel] = None,
161162
) -> BaseType: # noqa: C901
162163
"""
163164
Map a native Python type to a Graphene-supported Field type, where possible,
164165
throwing an error if we don't know what to map it to.
165166
"""
167+
168+
# Convert Python 11 UnionType to T.Union
169+
if isinstance(type_, UnionType):
170+
type_ = T.Union[type_.__args__]
171+
166172
if type_ == uuid.UUID:
167173
return UUID
168174
elif type_ in (str, bytes):
@@ -191,8 +197,8 @@ def find_graphene_type(
191197
elif registry and registry.get_type_for_model(type_):
192198
return registry.get_type_for_model(type_)
193199
elif registry and (
194-
isinstance(type_, BaseModel)
195-
or (inspect.isclass(type_) and issubclass(type_, BaseModel))
200+
isinstance(type_, BaseModel)
201+
or (inspect.isclass(type_) and issubclass(type_, BaseModel))
196202
):
197203
# If it's a Pydantic model that hasn't yet been wrapped with a ObjectType,
198204
# we can put a placeholder in and request that `resolve_placeholders()`
@@ -252,11 +258,11 @@ def find_graphene_type(
252258

253259

254260
def convert_generic_python_type(
255-
type_: T.Type,
256-
field: FieldInfo,
257-
registry: Registry,
258-
parent_type: T.Type = None,
259-
model: T.Type[BaseModel] = None,
261+
type_: T.Type,
262+
field: FieldInfo,
263+
registry: Registry,
264+
parent_type: T.Type = None,
265+
model: T.Type[BaseModel] = None,
260266
) -> BaseType: # noqa: C901
261267
"""
262268
Convert annotated Python generic types into the most appropriate Graphene
@@ -309,11 +315,11 @@ def convert_generic_python_type(
309315

310316

311317
def convert_union_type(
312-
type_: T.Type,
313-
field: FieldInfo,
314-
registry: Registry,
315-
parent_type: T.Type = None,
316-
model: T.Type[BaseModel] = None,
318+
type_: T.Type,
319+
field: FieldInfo,
320+
registry: Registry,
321+
parent_type: T.Type = None,
322+
model: T.Type[BaseModel] = None,
317323
):
318324
"""
319325
Convert an annotated Python Union type into a Graphene Union.
@@ -342,11 +348,11 @@ def convert_union_type(
342348

343349

344350
def convert_literal_type(
345-
type_: T.Type,
346-
field: FieldInfo,
347-
registry: Registry,
348-
parent_type: T.Type = None,
349-
model: T.Type[BaseModel] = None,
351+
type_: T.Type,
352+
field: FieldInfo,
353+
registry: Registry,
354+
parent_type: T.Type = None,
355+
model: T.Type[BaseModel] = None,
350356
):
351357
"""
352358
Convert an annotated Python Literal type into a Graphene Scalar or Union of Scalars.

0 commit comments

Comments
 (0)