Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Support for marshamallow 3 #34

Open
wants to merge 7 commits into
base: next-release
Choose a base branch
from
Open
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
42 changes: 30 additions & 12 deletions marshmallow_enum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,19 @@ class EnumField(Field):
default_error_messages = {
'by_name': 'Invalid enum member {input}',
'by_value': 'Invalid enum value {input}',
'must_be_string': 'Enum name must be string'
'must_be_string_or_list': 'Enum must be string or list of strings'
}

def __init__(
self, enum, by_value=False, load_by=None, dump_by=None, error='', *args, **kwargs
self,
enum,
by_value=False,
load_by=None,
dump_by=None,
error='',
allow_none=None,
*args,
**kwargs
):
self.enum = enum
self.by_value = by_value
Expand Down Expand Up @@ -69,17 +77,17 @@ def __init__(
self.load_by = load_by
self.dump_by = dump_by

super(EnumField, self).__init__(*args, **kwargs)
super(EnumField, self).__init__(allow_none=allow_none, *args, **kwargs)

def _serialize(self, value, attr, obj):
def _serialize(self, value, attr, obj, **kwargs):
if value is None:
return None
elif self.dump_by == LoadDumpOptions.value:
return value.value
else:
return value.name

def _deserialize(self, value, attr, data):
def _deserialize(self, value, attr, data, **kwargs):
if value is None:
return None
elif self.load_by == LoadDumpOptions.value:
Expand All @@ -94,13 +102,23 @@ def _deserialize_by_value(self, value, attr, data):
self.fail('by_value', input=value, value=value)

def _deserialize_by_name(self, value, attr, data):
if not isinstance(value, string_types):
self.fail('must_be_string', input=value, name=value)

try:
return getattr(self.enum, value)
except AttributeError:
self.fail('by_name', input=value, name=value)
# str
if isinstance(value, string_types):
try:
return getattr(self.enum, value)
except AttributeError:
self.fail('by_name', input=value, name=value)

# list
elif isinstance(value, list):
try:
return [getattr(self.enum, v) for v in value]
except AttributeError:
self.fail('by_name', input=value, name=value)

# validation
else:
self.fail('must_be_string_or_list', input=value, name=value)

def fail(self, key, **kwargs):
kwargs['values'] = ', '.join([text_type(mem.value) for mem in self.enum])
Expand Down
2 changes: 1 addition & 1 deletion tests/test_enum_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def test_uses_default_error_if_no_custom_provided(self):

class TestRegressions(object):

@pytest.mark.parametrize('bad_value', [object, object(), [], {}, 1, 3.4, False, ()])
@pytest.mark.parametrize('bad_value', [object, object(), {}, 1, 3.4, False, ()])
def test_by_name_must_be_string(self, bad_value):

class SomeEnum(Enum):
Expand Down