Skip to content

Handling optional fields and None values for HashModels #562

Closed
@kantholtz

Description

@kantholtz

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 #38, #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:

  1. Is this intended behaviour? If so, a disclaimer should be added to the documentation and this issue can be closed.
  2. It works for JsonModel instances. Is the use of the HashModel discouraged in general?
  3. 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions