Skip to content

Enum conversion fixes #665

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

Closed
wants to merge 14 commits into from

Conversation

zbyte64
Copy link
Collaborator

@zbyte64 zbyte64 commented Jun 10, 2019

Collected backwards compatible fixes from #654

Fixes #141 and allows using non-ascii characters as choice values.

name = force_text(name).encode('utf8').decode('ascii', 'ignore')
name = to_const(name)
if name.startswith('_'):
name = "A%s" % name
Copy link
Member

Choose a reason for hiding this comment

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

@zbyte64 I'm a bit confused with what is happening here. If the name starts with _ you're prefixing it with a A? Why is that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

assert_valid_name requires the name not to start with an _. Without this, a name like _amount would be caught and A_ would be prefixed to become A__amount. With this change, the name becomes A_amount, removing the extra _

Copy link
Member

Choose a reason for hiding this comment

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

@zbyte64 I don't think that is right. Running assert_valid_name on a values with both _ and __ work fine:

In [1]: from graphql import assert_valid_name

In [2]: assert_valid_name('foo')

In [3]: assert_valid_name('_foo')

In [4]: assert_valid_name('__foo')

Copy link
Collaborator Author

@zbyte64 zbyte64 Jun 10, 2019

Choose a reason for hiding this comment

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

Erm, I mispoke. assert_valid_name will accept names that start in _ but graphene's Enum will skip over them when populating members. Thus we need to prefix this special case ourselves.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think I see now. I expanded the test case to cover the various underscore mixtures.

Copy link
Contributor

Choose a reason for hiding this comment

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

How does a normal Python enum handle these sunder and dunder values?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Creating a python enum with a sunder causes:
ValueError: _names_ are reserved for future Enum use

Dunder is allowed but is ignored (doesn't generate a member).

Copy link
Contributor

Choose a reason for hiding this comment

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

Then does it make sense to have the same limitations in the Graphene Enum type instead of tacking A_ on the front?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We're already converting choices that wouldn't be allowed, like those that start with a number.

@spockNinja spockNinja removed their request for review July 3, 2019 22:31
@stale
Copy link

stale bot commented Sep 7, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Sep 7, 2019
@stale stale bot closed this Sep 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

_names_ are reserved for future Enum use
4 participants