Skip to content
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

Inconsistent None items serialization between Nested(…, many=True) and List(Nested(…)) fields #1566

Open
Aeron opened this issue Apr 10, 2020 · 2 comments

Comments

@Aeron
Copy link

Aeron commented Apr 10, 2020

At this point, a Nested field with the many=True parameter and a Nested field wrapped in the List will serialize None items differently: the first one will return a list of empty dictionaries, the latter one will return a list of None.

It’s unclear if it’s intentional or not, but it’s frustrating to say at least because these two concepts presumably imply to be interchangeable. Of course, #779 exists not without reason, but it’s a more complicated matter, and it’ll celebrate its second birthday in a few days.

So the main question here is: should it stay inconsistent by design or one approach is provenly false, and needs to be fixed.

Just in case, it’s easy to reproduce with the following example:

from marshmallow import Schema, fields


class N(Schema):
    a = fields.String()
    b = fields.String()


class S1(Schema):
    nested = fields.Nested(N, many=True)


class S2(Schema):
    nested = fields.List(fields.Nested(N))


schema1 = S1()
schema2 = S2()

data = {
    "nested": [None],
}

result1 = schema1.dump(data)
result2 = schema2.dump(data)

assert result1.get("nested") == [{}]
assert result2.get("nested") == [None]
@Aeron
Copy link
Author

Aeron commented Apr 10, 2020

Ah, I completely overlooked #1497, so it’s a kinda-duplicate, but that issue already closed and has no explicit resolution on the question above. It would be great to have a more distinct view on this subject, if possible. Please, correct me if I missed something.

@deckar01
Copy link
Member

While working on #1498, @Meallia uncovered that Nested(many) does not support enabling allow_none for list items. Schema.load() has no concept of allow_none, so it will always implicitly be true.

#779 (comment)

I think the issue is that changing this behavior for Nested(many) would require a major version bump, but there is a desire to just deprecate Nested(many) instead. Either way the discussion for this should probably continue on #779.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants