marshmallow-code / marshmallow-code/marshmallow
Non-obvious error when using fields.Nested with schema instance and many
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.2k
- Forks
- 738
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 7
Description
I discovered the hard way that fields.Nested silently ignores the many parameter when it's passed an instance. It's my fault, I can see in the docstring that this behaviour is described, but that didn't stop me wasting a load of time debugging the error. I see that using many=True is no longer recommended anyway, but it's not deprecated yet either - #779 may change that. The issue here isn't how Marshmallow behaves, but that it isn't obvious.
Actual behaviour:
Preferred, works: `fields.List(fields.Nested(Child()))`
Old style with nested class, works: `fields.Nested(Child, many=True)`
Old style with nested instance, broken: `fields.Nested(Child(), many=True)`
When trying the nested instance way, the error you get is unhelpful because it's the result of Marshmallow treating the nested schema as singular:
{'_schema': ['Invalid input type.']}
Confusing things even further, many and a nested instance seem to work when dumping (even though it shouldn't), but not when loading (as described). Since the broken way half works, I'm sure there will be code out there that relies on it.
Pull request to follow.
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 at fields.Nested and compare loading and dumping for the three forms shown in the issue: a nested instance with many=True, a nested class, and fields.List(fields.Nested(...)). Determine the intended handling of the nested-instance case, then add coverage that captures the expected result and the confusing error behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100