Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore Enum if it is unavailable #454

Merged
merged 2 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
66 changes: 35 additions & 31 deletions voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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"
7 changes: 5 additions & 2 deletions voluptuous/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
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,
Expand Down Expand Up @@ -104,7 +107,7 @@ def __call__(self, v):
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)

Expand Down