marshmallow-code / marshmallow-code/marshmallow

How is deserialization affected by only and partial?

Open
#535 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs
Dominant language
Python
Stars
7.2k
Forks
738
Avg merge
1d 23h
Merged PRs (30d)
7

Description

I was hoping to get some clarification on the differences between `only` and `partial` with respect to deserialization. (This question is similar to #509 which addresses serialization using the same fields.) Based on the docs and the response to that issue, I interpreted it to mean that `only` applies to serialization and `partial` applies to deserialization. However, in testing it looks like `only` also affects deserialization. What am I missing?

``` python
from marshmallow import Schema, fields

class MySchema(Schema):
foo = fields.Field(required=True)
bar = fields.Field(required=True)

only_schema = MySchema(only=('foo', ))
partial_schema = MySchema(partial=True)

print(only_schema.load({'foo': 42, 'bar': 24}))
print(only_schema.load({'foo': 42}))
print(partial_schema.load({'foo': 42, 'bar': 24}))
print(partial_schema.load({'foo': 42}))
```

Output:

``` python
UnmarshalResult(data={u'foo': 42}, errors={})
UnmarshalResult(data={u'foo': 42}, errors={})
UnmarshalResult(data={'foo': 42, 'bar': 24}, errors={})
UnmarshalResult(data={'foo': 42}, errors={})
```

I expected deserialization using `only_schema` to result in an error similar to the example below.

``` python
MySchema().load({'foo':42})
```

Output:

``` python
UnmarshalResult(data={'foo': 42}, errors={'bar': [u'Missing data for required field.']})
```

Ultimately, I'm trying to get away with using the same schema for (de)serialization, but maybe that's not the ideal way to use marshmallow.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read the documentation for `only` and `partial`, then compare their deserialization behavior with the serialization discussion in issue #509. Done means the intended distinction is established and the documentation or issue guidance clearly explains whether one schema should support both operations.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.