Skip to content

Commit 7ea4f42

Browse files
authored
Merge pull request #13 from BoyangHuang/master
Ignore all NoneTypes during evaluation of Unions
2 parents 2921c10 + 230d62f commit 7ea4f42

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

graphene_pydantic/converters.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,22 +240,20 @@ def convert_union_type(
240240
Convert an annotated Python Union type into a Graphene Union.
241241
"""
242242
inner_types = type_.__args__
243-
if len(inner_types) == 2 and NONE_TYPE in inner_types:
244-
# This is effectively a typing.Optional[T], which decomposes into a
245-
# typing.Union[None, T] -- we can return the Graphene type for T directly
246-
# since Pydantic will have already parsed it as optional
247-
native_type = next(x for x in inner_types if x != NONE_TYPE) # noqa: E721
248-
graphene_type = find_graphene_type(
249-
native_type, field, registry, parent_type=parent_type, model=model
250-
)
251-
return graphene_type
252-
253-
# Otherwise, we use a little metaprogramming -- create our own unique
243+
# We use a little metaprogramming -- create our own unique
254244
# subclass of graphene.Union that knows its constituent Graphene types
255245
parent_types = tuple(
256246
find_graphene_type(x, field, registry, parent_type=parent_type, model=model)
257247
for x in inner_types
248+
if x != NONE_TYPE
258249
)
250+
251+
# This is effectively a typing.Optional[T], which decomposes into a
252+
# typing.Union[None, T] -- we can return the Graphene type for T directly
253+
# since Pydantic will have already parsed it as optional
254+
if len(parent_types) == 1:
255+
return parent_types[0]
256+
259257
internal_meta_cls = type("Meta", (), {"types": parent_types})
260258

261259
union_cls = type(

0 commit comments

Comments
 (0)