Fixed Enum value getter

This commit is contained in:
Syrus Akbary 2016-06-04 15:42:17 -07:00
parent 449b8c67d1
commit 50c1ab62ba
2 changed files with 13 additions and 6 deletions

View File

@ -55,11 +55,12 @@ class EnumTypeMeta(ClassTypeMeta):
def construct(cls, bases, attrs):
if not cls._meta.enum:
cls._meta.enum = type(cls.__name__, (PyEnum,), attrs)
return super(EnumTypeMeta, cls).construct(bases, {})
return super(EnumTypeMeta, cls).construct(bases, cls._meta.enum.__members__)
def __call__(cls, *args, **kwargs):
if cls is Enum:
return Enum.create(PyEnum(*args, **kwargs))
if cls._meta.abstract:
return cls.create(PyEnum(*args, **kwargs))
return super(EnumTypeMeta, cls).__call__(*args, **kwargs)
def create(cls, python_enum):
@ -71,6 +72,3 @@ class EnumTypeMeta(ClassTypeMeta):
class Enum(six.with_metaclass(EnumTypeMeta, TypeProxy)):
class Meta:
abstract = True
def __getattr__(self, name):
return getattr(self._meta.enum, name)

View File

@ -50,3 +50,12 @@ def test_enum_from_builtin_enum():
]
assert isinstance(RGB(name='field_name').as_field(), Field)
assert isinstance(RGB(name='field_name').as_argument(), Argument)
def test_enum_value_from_class():
class RGB(Enum):
RED = 1
GREEN = 2
BLUE = 3
assert RGB.RED.value == 1