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

Provide reasonable error when first argument to EnumField is not an Enum #28

@MattF-NSIDC

Description

@MattF-NSIDC

Hello! I made a stupid mistake refactoring and didn't realize it. The error message didn't help :)

Modified example from front page:

from marshmallow import Schema
from enum import Enum
from marshmallow_enum import EnumField


class Something:
   foo = 1

class StopLight(Enum):
    green = 1
    yellow = 2
    red = 3

class TrafficStop(Schema):
    light_color = EnumField(Something)

schema = TrafficStop()

print(schema.dump({'light_color': StopLight.red}).data)
print(schema.load({'light_color': 'red'}).data)

Expected failure on schema instantiation:

RuntimeError: Received "Something" which is not of type Enum

Actually got failure on schema.load call:

{'light_color': 'red'}
Traceback (most recent call last):
  File "/opt/miniconda/envs/hermes-api/lib/python3.7/site-packages/marshmallow_enum/__init__.py", line 95, in _deserialize_by_name
    return getattr(self.enum, value)
AttributeError: type object 'Something' has no attribute 'red'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 20, in <module>
    print(schema.load({'light_color': 'red'}).data)
  File "/opt/miniconda/envs/hermes-api/lib/python3.7/site-packages/marshmallow/schema.py", line 580, in load
    result, errors = self._do_load(data, many, partial=partial, postprocess=True)
  File "/opt/miniconda/envs/hermes-api/lib/python3.7/site-packages/marshmallow/schema.py", line 660, in _do_load
    index_errors=self.opts.index_errors,
  File "/opt/miniconda/envs/hermes-api/lib/python3.7/site-packages/marshmallow/marshalling.py", line 295, in deserialize
    index=(index if index_errors else None)
  File "/opt/miniconda/envs/hermes-api/lib/python3.7/site-packages/marshmallow/marshalling.py", line 68, in call_and_store
    value = getter_func(data)
  File "/opt/miniconda/envs/hermes-api/lib/python3.7/site-packages/marshmallow/marshalling.py", line 288, in <lambda>
    data
  File "/opt/miniconda/envs/hermes-api/lib/python3.7/site-packages/marshmallow/fields.py", line 265, in deserialize
    output = self._deserialize(value, attr, data)
  File "/opt/miniconda/envs/hermes-api/lib/python3.7/site-packages/marshmallow_enum/__init__.py", line 82, in _deserialize
    return self._deserialize_by_name(value, attr, data)
  File "/opt/miniconda/envs/hermes-api/lib/python3.7/site-packages/marshmallow_enum/__init__.py", line 97, in _deserialize_by_name
    self.fail('by_name', input=value, name=value)
  File "/opt/miniconda/envs/hermes-api/lib/python3.7/site-packages/marshmallow_enum/__init__.py", line 100, in fail
    kwargs['values'] = ', '.join([str(mem.value) for mem in self.enum])
TypeError: 'type' object is not iterable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions