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

AttributeError exception when attempting to deserialize None #123

Closed
RealSalmon opened this Issue Jan 7, 2015 · 5 comments

Comments

Projects
None yet
2 participants
@RealSalmon
Contributor

RealSalmon commented Jan 7, 2015

When attempting to deserialize None into a Schema, an AttributeError exception is thrown.

from marshmallow import Schema


class MySchema(Schema):
    class Meta:
        fields = ('field1', 'field2')


test_dict = {'field1': 'value 1',
             'field2': 'value 2',
             'something': 'hey'}

assert MySchema().load(test_dict).data == {k: test_dict[k] for k in test_dict
                                           if k is not 'something'}
assert MySchema().load({}).data == {}
assert MySchema().load(None).data is None

The final assert throws . . .

Traceback (most recent call last):
  File "test.py", line 16, in <module>
    assert MySchema().load(None).data is None
  File "/Users/bjones/Desktop/marshmallow/lib/python2.7/site-packages/marshmallow/schema.py", line 490, in load
    result, errors = self._do_load(data, many, postprocess=True)
  File "/Users/bjones/Desktop/marshmallow/lib/python2.7/site-packages/marshmallow/schema.py", line 598, in _do_load
    dict_class=self.dict_class
  File "/Users/bjones/Desktop/marshmallow/lib/python2.7/site-packages/marshmallow/fields.py", line 277, in deserialize
    raw_value = data.get(attr_name, missing)
AttributeError: 'NoneType' object has no attribute 'get'

This is with 1.2.0.

Obvously the test case is trivial to work around, but in practice I am trying to deserialize a much larger collection where some of the members may be None.

@sloria

This comment has been minimized.

Member

sloria commented Jan 7, 2015

Thank you for reporting. I see three alternatives to address this:

  1. Allow load to receive None and return an empty dict as data
  2. Allow load to receive None and return None.
  3. If load receives None, raise a useful exception for the user to handle

Thoughts?

@RealSalmon

This comment has been minimized.

Contributor

RealSalmon commented Jan 7, 2015

@sloria Thanks for your response.

I like 2 the best, and see 3 as problematic.

In my present use case where this is causing problems, I am deserializing a list of dictionaries into a schema that has nested schemas. There as cases where the nested objects may indeed be None (e.g. an empty, previously serialized SQL Alchemy relationship to another model), and although I might be convinced otherwise, right now I would expect that to be faithfully represented.

e.g. this json should be able to be deserialized without an execption and have the null, nested object accurately represented in the result.

[
   {
      "field1": "value1",
      "nested1": null
   },
   {
      "field1": "value2",
      "nested1": { "otherfield": "somevalue"}
   }
]
@RealSalmon

This comment has been minimized.

Contributor

RealSalmon commented Jan 7, 2015

Happy to attempt tackling this myself and make a PR, btw . . . but I wanted to discuss a bit first to make sure I'm not thinking about this incorrectely or overlooking some other existing solution.

@sloria

This comment has been minimized.

Member

sloria commented Jan 7, 2015

OK, let's go with approach 2. Thanks for clarifying and offering to make a PR. I will gladly review and merge it!

I think we can consider this a bugfix, so please branch off of 1.2-line.

@RealSalmon

This comment has been minimized.

Contributor

RealSalmon commented Jan 8, 2015

@sloria sloria closed this Jan 10, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment