question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dump-only fields are passed when unknown = INCLUDE

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
sloriacommented, Oct 16, 2018

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.

2reactions
deckar01commented, Aug 14, 2018

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found