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

Pylance type checking issues with async redis.hset #3169

Open
epicwhale opened this issue Mar 1, 2024 · 2 comments
Open

Pylance type checking issues with async redis.hset #3169

epicwhale opened this issue Mar 1, 2024 · 2 comments

Comments

@epicwhale
Copy link

Version: What redis-py and what redis version is the issue happening on?
5.0.2

Platform: What platform / version? (For example Python 3.5.1 on Windows 7 / Ubuntu 15.10 / Azure)
3.12

Description: Description of your issue, stack traces from errors and code that reproduces the issue

Pylance throws a type checking issue while trying to use await with hset using a redis.asyncio.client.Redis instance

"int" is not awaitable
  "int" is incompatible with protocol "Awaitable[_T_co@Awaitable]"
    "__await__" is not presentPylance[reportGeneralTypeIssues](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportGeneralTypeIssues)
(method) def hset(
    name: str,
    key: str | None = None,
    value: str | None = None,
    mapping: dict[Unknown, Unknown] | None = None,
    items: list[Unknown] | None = None
) -> (Awaitable[int] | int)
import redis.asyncio as aio_redis
pool = aio_redis.ConnectionPool.from_url(
    f"redis://{settings.redis_host}:{settings.redis_port}/{settings.redis_db}"
)
client = await aio_redis.Redis.from_pool(pool)

await client.hset(key, field, publish_data_json)  # <--- pylance type check error here

Is this working as intended?

@richin13
Copy link

richin13 commented Mar 3, 2024

I have the exact same issue. The async client is not typed and instead relies on the core commands, for example hset defined here:

def hset(
self,
name: str,
key: Optional[str] = None,
value: Optional[str] = None,
mapping: Optional[dict] = None,
items: Optional[list] = None,
) -> Union[Awaitable[int], int]:

Notice the Union[Awaitable[int], int]. Then the async client extends from this alias class:

AsyncHashCommands = HashCommands

Now pyright (and pylance) complain because it still thinks that either of the two options is a possibility, when it actually isn't.

The solution would be to define the actual AsyncHashCommands class and add each method as async def but that sounds like a lot of work 😛

I've decided to live with it and simply # type: ignore it


Edit:

Alternatively, you can use inspect.isawaitable and act accordingly:

if inspect.isawaitable(rset := self._redis.set(key, data)):
    await rset

@JamesTann
Copy link

Related issue here (but non async): #2399

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

3 participants