A carrier's manifest feed has a column with an empty header, so each row arrives with the key "". We map it with data_key="". The schema acts as if no data key had been given: it looks for the attribute name on load and…
A carrier's manifest feed has a column with an empty header, so each row arrives with the key "". We map it with data_key="". The schema acts as if no data key had been given: it looks for the attribute name on load and writes the attribute name on dump.
from marshmallow import Schema, fields
class RowSchema(Schema):
name = fields.String(data_key="")
RowSchema().load({"": "Grace"})
RowSchema().dump({"name": "Grace"})
{"name": "Grace"} # load
{"": "Grace"} # dump
ValidationError: {'': ['Unknown field.']} # load
{'name': 'Grace'} # dump
An empty string is a data key like any other. That holds everywhere the data key is used:
name is then not a valid input key),@validates methods,A field with no data_key keeps using its attribute name.
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
empty-string-data-key-ignored
Changes under src/ are evaluated.