chore: Make Graphene enums iterable like Python enums (#1473)

* Makes Graphene enums iterable like Python enums by implementing __iter__
This commit is contained in:
Rens Groothuijsen 2022-11-16 21:30:49 +01:00 committed by GitHub
parent f891a3683d
commit 0b1bfbf65b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -56,6 +56,9 @@ class EnumMeta(SubclassWithMeta_Meta):
return super(EnumMeta, cls).__call__(*args, **kwargs)
# return cls._meta.enum(*args, **kwargs)
def __iter__(cls):
return cls._meta.enum.__iter__()
def from_enum(
cls, enum, name=None, description=None, deprecation_reason=None
): # noqa: N805

View File

@ -543,3 +543,25 @@ def test_hashable_instance_creation_enum():
assert trilogy_map[Episode.NEWHOPE] == "better"
assert trilogy_map[Episode.EMPIRE] == "best"
assert trilogy_map[5] == "foo"
def test_enum_iteration():
class TestEnum(Enum):
FIRST = 1
SECOND = 2
result = []
expected_values = ["FIRST", "SECOND"]
for c in TestEnum:
result.append(c.name)
assert result == expected_values
def test_iterable_instance_creation_enum():
TestEnum = Enum("TestEnum", [("FIRST", 1), ("SECOND", 2)])
result = []
expected_values = ["FIRST", "SECOND"]
for c in TestEnum:
result.append(c.name)
assert result == expected_values