Consistency with allow_none with serialization and deserialization
Author: matejspCreated Jul 16, 2025Updated Jul 16, 2026
I saw already comments how to workaround this issue: https://github.com/marshmallow-code/marshmallow/issues/229#issuecomment-363250656 https://github.com/marshmallow-code/marshmallow/issues/229#issuecomment-134387999
But still I would like to marshmallow to be consistent with itself.
Basically you have a schema with allow_none=False and after dumping it you cannot load it back (without manually fixing the output that is not correct according to schema).
import marshmallow
class TransferSchema(marshmallow.Schema):
id = marshmallow.fields.Int(allow_none=False)
str = marshmallow.fields.Str(allow_none=False)
x = TransferSchema()
dumped = x.dump({
'id': None,
'str': None,
})
print(dumped)
loaded = x.load(dumped)
print(loaded)error:
marshmallow.exceptions.ValidationError: {'id': ['Field may not be null.'], 'str': ['Field may not be null.']}I think allow_none should be respected for both on serialize and deserialize.
Source: marshmallow-code/marshmallow