Skip to content

mypy error: Invalid self argument "RedisCluster" to attribute function "register_script" #3712

@GuyEP-Modulate

Description

@GuyEP-Modulate

When our code calls register_script on a RedisCluster script, mypy flags the call as an error because the register_script method is defined like this in ScriptComamnds:

    def register_script(self: "redis.client.Redis", script: ScriptTextT) -> Script:
        # ... lines omitted ...
        return Script(self, script)

I believe this was done because the Script class expects an object of type redis.client.Redis, but I'd imagine this could be solved by making Script accept Union[Redis, RedisCluster] instead.

Steps to Repro

  1. Create a file with these contents:
    from typing import Union
    
    import redis
    from redis.commands.core import Script
    
    
    def load_script(redis_connection: Union[redis.Redis, redis.RedisCluster], script_filename: str) -> Script:
        with open(script_filename) as lua_script_file:
            lua_script_contents = lua_script_file.read()
    
            return redis_connection.register_script(lua_script_contents)
  2. Run mypy on the code.

Expected Results

No error from mypy.

Actual Results

mypy prints this error:

example.py:11: Invalid self argument "RedisCluster" to attribute function "register_script" with type "Callable[[Redis, Union[bytes, str, memoryview[int]]], Script]

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