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

'NoneType' object is not subscriptable #532

Closed
ihormelnykhebronsoft opened this issue Jul 2, 2023 · 7 comments
Closed

'NoneType' object is not subscriptable #532

ihormelnykhebronsoft opened this issue Jul 2, 2023 · 7 comments

Comments

@ihormelnykhebronsoft
Copy link

    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
Copy link
Author

ihormelnykhebronsoft commented Jul 2, 2023

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

@EnzoPB
Copy link

EnzoPB commented Aug 12, 2023

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.

@Praj-17
Copy link

Praj-17 commented Aug 19, 2023

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

@EnzoPB
Copy link

EnzoPB commented Aug 21, 2023

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

@jason810496
Copy link

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="json_user@email.com",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="json_user@email.com",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="hash_user@email.com",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="hash_user@email.com",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

@kubi-ozisik
Copy link

@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?

@slorello89
Copy link
Member

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

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

6 participants