redis-om-python icon indicating copy to clipboard operation
redis-om-python copied to clipboard

'NoneType' object is not subscriptable

Open ihormelnykhebronsoft opened this issue 1 year ago • 6 comments

    def parse_command(response, **options):
        commands = {}
        for command in response:
            cmd_dict = {}
>           cmd_name = str_if_bytes(command[0])
E           TypeError: 'NoneType' object is not subscriptable

.venv/lib/python3.10/site-packages/redis/client.py:560: TypeError

-------------

class Task(JsonModel):
    task_id: str = Field(index=True, default="")
    task_tag: str = Field(index=True)
    task_keyword: str = Field(index=True)
    data_json: str = Field(index=False, default="{}")
    task_status: str = Field(index=True)
    task_time: int = Field(index=False, default=0)

When i create a Task (Even did not call any save() stuff). It crashes. I guess something is missing

ihormelnykhebronsoft avatar Jul 02 '23 18:07 ihormelnykhebronsoft

task = Task(
            task_tag=newTag,
            task_keyword=keyword,
            task_status=STATUS_PEND,
            task_json=json.dumps(income_task),
        )

ihormelnykhebronsoft avatar Jul 02 '23 18:07 ihormelnykhebronsoft

I'm having the same issue using find() Traceback:

Traceback (most recent call last):
  File "./venv/lib/python3.11/site-packages/redis/client.py", line 1269, in execute_command
    return conn.retry.call_with_retry(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "./venv/lib/python3.11/site-packages/redis/retry.py", line 46, in call_with_retry
    return do()
           ^^^^
  File "./venv/lib/python3.11/site-packages/redis/client.py", line 1270, in <lambda>
    lambda: self._send_command_parse_response(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "./venv/lib/python3.11/site-packages/redis/client.py", line 1246, in _send_command_parse_response
    return self.parse_response(conn, command_name, **options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "./venv/lib/python3.11/site-packages/redis/client.py", line 1296, in parse_response
    return self.response_callbacks[command_name](response, **options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "./venv/lib/python3.11/site-packages/redis/client.py", line 560, in parse_command
    cmd_name = str_if_bytes(command[0])
                            ~~~~~~~^^^
TypeError: 'NoneType' object is not subscriptable
python-BaseException

Code:

import redis_om
import datetime
from typing import Optional

class Server(redis_om.HashModel):
    hostname: str
    ip: Optional[str]
    port: int
    created: datetime.datetime

if __name__ == '__main__':
    print(Server.find(Server.hostname == 'xx.xx.xx.xx'))

I'm using:

  • Redis server 7.0.8
  • Python 3.11
  • RedisOM Python module 0.2.1
  • Redis Python module 4.6.0
  • All of this on Ubuntu 23.04 Kernel 6.2.0-26

I don't think the code has a problem, because I get the exam same result by using the code given in the docs.

EnzoPB avatar Aug 12 '23 19:08 EnzoPB

Hi, Any solution over this, i'm facing the same issue.

Praj-17 avatar Aug 19 '23 19:08 Praj-17

@Praj-17 I changed to Redis Stack Server. I think the problem was that I was missing the Redisearch module.

EnzoPB avatar Aug 21 '23 16:08 EnzoPB

I face this problem too, and I found that the problem is that the Redis Server we use is Redis Server instead of Redis Stack Server
While I look into docker-compose.yml file of this OM for Python : Flask and a simple domain model Tutorial. The redis image is redis/redis-stack:latest instead of redis:latest.


Start a Redis Stack Server instead of Redis Server will solve this problem !

- docker run -p 6379:6379 -d redis:lastest
+ docker run -p 6379:6379 -d redis/redis-stack:lastest

The code I use to verify this problem is as follow:

from redis_om import get_redis_connection
from redis_om import JsonModel , HashModel , Migrator , Field
from typing import Optional

REDIS_URL = "redis://localhost:6379"
redis = get_redis_connection(url=REDIS_URL)

class UserJsonCache( JsonModel ):
    id: int = Field(index=True)
    name : str = Field(index=True)
    email: str = Field(index=True)
    avatar:Optional[str] =  Field(index=False)

    class Meta:
        database = redis

class UserHashCache( HashModel ):
    id: int = Field(index=True)
    name : str = Field(index=True)
    email: str = Field(index=True)
    avatar:Optional[str] =  Field(index=False)

    class Meta:
        database = redis

Migrator().run()

def test_create_user_json():
    new_user = UserJsonCache(id=1,name="json_user",email="[email protected]",avatar="image_url")
    new_user.save()
    pk = new_user.pk
    assert UserJsonCache.get(pk) == new_user

def test_find_user_json():
    user_be_found = UserJsonCache(id=1,name="json_user",email="[email protected]",avatar="image_url")
    res = UserJsonCache.find( UserJsonCache.id==1 ).first()

    assert res.id == user_be_found.id
    assert res.name == user_be_found.name
    assert res.email == user_be_found.email
    assert res.avatar == user_be_found.avatar
    '''
    We can't use assert `res == user_be_found` because `pk` is different
    '''

def test_create_user_hash():
    new_user = UserHashCache(id=2,name="hash_user",email="[email protected]",avatar="image_url")
    new_user.save()
    pk = new_user.pk
    assert UserHashCache.get(pk) == new_user

def test_find_user_hash():
    user_be_found = UserHashCache(id=2,name="hash_user",email="[email protected]",avatar="image_url")
    res = UserHashCache.find( UserHashCache.id==2 ).first()
    assert res.id == user_be_found.id
    assert res.name == user_be_found.name
    assert res.email == user_be_found.email
    assert res.avatar == user_be_found.avatar

Run well no matter using HashModel or JsonModel 🙌

pytest result pytest result

RedisInsight redis insight

jason810496 avatar Oct 09 '23 17:10 jason810496

@jason810496 By default the RedisJSON module is not being loaded and when you try to run a json command against the local instance, you see image this. However, I have loaded the modules with command: redis-server --loadmodule /opt/redis-stack/lib/rejson.so --loadmodule /opt/redis-stack/lib/redisearch.so --loadmodule /opt/redis-stack/lib/redistimeseries.so this command and still getting the same error. Which redis-stack image are you using?

kubi-ozisik avatar Nov 29 '23 21:11 kubi-ozisik

Going to close to tidy up (pretty clearly a redis/module version issues)

slorello89 avatar Apr 05 '24 15:04 slorello89