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

only argument inconsistent between Nested(S, many=True) and List(Nested(S)) #946

Closed
rooterkyberian opened this issue Sep 11, 2018 · 2 comments · Fixed by #1229
Closed

only argument inconsistent between Nested(S, many=True) and List(Nested(S)) #946

rooterkyberian opened this issue Sep 11, 2018 · 2 comments · Fixed by #1229

Comments

@rooterkyberian
Copy link
Contributor

rooterkyberian commented Sep 11, 2018

from pprint import pprint

from marshmallow import Schema
from marshmallow.fields import Integer, List, Nested, String


class Child(Schema):
    name = String()
    age = Integer()


class Family(Schema):
    children = List(Nested(Child))


class Family2(Schema):
    children = Nested(Child, many=True)

family = {'children':[
    {'name': 'Tommy', 'age': 12},
    {'name': 'Lily', 'age': 15},
]}

pprint(Family( only=['children.name']).dump(family).data)
pprint(Family2( only=['children.name']).dump(family).data)

returns

{'children': [{'age': 12, 'name': 'Tommy'}, {'age': 15, 'name': 'Lily'}]}
{'children': [{'name': 'Tommy'}, {'name': 'Lily'}]}

tested with marshmallow 2.15.4

The same applies to exclude argument.

@rooterkyberian
Copy link
Contributor Author

rooterkyberian commented Sep 11, 2018

For now I'm using following workaround:

class ListFix(List):
    @property
    def only(self):
        return getattr(self.container, 'only')

    @only.setter
    def only(self, new_options):
        original_options = getattr(self.container, 'only', ())
        if original_options:
            new_options &= type(new_options)(original_options)
        setattr(self.container, 'only', new_options)


class Child(Schema):
    name = String()
    age = Integer()


class Family(Schema):
    children = ListFix(Nested(Child))

the option propagation code was taken from BaseSchema.__apply_nested_option

maybe apply option code (the part I have copied to ListFix property) should be moved to field?

Edited: Just found a nasty side effect of my "fix"

family = {'children': [
    {'name': 'Tommy', 'age': 12},
    {'name': 'Lily', 'age': 15},
]}

for family_schema in (
        Family(),
        Family(only=['children.name']),
        Family2(),
        Family2(only=['children.name']),
):
    pprint(family_schema.dump(family).data)

prints

{'children': [{'name': 'Tommy'}, {'name': 'Lily'}]}
{'children': [{'name': 'Tommy'}, {'name': 'Lily'}]}
{'children': [{'age': 12, 'name': 'Tommy'}, {'age': 15, 'name': 'Lily'}]}
{'children': [{'name': 'Tommy'}, {'name': 'Lily'}]}

@sloria
Copy link
Member

sloria commented Oct 18, 2018

Thanks @rooterkyberian . Let's continue discussion of this in #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
2 participants