Skip to content

Commit f8ae641

Browse files
committed
Ensure consistent name for generated Union class
...by ensuring consistent ordering of the classes from which it's constructed, based on the class name.
1 parent 35fdc64 commit f8ae641

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

graphene_pydantic/converters.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,7 @@ def convert_literal_type(
351351
inner_types = type_.__args__
352352
# Here we'll expand the subtypes of this Literal into a corresponding more
353353
# general scalar type.
354-
scalar_types = {
355-
type(x)
356-
for x in inner_types
357-
if x != NONE_TYPE
358-
}
354+
scalar_types = {type(x) for x in inner_types if x != NONE_TYPE}
359355
graphene_scalar_types = [
360356
convert_pydantic_type(x, field, registry, parent_type=parent_type, model=model)
361357
for x in scalar_types
@@ -368,6 +364,10 @@ def convert_literal_type(
368364
internal_meta_cls = type("Meta", (), {"types": graphene_scalar_types})
369365

370366
union_cls = type(
371-
construct_union_class_name(scalar_types), (Union,), {"Meta": internal_meta_cls}
367+
construct_union_class_name(
368+
sorted(scalar_types, key=lambda x: x.__class__.__name__)
369+
),
370+
(Union,),
371+
{"Meta": internal_meta_cls},
372372
)
373373
return union_cls

tests/test_converters.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,18 @@ def test_union():
7474
assert field.default_value == 5.0
7575
assert field.type.__name__.startswith("UnionOf")
7676

77+
7778
def test_literal():
78-
field = _convert_field_from_spec("attr", (T.Literal['literal1', 'literal2', 3], 3))
79+
field = _convert_field_from_spec("attr", (T.Literal["literal1", "literal2", 3], 3))
7980
assert issubclass(field.type, graphene.Union)
8081
assert field.default_value == 3
8182
assert field.type.__name__.startswith("UnionOf")
8283

8384

8485
def test_literal_singleton():
85-
field = _convert_field_from_spec("attr", (T.Literal['literal1'], 'literal1'))
86+
field = _convert_field_from_spec("attr", (T.Literal["literal1"], "literal1"))
8687
assert issubclass(field.type, graphene.String)
87-
assert field.default_value == 'literal1'
88+
assert field.default_value == "literal1"
8889
assert field.type == graphene.String
8990

9091

0 commit comments

Comments
 (0)