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

Allow default value for fields to be callable. #131

Merged
merged 1 commit into from Jul 12, 2017

Conversation

@NightBlues
Copy link
Contributor

commented Jun 29, 2017

Hi there! As docstring
https://github.com/marshmallow-code/marshmallow/blob/dev/marshmallow/fields.py#L63 says the default value may be callable. I think it's a common use case, f.e. passing list or dict constructor to field.

from marshmallow import Schema, fields
class MySchema(Schema):
    numbers = fields.List(fields.Int, default=list)
@NightBlues

This comment has been minimized.

Copy link
Contributor Author

commented Jul 4, 2017

@sloria, what do you think about this?)

@sloria
Copy link
Member

left a comment

Thanks @NightBlues for the PR. I think this makes sense, but I have a slight concern about the implementation. There's a possibility of swallowing a TypeError raised within the callable. We should probably just mirror the implementation from marshmallow:

                    if callable(self.default):
                        return self.default()
                    else:
                        return self.default
@sloria

This comment has been minimized.

Copy link
Member

commented Jul 10, 2017

Once my comment above has been addressed and tests are added, this should be good to merge.

@NightBlues NightBlues force-pushed the NightBlues:fields_default_callable branch from 541f45b to eea26fb Jul 12, 2017

@NightBlues

This comment has been minimized.

Copy link
Contributor Author

commented Jul 12, 2017

@sloria is that better?

@sloria

sloria approved these changes Jul 12, 2017

@sloria

This comment has been minimized.

Copy link
Member

commented Jul 12, 2017

Looks good to me. Thanks!

@sloria sloria merged commit caf8a77 into marshmallow-code:dev Jul 12, 2017

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.