marshmallow-code / marshmallow-code/marshmallow
Empty strings as None (revisited)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.2k
- Forks
- 738
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 7
Description
I'd like to revisit this suggestion - allowing empty strings to be interpreted as None:
https://github.com/marshmallow-code/marshmallow/issues/543
I understand the sentiment of the response: "it's a bad idea to support that". However, that's not sufficient for some use cases.
For example, right now I'm trying to use marshmallow to convert an XML format where null values are sent as empty strings. I don't get to control the XML format.
I think it's entirely reasonable for a Field to interpret a null datestring (or a null decimal string) as None, and control via allow_none whether to pass that. I don't think it's reasonable to fall down and say "the NULL value defined by your format is invalid, and we're not going to allow you to express that". This just forces a lot of ugly preprocessing workarounds onto library users, who need to clutter their code with superfluous, bug-prone value = value or None type declarations... thereby mixing format definition code in the middle of application code, which makes it hard to use marshmallow to write good code for these use cases.
I feel that the right place to define a format is, well, in the Schema class definition. A patch to accomplish this is simple:
- add an attribute to fields.Field, e.g.
null_values = {None} - change fields.Field.deserialize() line 263 as follows:
if getattr(self, 'allow_none', False) is True and value in self.null_values: return None
We can stop there, and allow users to support their nasty unhygienic formats by overriding null_values in a custom subclass... or even take it a step further and allow first-class support for text-only formats by directly setting null_values = {None, ''} for fields.Decimal, fields.DateTime, fields.Date, fields.Time, etc. By Zeus, maybe even fields.Integer could treat empty strings as None!!
I'm happy to submit a PR to that effect, but I'd like to see where the developers' heads are at vis-a-vis using marshmallow for non-JSON formats before wasting anybody's time.
Thanks, and happy new year!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the prior discussion in issue #543 and the 32-comment thread here, then inspect fields.Field.deserialize around the referenced line 263 and the Decimal, DateTime, Date, Time, and Integer field classes. Done means the maintainers agree on the null_values design and supported field scope before implementation begins.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100