Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Graphene enums iterable like Python enums #1473

Merged
merged 2 commits into from Nov 16, 2022

Conversation

rgroothuijsen
Copy link
Contributor

Since Python enums are iterable, the expectation exists that Graphene enums are similarly iterable, as shown in #1264. The proposed solution is to do a straightforward pass-through of the underlying Python enum, so that it will behave exactly the same way.

Fixes #1264

flipbit03
flipbit03 previously approved these changes Nov 14, 2022
Copy link
Contributor

@flipbit03 flipbit03 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@erikwrede
Copy link
Member

Thanks for the PR! Will test it locally this afternoon and it should be good to merge afterwards! 🙂

Copy link
Member

@erikwrede erikwrede left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thank you!
Added a small comment suggesting a slight improvment. Should be good to merge afterwards 🙂

Comment on lines 1 to 13
from graphene.types.enum import Enum


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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be better-placed in test_enum.py, as this is not really a bug but rather a new "Feature" of the graphene-style enums.
Can you also add this test case for the instance-creation style of enums?

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

@erikwrede erikwrede merged commit 0b1bfbf into graphql-python:master Nov 16, 2022
@erikwrede
Copy link
Member

erikwrede commented Nov 16, 2022

Thank you for the PRs!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

KeyError: 0 while iterating PyEnum-style
3 participants