Our document schemas are generated from a spec where every field carries required. The kind discriminator is a constant, and declaring it as fields.Constant("invoice", required=True) blows up before a single document is…
Our document schemas are generated from a spec where every field carries required. The kind discriminator is a constant, and declaring it as fields.Constant("invoice", required=True) blows up before a single document is loaded.
from marshmallow import Schema, fields
class InvoiceSchema(Schema):
kind = fields.Constant("invoice", required=True)
n = fields.Integer()
InvoiceSchema().load({"n": 1})
{"kind": "invoice", "n": 1}
ValueError: 'load_default' must not be set for required fields.
The error is raised by fields.Constant(...) itself, so the class body never finishes.
required=True must not make loading fail when the key is absent: the constant is used. A value in the input is ignored, as it is today for a constant that is not required, and dumping gives the constant.None in the input is still rejected with "Field may not be null." unless the constant itself is None.fields.Integer(required=True, load_default=3) still raises the ValueError above.Adapted from marshmallow-code/marshmallow (MIT).
Visible test command
pytest testsThe repository’s own tests. Run all tests includes them, and so does every submission.
Repository
required-constant-field-raises
Changes under src/ are evaluated.