Liii
Liii

Reputation: 41

How to receive the return value when the type hint is Union[Awaitable[int], int]?

My python code is using redis-py to query redis. 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?

Upvotes: 4

Views: 787

Answers (0)

Related Questions