marshmallow: dump-only fields are passed when unknown = INCLUDE

When unknown=INCLUDE, dump-only fields are passed as unknown (and not validated).

This is due to https://github.com/marshmallow-code/marshmallow/pull/865 and is a side-effect of being considered unknown, but is this really what we want?

Modified test:

    def test_dump_only_fields_considered_unknown(self):
        class MySchema(Schema):
            foo = fields.Field(dump_only=True)

        with pytest.raises(ValidationError) as excinfo:
            MySchema().load({'foo': 42})
        err = excinfo.value
        assert 'foo' in err.messages
        assert err.messages['foo'] == ['Unknown field.']

        # Test with unknown=INCLUDE
        data = MySchema(unknown=INCLUDE).load({'foo': 42})
        assert 'foo' not in data  # fail

Excluding them seems inconsistent (why exclude a dump-only field and not a real unknown field if dump-only are treated as unknown?) but less risky.

I’m not saying this is a bug, but I just got surprised by it and it is not explicitly tested, so I figured I’d ask.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (15 by maintainers)

Most upvoted comments

I also find the current behavior to be the most intuitive. Unless there are any strong objections, I’ll plan on merging #981 within the next few days.

What if dump_only accepted RAISE/EXCLUDE values and could be set to EXCLUDE to explicitly opt in to the silently ignore behavior?

My interpretation of “include unknown fields, but only dump foo” when “foo” is loaded from serialized data is that it would pass the schema validation, then fail the field validation.

Result Reason
RAISE Schema error The field key is not declared on the schema.
INCLUDE Field error The field does not allow a value during load.
EXCLUDE No error, No field value The value was discarded during the schema validation.