diff --git a/tests/unit/test_converters.py b/tests/unit/test_converters.py index 4925940..5b2c695 100644 --- a/tests/unit/test_converters.py +++ b/tests/unit/test_converters.py @@ -347,6 +347,30 @@ def test_convert_not_sequence(self): # Verify assert value == "1" + def test_convert_enum(self): + try: + from enum import Enum + + class OptionType(Enum): + OP1 = "FirstOp" + OP2 = "SecondOp" + OP3 = "ThirdOp" + + # Setup + registry = converters.ConverterFactoryRegistry( + (converters.StandardConverter(),) + ) + key = converters.keys.Sequence(converters.keys.CONVERT_TO_STRING) + + # Run + converter = registry[key](None) + value = converter(OptionType.OP1) + + # Verify + assert value == "FirstOp" + except ImportError: + pass + def test_eq(self): assert converters.keys.Sequence(0) == converters.keys.Sequence(0) assert not (converters.keys.Sequence(1) == converters.keys.Sequence(0)) diff --git a/uplink/converters/keys.py b/uplink/converters/keys.py index 2ca40d8..8dda471 100644 --- a/uplink/converters/keys.py +++ b/uplink/converters/keys.py @@ -5,6 +5,11 @@ """ # Standard library imports import functools +try: + from enum import Enum +except ImportError: # pragma: no cover + # Enum is added on version 3.4 + Enum = type(None) # Local imports @@ -86,6 +91,8 @@ class Sequence(CompositeKey): def convert(self, converter, value): if isinstance(value, (list, tuple)): return list(map(converter, value)) + elif isinstance(value, Enum) and hasattr(value, 'value'): + return value.value else: return converter(value)