Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hset for key=0 raises redis.exceptions.DataError #1337

Closed
AleksMat opened this issue May 6, 2020 · 1 comment
Closed

hset for key=0 raises redis.exceptions.DataError #1337

AleksMat opened this issue May 6, 2020 · 1 comment

Comments

@AleksMat
Copy link
Contributor

AleksMat commented May 6, 2020

Hi,

In the new redis-py version 3.5.0 I have noticed some additional checks in Redis.hset method. Because of these checks the following corner case now fails:

Redis().hset('my-name', 0, 'my-value')

raising an error redis.exceptions.DataError: 'hset' with no key value pairs.

The problem is in this line where not key check is made.

Potential solutions:

  • Changing not key into key is None check. This covers case key=0 but it doesn't raise an error if key='' where it probably should?
  • Changing not key into key is None or not str(key) . I think this should cover all cases but it's an additional casting operation.

Let me know if any of these options makes sense. I would be happy to submit a PR for this.

For now, I'll just make sure that I always cast key argument into string in my code to avoid this issue.

@andymccurdy
Copy link
Contributor

It appears that your first suggestion (comparing key to None) is sufficient. It's actually legal in Redis to have an empty string as a key within a hash. A PR would be great, thanks!

In [3]: r.hset('my-hash', mapping={'foo': 'bar'})                                                                                                                                 
Out[3]: 1

# manually sending commands to a connection to avoid the `hset` checks
In [5]: conn = r.connection_pool.get_connection('_')
In [6]: conn.send_command('HSET', 'my-hash', '', 'empty value')
In [7]: conn.read_response()                                                                                                                                                         
Out[7]: 1

# notice the empty string key...
In [8]: r.hgetall('my-hash')                                                                                                                                                      
Out[8]: {b'foo': b'bar', b'': b'empty value'}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants