summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorepenet <6771947+epenet@users.noreply.github.com>2022-04-02 21:13:57 +0200
committerGitHub <noreply@github.com>2022-04-02 21:13:57 +0200
commit1451f0f288f5765af8d8d542d9b274ae5a5a7e28 (patch)
tree359be70d04dc9b9f2bc529b9479998b6858ab974
parent2e5d6540e81ae015b5e39503c4f7ed3e2fba93b9 (diff)
downloadvoluptuous-1451f0f288f5765af8d8d542d9b274ae5a5a7e28.tar.gz
Ignore `Enum` if it is unavailable (#454)
-rw-r--r--.github/workflows/tests.yml2
-rw-r--r--voluptuous/tests/tests.py66
-rw-r--r--voluptuous/validators.py7
3 files changed, 42 insertions, 33 deletions
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 9cfed7f..fd90ddd 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -18,6 +18,7 @@ jobs:
- { python-version: "3.8", session: "py38" }
- { python-version: "3.7", session: "py37" }
- { python-version: "3.6", session: "py36" }
+ - { python-version: "2.7", session: "py27" }
steps:
- name: Check out the repository
@@ -29,6 +30,7 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install tox-setuptools-version
+ if: ${{ matrix.session != 'py27' }}
run: |
pip install tox-setuptools-version
diff --git a/voluptuous/tests/tests.py b/voluptuous/tests/tests.py
index 049221b..8b95500 100644
--- a/voluptuous/tests/tests.py
+++ b/voluptuous/tests/tests.py
@@ -1,6 +1,9 @@
import copy
import collections
-from enum import Enum
+try:
+ from enum import Enum
+except ImportError:
+ Enum = None
import os
import sys
@@ -1613,38 +1616,39 @@ def test_any_with_discriminant():
else:
assert False, "Did not raise correct Invalid"
-def test_coerce_enum():
- """Test Coerce Enum"""
- class Choice(Enum):
- Easy = 1
- Medium = 2
- Hard = 3
+if Enum:
+ def test_coerce_enum():
+ """Test Coerce Enum"""
+ class Choice(Enum):
+ Easy = 1
+ Medium = 2
+ Hard = 3
- class StringChoice(str, Enum):
- Easy = "easy"
- Medium = "medium"
- Hard = "hard"
+ class StringChoice(str, Enum):
+ Easy = "easy"
+ Medium = "medium"
+ Hard = "hard"
- schema = Schema(Coerce(Choice))
- string_schema = Schema(Coerce(StringChoice))
+ schema = Schema(Coerce(Choice))
+ string_schema = Schema(Coerce(StringChoice))
- # Valid value
- assert schema(1) == Choice.Easy
- assert string_schema("easy") == StringChoice.Easy
+ # Valid value
+ assert schema(1) == Choice.Easy
+ assert string_schema("easy") == StringChoice.Easy
- # Invalid value
- try:
- schema(4)
- except Invalid as e:
- assert_equal(str(e),
- "expected Choice or one of 1, 2, 3")
- else:
- assert False, "Did not raise Invalid for String"
+ # Invalid value
+ try:
+ schema(4)
+ except Invalid as e:
+ assert_equal(str(e),
+ "expected Choice or one of 1, 2, 3")
+ else:
+ assert False, "Did not raise Invalid for String"
- try:
- string_schema("hello")
- except Invalid as e:
- assert_equal(str(e),
- "expected StringChoice or one of 'easy', 'medium', 'hard'")
- else:
- assert False, "Did not raise Invalid for String"
+ try:
+ string_schema("hello")
+ except Invalid as e:
+ assert_equal(str(e),
+ "expected StringChoice or one of 'easy', 'medium', 'hard'")
+ else:
+ assert False, "Did not raise Invalid for String"
diff --git a/voluptuous/validators.py b/voluptuous/validators.py
index 109342d..bf1ab06 100644
--- a/voluptuous/validators.py
+++ b/voluptuous/validators.py
@@ -4,7 +4,10 @@ import datetime
import sys
from functools import wraps
from decimal import Decimal, InvalidOperation
-from enum import Enum
+try:
+ from enum import Enum
+except ImportError:
+ Enum = None
from voluptuous.schema_builder import Schema, raises, message
from voluptuous.error import (MultipleInvalid, CoerceInvalid, TrueInvalid, FalseInvalid, BooleanInvalid, Invalid,
@@ -104,7 +107,7 @@ class Coerce(object):
return self.type(v)
except (ValueError, TypeError, InvalidOperation):
msg = self.msg or ('expected %s' % self.type_name)
- if not self.msg and issubclass(self.type, Enum):
+ if not self.msg and Enum and issubclass(self.type, Enum):
msg += " or one of %s" % str([e.value for e in self.type])[1:-1]
raise CoerceInvalid(msg)