question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow load to add missing fields with default values

See original GitHub issue

When I have something like this:

from marshmallow import Schema, fields

class UserSchema(Schema):
    name = fields.Str(required=True)
    email = fields.Email(required=True)
    created_at = fields.DateTime()
    some_other = fields.Str(default='This is something else')
    
user_data = {
    'created_at': '2014-08-11T05:26:03.869245',
    'email': u'ken@yahoo.com',
    'name': u'Ken'
}
schema = UserSchema()
result = schema.load(user_data)
pprint(result)

I currently get:

{'created_at': datetime.datetime(2014, 8, 11, 5, 26, 3, 869245),
 'email': 'ken@yahoo.com',
 'name': 'Ken'}

I would expect that some_other would be added with the default set to This is something else, especially if we are passing this to @post_load to create an object so that I don’t have to have said object also handle setting the default (and potentially getting out of sync).

This is especially handy when using marshmallow to validate and transform the data as necessary on a load before passing it further down the stack to avoid having to add a bunch of if key in obj statements.

Currently I end up doing:

result = schema.dump(schema.load(user_data))
pprint(result)

Which does have the desired result:

{'created_at': '2014-08-11T05:26:03.869245+00:00',
 'email': 'ken@yahoo.com',
 'name': 'Ken',
 'some_other': 'This is something else'}

but is doing a lot of extra work that shouldn’t be necessary.


Is there already a recipe for doing something like this with a @post_load decorator? If so I don’t mind using that.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

18reactions
deckar01commented, Apr 17, 2018

It does seem arbitrary for default to only be used for serialization. It isn’t immediately obvious that missing is the deserialization complement. It would be nice if there was a little more symmetry here. Maybe something like default_load, default_dump, and default to set both at once.

16reactions
bertjwregeercommented, Apr 17, 2018

If only I had the nagging feeling to check the docs for Field before I opened this issue and found missing.

Sorry for the noise!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Protobuf: feature of adding missing fields with default values
Make Protobuf fields that are not serialized on the wire (missing in capture files) to be displayed with default values.
Read more >
Python, Pandas: Add default for missing values - Stack Overflow
Python, Pandas: Add default for missing values - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational ...
Read more >
Working with missing data — pandas 1.5.2 documentation
You can insert missing values by simply assigning to containers. The actual missing value used will be chosen based on the dtype. For...
Read more >
Language Guide (proto3) | Protocol Buffers - Google Developers
Defining A Message Type; Scalar Value Types; Default Values; Enumerations; Using Other Message Types; Nested Types; Updating A Message Type; Unknown Fields ......
Read more >
Filling Default Values Using Mapping - Qlikview Cookbook
MAPPING LOAD null(), 'MISSING!' AutoGenerate 1;. Next we connect fields to the mapping tables with MAP USING statements.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found