mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-07-13 09:42:32 +03:00
prefix underscore choice name only if it is dunder, otherwise underscores like sunder are allowed
This commit is contained in:
parent
43233d7d9d
commit
59184a117d
|
@ -28,10 +28,18 @@ from .utils import import_single_dispatch
|
|||
singledispatch = import_single_dispatch()
|
||||
|
||||
|
||||
def _is_dunder(name):
|
||||
"""Returns True if a __dunder__ name, False otherwise."""
|
||||
return (len(name) > 4 and
|
||||
name[:2] == name[-2:] == '__' and
|
||||
name[2:3] != '_' and
|
||||
name[-3:-2] != '_')
|
||||
|
||||
|
||||
def convert_choice_name(name):
|
||||
name = force_text(name).encode('utf8').decode('ascii', 'ignore')
|
||||
name = to_const(name)
|
||||
if name.startswith('_'):
|
||||
if _is_dunder(name):
|
||||
name = "A%s" % name
|
||||
try:
|
||||
assert_valid_name(name)
|
||||
|
|
|
@ -199,7 +199,7 @@ def test_field_with_choices_collision():
|
|||
def test_field_with_choices_underscore():
|
||||
field = models.CharField(
|
||||
choices=(
|
||||
("__amount__", "Amount"),
|
||||
("_amount__", "Amount"),
|
||||
("__percentage__", "Percentage"),
|
||||
),
|
||||
)
|
||||
|
@ -212,8 +212,8 @@ def test_field_with_choices_underscore():
|
|||
|
||||
graphene_type = convert_django_field_with_choices(field)
|
||||
assert len(graphene_type._meta.enum.__members__) == 2
|
||||
assert graphene_type._meta.enum.__members__["A__AMOUNT__"].value == "__amount__"
|
||||
assert graphene_type._meta.enum.__members__["A__AMOUNT__"].description == "Amount"
|
||||
assert graphene_type._meta.enum.__members__["_AMOUNT__"].value == "_amount__"
|
||||
assert graphene_type._meta.enum.__members__["_AMOUNT__"].description == "Amount"
|
||||
assert graphene_type._meta.enum.__members__["A__PERCENTAGE__"].value == "__percentage__"
|
||||
assert graphene_type._meta.enum.__members__["A__PERCENTAGE__"].description == "Percentage"
|
||||
|
||||
|
|
|
@ -174,7 +174,7 @@ type Reporter {
|
|||
enum ReporterAChoice {
|
||||
A_1
|
||||
A_2
|
||||
A_3
|
||||
_3
|
||||
}
|
||||
|
||||
enum ReporterReporterType {
|
||||
|
|
Loading…
Reference in New Issue
Block a user