redis-om-python
redis-om-python copied to clipboard
Handling optional fields and None values for HashModels
I was unable to find an explanation of how None values are handled. I've looked through the docs and hashmodel tests and did not find any explicit things regarding None
values.
>>> from redis_om import HashModel, Field, Migrator
...
... class A(HashModel):
... x: str | None = Field(index=True)
...
... a = A(x=None)
>>> from redis_om import HashModel, Field, Migrator
...
... class A(HashModel):
... x: str | None = Field(index=True)
...
... a = A(x=None)
>>> a.save()
A(pk='01H9N93N9RFHHCQZS59P91572Y', x=None)
>>> A.get(a.pk)
A(pk='01H9N93N9RFHHCQZS59P91572Y', x='')
I think this is an unexpected behaviour (even if it makes sense when thinking from a "low-level" redis pov). It is also inconsistent with redis-py
where saving a None value leads to str(None)
being called which saves the literal string 'None'
to redis.
See also https://github.com/redis/redis-om-python/issues/38, https://github.com/redis/redis-om-python/issues/254
I've opened this issue to discuss a fix (the other open issues have been open for a very long time) and I'm happy to help out code-wise. I have a few questions:
- Is this intended behaviour? If so, a disclaimer should be added to the documentation and this issue can be closed.
- It works for
JsonModel
instances. Is the use of theHashModel
discouraged in general? - If it should be changed, how should it be approached? Obviously, assuming an empty string is always meant to be
None
is a bad idea. Maybe a "magic" (unlikely, random) string could be used instead?