Repository: polygraph-python/polygraph

Issue:
EnumType should define values only using EnumValue

Comments:

Patch:
diff --git a/polygraph/types/definitions.py b/polygraph/types/definitions.py
index 5943fc1..dcec78e 100644
--- a/polygraph/types/definitions.py
+++ b/polygraph/types/definitions.py
@@ -32,12 +32,11 @@ Field = namedtuple(
 class EnumValue:
     __slots__ = ["name", "description", "is_deprecated", "deprecation_reason", "parent"]
 
-    def __init__(self, name, parent, description=None, deprecation_reason=None):
+    def __init__(self, description=None, name=None, deprecation_reason=None):
         self.name = name
         self.description = description
         self.is_deprecated = bool(deprecation_reason)
         self.deprecation_reason = deprecation_reason
-        self.parent = parent
 
     def __repr__(self):
         return "EnumValue('{}')".format(self.name)
diff --git a/polygraph/types/enum.py b/polygraph/types/enum.py
index 110d0ff..de76f83 100644
--- a/polygraph/types/enum.py
+++ b/polygraph/types/enum.py
@@ -9,10 +9,10 @@ from polygraph.types.definitions import EnumValue, TypeKind
 
 class EnumTypeMeta(PolygraphTypeMeta):
     def __new__(cls, name, bases, namespace):
-        for key, desc in namespace.items():
-            if not key.startswith("_") and key != "Type":
-                desc = namespace.get(key)
-                namespace[key] = EnumValue(name=key, description=desc, parent=name)
+        for key, value in namespace.items():
+            if type(value) == EnumValue:
+                value.name = value.name or key
+                value.parent = name
         return super().__new__(cls, name, bases, namespace)
 
 


Test patch:
diff --git a/polygraph/types/tests/test_enum.py b/polygraph/types/tests/test_enum.py
index dabba73..3525570 100644
--- a/polygraph/types/tests/test_enum.py
+++ b/polygraph/types/tests/test_enum.py
@@ -2,19 +2,19 @@ from unittest import TestCase
 
 from polygraph.exceptions import PolygraphValueError
 from polygraph.types.api import typedef
-from polygraph.types.enum import EnumType
+from polygraph.types.enum import EnumType, EnumValue
 
 
 class Colours(EnumType):
-    RED = "The colour of fury"
-    GREEN = "The colour of envy"
-    BLUE = "The colour of sloth"
+    RED = EnumValue("The colour of fury")
+    GREEN = EnumValue("The colour of envy")
+    BLUE = EnumValue("The colour of sloth")
 
 
 class Shapes(EnumType):
-    RECTANGLE = "A quadrangle"
-    SQUARE = "Also a quadrangle"
-    RHOMBUS = "Yet another quadrangle"
+    RECTANGLE = EnumValue("A quadrangle")
+    SQUARE = EnumValue("Also a quadrangle")
+    RHOMBUS = EnumValue("Yet another quadrangle")
 
 
 class EnumTest(TestCase):
@@ -47,3 +47,11 @@ class EnumTest(TestCase):
     def test_enum_type(self):
         colour_type = typedef(Colours)
         self.assertEqual(len(colour_type.enum_values), 3)
+
+    def test_enum_value_name(self):
+        class NamedValue(EnumType):
+            ORIGINAL = EnumValue("Name is ORIGINAL")
+            REPLACED = EnumValue("Name is NOT_REPLACED", name="NOT_REPLACED")
+
+        self.assertEqual(NamedValue.ORIGINAL.name, "ORIGINAL")
+        self.assertEqual(NamedValue.REPLACED.name, "NOT_REPLACED")