typing.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # from __future__ import annotations
  2. from datetime import datetime, timedelta
  3. from typing import (
  4. TYPE_CHECKING,
  5. Any,
  6. Awaitable,
  7. Iterable,
  8. Mapping,
  9. Type,
  10. TypeVar,
  11. Union,
  12. )
  13. from redis.compat import Protocol
  14. if TYPE_CHECKING:
  15. from redis._parsers import Encoder
  16. from redis.asyncio.connection import ConnectionPool as AsyncConnectionPool
  17. from redis.connection import ConnectionPool
  18. Number = Union[int, float]
  19. EncodedT = Union[bytes, memoryview]
  20. DecodedT = Union[str, int, float]
  21. EncodableT = Union[EncodedT, DecodedT]
  22. AbsExpiryT = Union[int, datetime]
  23. ExpiryT = Union[int, timedelta]
  24. ZScoreBoundT = Union[float, str] # str allows for the [ or ( prefix
  25. BitfieldOffsetT = Union[int, str] # str allows for #x syntax
  26. _StringLikeT = Union[bytes, str, memoryview]
  27. KeyT = _StringLikeT # Main redis key space
  28. PatternT = _StringLikeT # Patterns matched against keys, fields etc
  29. FieldT = EncodableT # Fields within hash tables, streams and geo commands
  30. KeysT = Union[KeyT, Iterable[KeyT]]
  31. ChannelT = _StringLikeT
  32. GroupT = _StringLikeT # Consumer group
  33. ConsumerT = _StringLikeT # Consumer name
  34. StreamIdT = Union[int, _StringLikeT]
  35. ScriptTextT = _StringLikeT
  36. TimeoutSecT = Union[int, float, _StringLikeT]
  37. # Mapping is not covariant in the key type, which prevents
  38. # Mapping[_StringLikeT, X] from accepting arguments of type Dict[str, X]. Using
  39. # a TypeVar instead of a Union allows mappings with any of the permitted types
  40. # to be passed. Care is needed if there is more than one such mapping in a
  41. # type signature because they will all be required to be the same key type.
  42. AnyKeyT = TypeVar("AnyKeyT", bytes, str, memoryview)
  43. AnyFieldT = TypeVar("AnyFieldT", bytes, str, memoryview)
  44. AnyChannelT = TypeVar("AnyChannelT", bytes, str, memoryview)
  45. ExceptionMappingT = Mapping[str, Union[Type[Exception], Mapping[str, Type[Exception]]]]
  46. class CommandsProtocol(Protocol):
  47. connection_pool: Union["AsyncConnectionPool", "ConnectionPool"]
  48. def execute_command(self, *args, **options):
  49. ...
  50. class ClusterCommandsProtocol(CommandsProtocol, Protocol):
  51. encoder: "Encoder"
  52. def execute_command(self, *args, **options) -> Union[Any, Awaitable]:
  53. ...