Repository: polygraph-python/polygraph

Issue:
Populate EnumValue field for Enum type definiton

Comments:

Patch:
diff --git a/polygraph/types/basic_type.py b/polygraph/types/basic_type.py
index 7e889d1..20dc9b6 100644
--- a/polygraph/types/basic_type.py
+++ b/polygraph/types/basic_type.py
@@ -1,6 +1,6 @@
 from polygraph.exceptions import PolygraphValueError
 from polygraph.types.api import typedef
-from polygraph.types.definitions import TypeDefinition, TypeKind
+from polygraph.types.definitions import EnumValue, TypeDefinition, TypeKind
 from polygraph.utils.trim_docstring import trim_docstring
 
 
@@ -12,6 +12,12 @@ def get_field_list(namespace):
     ]
 
 
+def get_enum_value_list(namespace):
+    return [
+        value for value in namespace.values() if isinstance(value, EnumValue)
+    ]
+
+
 class PolygraphTypeMeta(type):
     def __new__(cls, name, bases, namespace):
         default_description = trim_docstring(namespace.get("__doc__", ""))
@@ -33,7 +39,7 @@ class PolygraphTypeMeta(type):
                 fields=get_field_list(namespace),
                 possible_types=getattr(meta, "possible_types", None),
                 interfaces=None,  # FIXME
-                enum_values=None,  # FIXME
+                enum_values=get_enum_value_list(namespace),
                 input_fields=None,  # FIXME
                 of_type=getattr(meta, "of_type", None)
             )
diff --git a/polygraph/types/definitions.py b/polygraph/types/definitions.py
index 1d0008a..5943fc1 100644
--- a/polygraph/types/definitions.py
+++ b/polygraph/types/definitions.py
@@ -29,6 +29,20 @@ Field = namedtuple(
 )
 
 
+class EnumValue:
+    __slots__ = ["name", "description", "is_deprecated", "deprecation_reason", "parent"]
+
+    def __init__(self, name, parent, description=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)
+
+
 TypeDefinition = namedtuple(
     "TypeDefinition",
     [
diff --git a/polygraph/types/enum.py b/polygraph/types/enum.py
index 42efa7f..110d0ff 100644
--- a/polygraph/types/enum.py
+++ b/polygraph/types/enum.py
@@ -4,21 +4,7 @@ from polygraph.types.basic_type import (
     PolygraphOutputType,
     PolygraphTypeMeta,
 )
-from polygraph.types.definitions import TypeKind
-
-
-class EnumValue:
-    __slots__ = ["name", "description", "is_deprecated", "deprecation_reason", "parent"]
-
-    def __init__(self, name, parent, description=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)
+from polygraph.types.definitions import EnumValue, TypeKind
 
 
 class EnumTypeMeta(PolygraphTypeMeta):


Test patch:
diff --git a/polygraph/types/tests/test_enum.py b/polygraph/types/tests/test_enum.py
index c59940c..dabba73 100644
--- a/polygraph/types/tests/test_enum.py
+++ b/polygraph/types/tests/test_enum.py
@@ -1,6 +1,7 @@
 from unittest import TestCase
 
 from polygraph.exceptions import PolygraphValueError
+from polygraph.types.api import typedef
 from polygraph.types.enum import EnumType
 
 
@@ -10,6 +11,12 @@ class Colours(EnumType):
     BLUE = "The colour of sloth"
 
 
+class Shapes(EnumType):
+    RECTANGLE = "A quadrangle"
+    SQUARE = "Also a quadrangle"
+    RHOMBUS = "Yet another quadrangle"
+
+
 class EnumTest(TestCase):
 
     def test_simple_enum(self):
@@ -29,3 +36,14 @@ class EnumTest(TestCase):
         self.assertEqual(Colours(Colours.RED), Colours.RED)
         with self.assertRaises(PolygraphValueError):
             Colours("RED")
+
+    def test_enum_values_dont_mix(self):
+        with self.assertRaises(PolygraphValueError):
+            Colours(Shapes.RECTANGLE)
+
+        with self.assertRaises(PolygraphValueError):
+            Shapes(Colours.BLUE)
+
+    def test_enum_type(self):
+        colour_type = typedef(Colours)
+        self.assertEqual(len(colour_type.enum_values), 3)