Skip to content

Best way to receive the return value when the type hint is Union[Awaitable[int], int]? #2399

Open
@hyperstarkkk

Description

@hyperstarkkk

I noticed the return value type hint of redis-py functions are always like: Union[Awaitable[int], int]: (e.g. redis.commands.core.py:L4824)

def hlen(self, name: str) -> Union[Awaitable[int], int]:
    """
    Return the number of elements in hash ``name``

    For more information see https://redis.io/commands/hlen
    """
    return self.execute_command("HLEN", name)

When I receive the value as normal

dict_size = redis.hlen("dict_key")
assert dict_size < 10

Pylance will remind me

Operator "<=" not supported for types "Awaitable[int] | int" and "Literal[10]"
  Operator "<=" not supported for types "Awaitable[int]" and "Literal[10]"

How to let Pylance know I am not calling the function in asynchronous way, so I can mitigate this type checking error?

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