Closed wojcikstefan closed 6 years ago
Is there ever a case where we'd want e.g. empty lists to be serialized as []
instead of None
?
We could have another overridable method like serialize_empty
?
I'd try to avoid such a method, especially since "empty" might mean many different things (e.g. people might expect []
, None
, ''
, {}
, etc. to be treated as empty). Having to guess/check documentation to figure out which serialization method a given value will be piped through doesn't sound great.
I guess @thomasst has a point here... In order to make serialization completely flexible and remove as much "magic" (aka special implicit behavior) as possible, we may want to treat all values as equal and always pipe them through field.serialize
... We lose some DRYness, but now that I've pondered it some more I think it's a fair price to pay.
Agreed @thomasst @tsx ?
That works for me.
I don't care too strongly. Although there's one more related topic to think about: required
constraint, which is also tied to the definition of empty-ness and is taken care of at the generic level with an extra overridable method has_value
.
Yea, for a while I thought about only serializing values for which field.has_value
returns True, and either passing the others as-is or hard-setting them to None
, but I now believe it would only make things more confusing.
Before this PR, serializing an object could fail even if its data was clean. For example, if you had an optional
DateTime
field and tried to serialize{'dt_field_name': None}
, you'd getAttributeError: 'NoneType' object has no attribute 'isoformat'
. That's because the overriddenDateTime.serialize
method expects a validdatetime.datetime
value: https://github.com/closeio/cleancat/blob/48b99263e89e7909d305e8d861e6098fdba6476d/cleancat/base.py#L177-L178All the other overridden
serialize
methods (e.g.List.serialize
,Enum.serialize
, etc.) follow the same pattern. I figured this is an issue we can tackle generically and never callfield.serialize(None)
.An alternative would be to fix the overridden methods by prefixing them all with
if value is None: return
, but I think that would lead to a lot of copy-pasting as we create/override moreserialize
methods in the future.