-
-
Notifications
You must be signed in to change notification settings - Fork 629
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
keep_none
option to skip Field._serialize() on None values
#1096
Comments
Does setting https://marshmallow.readthedocs.io/en/3.0/api_reference.html#marshmallow.fields.Field from marshmallow import fields, Schema
class Test(Schema):
foo = fields.Str(missing=None, default=None)
schema = Test()
obj = schema.load({})
# {'foo': None}
schema.dump(obj)
# {'foo': None} |
@deckar01 Sadly no, for 2 reasons:
From: https://github.com/marshmallow-code/marshmallow/blob/3.0/marshmallow/fields.py#L245 EDIT 1: link to |
Oh, missed that it was an issue for custom fields. Thanks. Ya, the https://marshmallow.readthedocs.io/en/3.0/custom_fields.html#creating-a-field-class |
I didn't even realize it was in most/all of the native Fields. That's actually a bit concerning for me. It means my field serialization is missing some constraints I would like to have. |
@PhilMarsh so is your issue solved by updating your custom fields' |
On a related note: #1381 implements a It seems like you're looking for an analagous API for serialization, like the |
Closing for now. Please comment if this is still an issue |
I have optional attrs in some of my app-level objects.
My app-level objects are fully explicit: missing optional fields are explicitly set to
None
instead of being left unset.I want my Schema to be similarly explicit on dump (ie: never skip fields), while being permissive on load (ie: allow missing fields as
None
).So the behavior I want for my Fields is:
A) on load, missing or
None
is deserialized toNone
.B) on dump,
None
is serialized toNone
.Requirement A is easily satisfied by
missing=None
.Requirement B requires more work. For custom types, I must add an
is None
check to the override ofField._serialize()
.This asymmetry of load and dump behavior is strange to me for a couple reasons:
But dump-time optionality is controlled statically by the Field implementation, forcing all Schemas to accept the same behavior (or use some sort of Field factory to customize behavior). I think it is common to have types which are optional in some Schemas and required in other Schemas.
None
may be a valid attr value on the parent object/Schema, butNone
is not a valid value for my custom Field's data type. For example, given:.bar
is optional,Foo.bar = None
is a valid assignment. ButNone
is not a validBar
object. So aBarField
should not be responsible for serializing aNone
. I would actually expectBarField
to raise someTypeError
-like if it was given aNone
to serialize.My proposal is to add a
keep_none
parameter toField
to make this dump behavior configurable.Behavior is:
None
andkeep_none is True
, then returnNone
as the serialized value and do not callField._serialize()
.This seems similar to #229, except I don't want to skip the
None
Fields; I want to keep them asNone
in the serialized output.EDIT 1: Requirement A is satisfied by
missing=None
(which impliesallow_none=True
), not byallow_none=True
alone.The text was updated successfully, but these errors were encountered: