marshmallow icon indicating copy to clipboard operation
marshmallow copied to clipboard

Add missing_values parameter to Field

Open sloria opened this issue 4 years ago • 5 comments

Proves the concept proposed in https://github.com/marshmallow-code/marshmallow/issues/713#issuecomment-529154890

Still needs docs and details, but this is ready for review.

TODO:

  • [x] ~Respect missing_values on serialization?~ see discussion
  • [ ] Update Field.__repr__ to include missing_values
  • [ ] Docs

sloria avatar Sep 08 '19 14:09 sloria

LGTM.

Respect missing_values on serialization?

What would that do? I'm tempted to say no. Do we do anything specific with missing already?

lafrech avatar Sep 09 '19 20:09 lafrech

It would make default value get returned for missing values on serialization.

from marshmallow import Schema, fields


class ArtistSchema(Schema):
    name = fields.Str(default="---", missing_values=("",))


sch = ArtistSchema()
print(sch.dump({}))   # =>  {'name': '---'}
print(sch.dump({"name": ""})) # => {'name': ''} now, but maybe should be same as above

Sure, it's not a common use case, but it seems inconsistent to not handle it.

sloria avatar Sep 09 '19 21:09 sloria

OK, I get it.

Maybe it shouldn't be the same parameter.

IIUC, the primary use case for this is an API for which it is complicated to enforce removal of empty-ish values. There is no reason the same constraint would apply to the serialized form of the data.

But the database may have its own constraint. When using SQL, I already felt the need to treat None as missing to avoid returning null (https://github.com/marshmallow-code/marshmallow/issues/229#issuecomment-363250656) and your proposal would solve that. But I didn't want to treat null as missing when deserializing.

Symmetry would recommend default_values, although the name is a bit misleading.

lafrech avatar Sep 09 '19 21:09 lafrech

That's a good point; the client inputs for "no data" may be different from the database's representation.

default_values is indeed misleading. If we go with https://github.com/marshmallow-code/marshmallow/issues/778 , we could do load_default_values and dump_default_values, I guess. Maybe we defer the serialization param for now.

sloria avatar Sep 09 '19 21:09 sloria

default_values is indeed misleading. If we go with https://github.com/marshmallow-code/marshmallow/issues/778 , we could do load_default_values and dump_default_values, I guess. Maybe we defer the serialization param for now.

#1742 is about to close #778.

We could then change the names here. And do the serialization part in here or another PR.

lafrech avatar Apr 08 '21 20:04 lafrech