Skip to content

Unable to instantiate RedisCluster according to pyright #3574

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

Open
trajano opened this issue Mar 26, 2025 · 12 comments
Open

Unable to instantiate RedisCluster according to pyright #3574

trajano opened this issue Mar 26, 2025 · 12 comments
Assignees

Comments

@trajano
Copy link

trajano commented Mar 26, 2025

I got the following error on pyright

 error: Cannot instantiate abstract class "RedisCluster"
    "CommandsProtocol.connection_pool" is not implemented (reportAbstractUsage)

When doing

return RedisCluster(startup_nodes=startup_nodes,  protocol=3)
@vladvildanov
Copy link
Collaborator

vladvildanov commented Mar 27, 2025

@trajano Hi! Which version of client are you using? Could you please double check that class that you're instantiating is a correct one:

from redis.cluster import RedisCluster

@trajano
Copy link
Author

trajano commented Mar 27, 2025

I'm using the asyncio one

from redis.asyncio import Redis
from redis.asyncio.cluster import ClusterNode, RedisCluster

@vladvildanov
Copy link
Collaborator

Which client version are you using? RedisCluster client was never marked as an abstract class, could you please double check if the instance you're creating is actually redis-py RedisCluster? Could you please also try it with latest version of redis-py?

@trajano
Copy link
Author

trajano commented Mar 28, 2025

The pyproject.yaml says "redis>=5.2.1", https://pypi.org/project/redis/

from redis.asyncio import Redis
from redis.asyncio.cluster import ClusterNode, RedisCluster

Image

It's not marked as abstract but pyright deemed it to be abstract because it is missing the implementation of "CommandsProtocol.connection_pool" is not implemented (reportAbstractUsage)

@vladvildanov
Copy link
Collaborator

@trajano Yeah, I totally understand from the exception message. CommandsProtocol.connection_pool isn't a method, it's a property. Also, CommandsProtocol extends Python Protocol and this class isn't instantiable at all. I cannot reproduce this behaviour by just instantiate async RedisCluster class on my local. Could you please verify that pyright isn't a source of issue? Also please share more info about your environment, anything that would help me to reproduce the issue

@trajano
Copy link
Author

trajano commented Mar 31, 2025

I just added a ticket on pyright, but not sure what the rules are for abstract classes in Python, it's not as strict as in Java.

@trajano
Copy link
Author

trajano commented Mar 31, 2025

According to microsoft/pyright#10225 seems like it's an issue with this library.

@vladvildanov
Copy link
Collaborator

@trajano Maybe, but if we would have an issue with instantiating RedisCluster object it would be catched by our tests. That's why I'm asking additional information to understand how to reproduce this issue. Anything about your environment would be appreciated

@trajano
Copy link
Author

trajano commented Mar 31, 2025

@vladvildanov I don't have any issues of instantiating it at run time.

This is primarily a type validation issue which is done on pyright (and according to microsoft/pyright#10225 (comment) it also applies to MyPy as well. The workaround I am doing is to "ignore" the type error.

@trajano
Copy link
Author

trajano commented Apr 1, 2025

From what I can tell to fix this you add

        self._lock: Optional[asyncio.Lock] = None
        # add the following to fix pyright issue
        self.connection_pool: "redis.asyncio.ConnectionPool"

In asyncio/cluster.py line 393

@trajano
Copy link
Author

trajano commented Apr 1, 2025

Python is not my primary language, so I am not sure what kind of impact that would have. Though on a side note, it is kind of annoying that the async APIs return both normal and Awaitable when it should only return awaitables.

@aiguofer
Copy link

I'm running into the same issue... I'll just # type: ignore for now, which is a shame, but hoping typing gets fixed for async redis!

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

4 participants