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

Fix #19 by using choice keys as enum keys, not choice descriptions #23

Merged
merged 1 commit into from
Oct 15, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions graphene_django/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from graphene.types.datetime import DateTime
from graphene.types.json import JSONString
from graphene.utils.str_converters import to_const
from graphql import assert_valid_name

from .compat import (ArrayField, HStoreField, JSONField, RangeField,
RelatedObject, UUIDField)
Expand All @@ -17,7 +18,12 @@


def convert_choice_name(name):
return to_const(force_text(name))
name = to_const(force_text(name))
try:
assert_valid_name(name)
except AssertionError:
name = "A_%s" % name

Choose a reason for hiding this comment

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

This could break badly if python is run with optimisations on (python -O) because that disables assert statements, as used in assert_valid_name. The possible fixes are to update assert_valid_name to raise a different exception (ValueError probably) or to add a parallel function to assert_valid_name for use like this.

return name


def get_choices(choices):
Expand All @@ -26,7 +32,7 @@ def get_choices(choices):
for choice in get_choices(help_text):
yield choice
else:
name = convert_choice_name(help_text)
name = convert_choice_name(value)
description = help_text
yield name, value, description

Expand Down