Skip to content

Incorrect typing in async Redis search #2972

Closed as not planned
Closed as not planned
@winstxnhdw

Description

@winstxnhdw

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

5.0.1

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

Arch Linux

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

Incorrect typing in Redis asyncio search. query_params should accept bytes similar to the normal Redis search.

    async def search(
        self,
        query: Union[str, Query],
        query_params: Dict[str, Union[str, int, float]] = None,
    ):

This is the corrected version.

    async def search(
        self,
        query: Union[str, Query],
        query_params: Dict[str, Union[str, int, float, bytes]] = None,
    ):

This is also the incorrect way to do dictionary types because Dict only accepts invariant types. See here. This means you'll get an error even if you implemented the corrected version when you do something like this.

    redis_query_parameters = {
        'vec': Embedding().encode_normalise(request.query).tobytes()
    }

    search_response = await redis.ft(Config.redis_index_name).search(
        redis_query,
        redis_query_parameters
    )

Which forces your users to apply an incorrect type hint to redis_query_parameters to get it to work.

    redis_query_parameters: dict[str, str | int | float | bytes] = {
        'vec': Embedding().encode_normalise(request.query).tobytes()
    }

Ideally, you should write your dict types like so.

T = TypeVar("T", bound=str | float | int | bytes)

class AsyncSearchCommands(SearchCommands):
    async def search(
        self,
        query: Union[str, Query],
        query_params: Dict[str, T] = None,
    ): ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions