redis/redis-py · error · TypeError

Async Redis client does not support class inclusion

Error message

Async Redis client does not support class inclusion

What it means

Raised by AsyncBasicKeyCommands.__contains__ when you write key in async_client. The sync client maps this to EXISTS, but membership testing in the async client requires an awaited call, which the synchronous in operator cannot perform, so it is blocked.

Source

Thrown at redis/commands/core.py:4812

        """
        pieces: list[str | int] = [key1, key2]
        if len:
            pieces.append("LEN")
        if idx:
            pieces.append("IDX")
        if minmatchlen is not None and minmatchlen != 0:
            pieces.extend(["MINMATCHLEN", minmatchlen])
        if withmatchlen:
            pieces.append("WITHMATCHLEN")
        return self.execute_command("LCS", *pieces, keys=[key1, key2])


class AsyncBasicKeyCommands(BasicKeyCommands):
    def __delitem__(self, name: KeyT):
        raise TypeError("Async Redis client does not support class deletion")

    def __contains__(self, name: KeyT):
        raise TypeError("Async Redis client does not support class inclusion")

    def __getitem__(self, name: KeyT):
        raise TypeError("Async Redis client does not support class retrieval")

    def __setitem__(self, name: KeyT, value: EncodableT):
        raise TypeError("Async Redis client does not support class assignment")

    async def watch(self, *names: KeyT) -> None:
        return super().watch(*names)

    async def unwatch(self) -> None:
        return super().unwatch()


class ListCommands(CommandsProtocol):
    """
    Redis commands for List data type.
    see: https://redis.io/topics/data-types#lists

View on GitHub (pinned to da03cdc7e8)

Solutions

  1. Replace 'key in async_client' with `(await async_client.exists(key)) > 0`.
  2. Use await async_client.exists(key) directly for the boolean check.

Example fix

# before
if 'mykey' in r:
    ...
# after
if await r.exists('mykey'):
    ...
Defensive patterns

Strategy: type-guard

Type guard

import redis.asyncio as aioredis
def is_async_client(client) -> bool:
    return isinstance(client, aioredis.Redis)

Prevention

When it happens

Trigger: Writing `if 'mykey' in async_client:` against an instance of redis.asyncio.Redis.

Common situations: Porting sync code that used `if key in r:` to async; using dict-style containment checks in async code.

Related errors


AI-assisted analysis of redis/redis-py@da03cdc7e8 (2026-08-04). Data as JSON: /data/errors/6ab27e155a2a4ab3.json. Report an issue: GitHub.