marshmallow-code / marshmallow-code/marshmallow
Default deserialization with Function field
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.2k
- Forks
- 738
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 7
Description
Based on the documentation for the deserialize parameter for `fields.Function`:
- deserialize – A callable from which to retrieve the value. The function must take a single argument value which is the value to be deserialized. It can also optionally take a context argument, which is a dictionary of context variables passed to the deserializer. If no callable is provided then `value` will be passed through unchanged.
I would expect that leaving the parameter out would populate the field with the source value, and that this code would work:
```
import marshmallow
from marshmallow import Schema, fields
class Bar(Schema):
baz = fields.String()
qux = fields.String()
class Foo(Schema):
baz = fields.Function(serialize=lambda x: x['bar']['baz'])
qux = fields.Function(serialize=lambda x: x['bar']['baz'])
@marshmallow.post_load
def post_load(self, data, many, **kwargs):
return {'bar': data}
s = {'baz': 'blue', 'qux': 'orange'}
print(s)
d = Foo().load(s)
print(d)
print(Foo().dumps(d))
```
However, I get the error:
`marshmallow.exceptions.ValidationError: {'baz': ['Unknown field.'], 'qux': ['Unknown field.']}`
It works as I would expect if I do
```
class Foo(Schema):
baz = fields.Function(serialize=lambda x: x['bar']['baz'],
deserialize=lambda x: x)
qux = fields.Function(serialize=lambda x: x['bar']['baz'],
deserialize=lambda x: x)
```
Am I just reading the documentation wrong, or is this a bug?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the deserialization path for fields.Function and reproduce the shown Foo().load(s) example, comparing omitted deserialize with deserialize=lambda x: x. Done when the documented default behavior is either confirmed or corrected and the example loads without Unknown field errors while preserving the expected round trip.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100