Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions graphene/types/enum.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import builtins
from enum import Enum as PyEnum

from graphene.utils.subclass_with_meta import SubclassWithMeta_Meta
Expand Down Expand Up @@ -69,8 +70,8 @@ def from_enum(cls, enum, name=None, description=None, deprecation_reason=None):
"description": description,
"deprecation_reason": deprecation_reason,
}
meta_class = type("Meta", (object,), meta_dict)
return type(name, (Enum,), {"Meta": meta_class})
meta_class = builtins.type("Meta", (object,), meta_dict)
return builtins.type(name, (Enum,), {"Meta": meta_class})


class Enum(UnmountedType, BaseType, metaclass=EnumMeta):
Expand Down
30 changes: 30 additions & 0 deletions graphene/types/tests/test_enum.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import builtins
from textwrap import dedent

import pytest

from .. import enum as enum_module

from ..argument import Argument
from ..enum import Enum, PyEnum
from ..field import Field
Expand Down Expand Up @@ -613,3 +618,28 @@ def resolve_color(_, info):
results = schema.execute("query { color }")
assert not results.errors
assert results.data["color"] == RGB.description.name


@pytest.mark.parametrize("construction", ["class", "functional", "from_enum"])
def test_enum_named_type_does_not_shadow_builtin_type(monkeypatch, construction):
# EnumMeta registers enum classes in this module to support pickling.
# Restore that module-level name so this test cannot affect other tests.
monkeypatch.setattr(enum_module, "type", builtins.type, raising=False)

if construction == "class":

class type(Enum):
FIRST = 1

enum = type
elif construction == "functional":
enum = Enum("type", [("FIRST", 1)])
else:
enum = Enum.from_enum(PyEnum("type", [("FIRST", 1)]))

assert enum._meta.name == "type"
assert enum.FIRST.value == 1

# Constructing another enum must still work after registering "type".
other = Enum("AfterType", [("SECOND", 2)])
assert other.SECOND.value == 2