dump-only fields are passed when unknown = INCLUDE
See original GitHub issueWhen 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.
Issue Analytics
- State:
- Created 5 years ago
- Comments:15 (15 by maintainers)
Top Results From Across the Web
Quickstart — marshmallow 3.19.0 documentation
When loading, dump-only fields are considered unknown. If the unknown option is set to INCLUDE , values with keys corresponding to those fields...
Read more >python - SQLAlchemy @property causes 'Unknown Field' error ...
In marshmallow 2, unknown or dump_only fields are ignored from input. Unless the user decides to add his own validation to error on...
Read more >marshmallow -- simplified object serialization | We all are data.
When loading, dump-only fields are considered unknown. If the unknown option is set to INCLUDE , values with keys corresponding to those ...
Read more >marshmallow - Read the Docs
Use a Nested field to represent the relationship, passing in a nested schema class. from marshmallow import Schema, fields, pprint class UserSchema(Schema):.
Read more >great_expectations.marshmallow__shade.schema
unknown – Whether to exclude, include, or raise an error for unknown fields in the data. Use EXCLUDE , INCLUDE or RAISE ....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub 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
acceptedRAISE
/EXCLUDE
values and could be set toEXCLUDE
to explicitly opt in to the silently ignore behavior?