Skip to content

Commit 6d697e7

Browse files
fix redis str types (#4783)
1 parent 9af49c0 commit 6d697e7

File tree

1 file changed

+201
-26
lines changed

1 file changed

+201
-26
lines changed

third_party/2and3/redis/client.pyi

Lines changed: 201 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
from datetime import timedelta
2-
from typing import Any, Callable, Dict, Iterable, Iterator, List, Mapping, Optional, Sequence, Set, Text, Tuple, Union, overload
2+
from typing import (
3+
Any,
4+
Callable,
5+
Dict,
6+
Generic,
7+
Iterable,
8+
Iterator,
9+
List,
10+
Mapping,
11+
Optional,
12+
Sequence,
13+
Set,
14+
Text,
15+
Tuple,
16+
TypeVar,
17+
Union,
18+
overload,
19+
)
320
from typing_extensions import Literal
421

522
from .connection import ConnectionPool
@@ -38,14 +55,174 @@ def parse_slowlog_get(response, **options): ...
3855
_Value = Union[bytes, float, int, Text]
3956
_Key = Union[Text, bytes]
4057

41-
class Redis(object):
58+
# Lib returns str or bytes depending on Python version and value of decode_responses
59+
_StrType = TypeVar("_StrType", bound=Union[Text, bytes])
60+
61+
class Redis(Generic[_StrType]):
4262
RESPONSE_CALLBACKS: Any
63+
@overload
64+
@classmethod
65+
def from_url(
66+
cls,
67+
url: Text,
68+
host: Optional[Text] = ...,
69+
port: Optional[int] = ...,
70+
db: Optional[int] = ...,
71+
password: Optional[Text] = ...,
72+
socket_timeout: Optional[float] = ...,
73+
socket_connect_timeout: Optional[float] = ...,
74+
socket_keepalive: Optional[bool] = ...,
75+
socket_keepalive_options: Optional[Mapping[str, Union[int, str]]] = ...,
76+
connection_pool: Optional[ConnectionPool] = ...,
77+
unix_socket_path: Optional[Text] = ...,
78+
encoding: Text = ...,
79+
encoding_errors: Text = ...,
80+
charset: Optional[Text] = ...,
81+
errors: Optional[Text] = ...,
82+
decode_responses: Optional[bool] = ...,
83+
retry_on_timeout: bool = ...,
84+
ssl: bool = ...,
85+
ssl_keyfile: Optional[Text] = ...,
86+
ssl_certfile: Optional[Text] = ...,
87+
ssl_cert_reqs: Optional[Union[str, int]] = ...,
88+
ssl_ca_certs: Optional[Text] = ...,
89+
ssl_check_hostname: bool = ...,
90+
max_connections: Optional[int] = ...,
91+
single_connection_client: bool = ...,
92+
health_check_interval: float = ...,
93+
client_name: Optional[Text] = ...,
94+
username: Optional[Text] = ...,
95+
) -> Redis[bytes]: ...
96+
@overload
4397
@classmethod
44-
def from_url(cls, url: Text, db: Optional[int] = ..., **kwargs) -> Redis: ...
98+
def from_url(
99+
cls,
100+
url: Text,
101+
host: Optional[Text] = ...,
102+
port: Optional[int] = ...,
103+
db: Optional[int] = ...,
104+
password: Optional[Text] = ...,
105+
socket_timeout: Optional[float] = ...,
106+
socket_connect_timeout: Optional[float] = ...,
107+
socket_keepalive: Optional[bool] = ...,
108+
socket_keepalive_options: Optional[Mapping[str, Union[int, str]]] = ...,
109+
connection_pool: Optional[ConnectionPool] = ...,
110+
unix_socket_path: Optional[Text] = ...,
111+
encoding: Text = ...,
112+
encoding_errors: Text = ...,
113+
charset: Optional[Text] = ...,
114+
decode_responses: Literal[True] = ...,
115+
errors: Optional[Text] = ...,
116+
retry_on_timeout: bool = ...,
117+
ssl: bool = ...,
118+
ssl_keyfile: Optional[Text] = ...,
119+
ssl_certfile: Optional[Text] = ...,
120+
ssl_cert_reqs: Optional[Union[str, int]] = ...,
121+
ssl_ca_certs: Optional[Text] = ...,
122+
ssl_check_hostname: bool = ...,
123+
max_connections: Optional[int] = ...,
124+
single_connection_client: bool = ...,
125+
health_check_interval: float = ...,
126+
client_name: Optional[Text] = ...,
127+
username: Optional[Text] = ...,
128+
) -> Redis[str]: ...
45129
connection_pool: Any
46130
response_callbacks: Any
131+
@overload
132+
def __new__(
133+
cls,
134+
host: Text = ...,
135+
port: int = ...,
136+
db: int = ...,
137+
password: Optional[Text] = ...,
138+
socket_timeout: Optional[float] = ...,
139+
socket_connect_timeout: Optional[float] = ...,
140+
socket_keepalive: Optional[bool] = ...,
141+
socket_keepalive_options: Optional[Mapping[str, Union[int, str]]] = ...,
142+
connection_pool: Optional[ConnectionPool] = ...,
143+
unix_socket_path: Optional[Text] = ...,
144+
encoding: Text = ...,
145+
encoding_errors: Text = ...,
146+
charset: Optional[Text] = ...,
147+
errors: Optional[Text] = ...,
148+
retry_on_timeout: bool = ...,
149+
ssl: bool = ...,
150+
ssl_keyfile: Optional[Text] = ...,
151+
ssl_certfile: Optional[Text] = ...,
152+
ssl_cert_reqs: Optional[Union[str, int]] = ...,
153+
ssl_ca_certs: Optional[Text] = ...,
154+
ssl_check_hostname: bool = ...,
155+
max_connections: Optional[int] = ...,
156+
single_connection_client: bool = ...,
157+
health_check_interval: float = ...,
158+
client_name: Optional[Text] = ...,
159+
username: Optional[Text] = ...,
160+
) -> Redis[bytes]: ...
161+
@overload
162+
def __new__(
163+
cls,
164+
host: Text = ...,
165+
port: int = ...,
166+
db: int = ...,
167+
password: Optional[Text] = ...,
168+
socket_timeout: Optional[float] = ...,
169+
socket_connect_timeout: Optional[float] = ...,
170+
socket_keepalive: Optional[bool] = ...,
171+
socket_keepalive_options: Optional[Mapping[str, Union[int, str]]] = ...,
172+
connection_pool: Optional[ConnectionPool] = ...,
173+
unix_socket_path: Optional[Text] = ...,
174+
encoding: Text = ...,
175+
encoding_errors: Text = ...,
176+
charset: Optional[Text] = ...,
177+
errors: Optional[Text] = ...,
178+
decode_responses: Literal[True] = ...,
179+
retry_on_timeout: bool = ...,
180+
ssl: bool = ...,
181+
ssl_keyfile: Optional[Text] = ...,
182+
ssl_certfile: Optional[Text] = ...,
183+
ssl_cert_reqs: Optional[Union[str, int]] = ...,
184+
ssl_ca_certs: Optional[Text] = ...,
185+
ssl_check_hostname: bool = ...,
186+
max_connections: Optional[int] = ...,
187+
single_connection_client: bool = ...,
188+
health_check_interval: float = ...,
189+
client_name: Optional[Text] = ...,
190+
username: Optional[Text] = ...,
191+
) -> Redis[str]: ...
192+
@overload
47193
def __init__(
48-
self,
194+
self: Redis[str],
195+
host: Text = ...,
196+
port: int = ...,
197+
db: int = ...,
198+
password: Optional[Text] = ...,
199+
socket_timeout: Optional[float] = ...,
200+
socket_connect_timeout: Optional[float] = ...,
201+
socket_keepalive: Optional[bool] = ...,
202+
socket_keepalive_options: Optional[Mapping[str, Union[int, str]]] = ...,
203+
connection_pool: Optional[ConnectionPool] = ...,
204+
unix_socket_path: Optional[Text] = ...,
205+
encoding: Text = ...,
206+
encoding_errors: Text = ...,
207+
charset: Optional[Text] = ...,
208+
errors: Optional[Text] = ...,
209+
decode_responses: Literal[True] = ...,
210+
retry_on_timeout: bool = ...,
211+
ssl: bool = ...,
212+
ssl_keyfile: Optional[Text] = ...,
213+
ssl_certfile: Optional[Text] = ...,
214+
ssl_cert_reqs: Optional[Union[str, int]] = ...,
215+
ssl_ca_certs: Optional[Text] = ...,
216+
ssl_check_hostname: bool = ...,
217+
max_connections: Optional[int] = ...,
218+
single_connection_client: bool = ...,
219+
health_check_interval: float = ...,
220+
client_name: Optional[Text] = ...,
221+
username: Optional[Text] = ...,
222+
) -> None: ...
223+
@overload
224+
def __init__(
225+
self: Redis[bytes],
49226
host: Text = ...,
50227
port: int = ...,
51228
db: int = ...,
@@ -60,7 +237,7 @@ class Redis(object):
60237
encoding_errors: Text = ...,
61238
charset: Optional[Text] = ...,
62239
errors: Optional[Text] = ...,
63-
decode_responses: bool = ...,
240+
decode_responses: Optional[bool] = ...,
64241
retry_on_timeout: bool = ...,
65242
ssl: bool = ...,
66243
ssl_keyfile: Optional[Text] = ...,
@@ -153,11 +330,11 @@ class Redis(object):
153330
__contains__: Any
154331
def expire(self, name: _Key, time: Union[int, timedelta]) -> bool: ...
155332
def expireat(self, name, when): ...
156-
def get(self, name: _Key) -> Any: ... # Optional[Union[str, bytes]] depending on decode_responses
333+
def get(self, name: _Key) -> Optional[_StrType]: ...
157334
def __getitem__(self, name): ...
158335
def getbit(self, name: _Key, offset: int) -> int: ...
159336
def getrange(self, key, start, end): ...
160-
def getset(self, name, value): ...
337+
def getset(self, name, value) -> Optional[_StrType]: ...
161338
def incr(self, name, amount=...): ...
162339
def incrby(self, name, amount=...): ...
163340
def incrbyfloat(self, name, amount=...): ...
@@ -197,18 +374,18 @@ class Redis(object):
197374
def watch(self, *names): ...
198375
def unlink(self, *names: _Key) -> int: ...
199376
def unwatch(self): ...
200-
def blpop(self, keys: Union[_Value, Iterable[_Value]], timeout: int = ...) -> Optional[Tuple[bytes, bytes]]: ...
201-
def brpop(self, keys: Union[_Value, Iterable[_Value]], timeout: int = ...) -> Optional[Tuple[bytes, bytes]]: ...
377+
def blpop(self, keys: Union[_Value, Iterable[_Value]], timeout: int = ...) -> Optional[Tuple[_StrType, _StrType]]: ...
378+
def brpop(self, keys: Union[_Value, Iterable[_Value]], timeout: int = ...) -> Optional[Tuple[_StrType, _StrType]]: ...
202379
def brpoplpush(self, src, dst, timeout=...): ...
203-
def lindex(self, name: _Key, index: int) -> Optional[bytes]: ...
380+
def lindex(self, name: _Key, index: int) -> Optional[_StrType]: ...
204381
def linsert(
205382
self, name: _Key, where: Literal["BEFORE", "AFTER", "before", "after"], refvalue: _Value, value: _Value
206383
) -> int: ...
207384
def llen(self, name: _Key) -> int: ...
208385
def lpop(self, name): ...
209386
def lpush(self, name: _Value, *values: _Value) -> int: ...
210387
def lpushx(self, name, value): ...
211-
def lrange(self, name: _Key, start: int, end: int) -> List[bytes]: ...
388+
def lrange(self, name: _Key, start: int, end: int) -> List[_StrType]: ...
212389
def lrem(self, name: _Key, count: int, value: _Value) -> int: ...
213390
def lset(self, name: _Key, index: int, value: _Value) -> bool: ...
214391
def ltrim(self, name: _Key, start: int, end: int) -> bool: ...
@@ -228,7 +405,7 @@ class Redis(object):
228405
alpha: bool = ...,
229406
store: None = ...,
230407
groups: bool = ...,
231-
) -> List[bytes]: ...
408+
) -> List[_StrType]: ...
232409
@overload
233410
def sort(
234411
self,
@@ -256,15 +433,13 @@ class Redis(object):
256433
store: _Key,
257434
groups: bool = ...,
258435
) -> int: ...
259-
def scan(
260-
self, cursor: int = ..., match: Optional[_Key] = ..., count: Optional[int] = ...
261-
) -> Tuple[int, List[Any]]: ... # Tuple[int, List[_Key]] depending on decode_responses
262-
def scan_iter(
263-
self, match: Optional[Text] = ..., count: Optional[int] = ...
264-
) -> Iterator[Any]: ... # Iterator[_Key] depending on decode_responses
265-
def sscan(self, name: _Key, cursor: int = ..., match: Text = ..., count: int = ...) -> Tuple[int, List[bytes]]: ...
436+
def scan(self, cursor: int = ..., match: Optional[_Key] = ..., count: Optional[int] = ...) -> Tuple[int, List[_StrType]]: ...
437+
def scan_iter(self, match: Optional[Text] = ..., count: Optional[int] = ...) -> Iterator[_StrType]: ...
438+
def sscan(self, name: _Key, cursor: int = ..., match: Text = ..., count: int = ...) -> Tuple[int, List[_StrType]]: ...
266439
def sscan_iter(self, name, match=..., count=...): ...
267-
def hscan(self, name: _Key, cursor: int = ..., match: Text = ..., count: int = ...) -> Tuple[int, Dict[bytes, bytes]]: ...
440+
def hscan(
441+
self, name: _Key, cursor: int = ..., match: Text = ..., count: int = ...
442+
) -> Tuple[int, Dict[_StrType, _StrType]]: ...
268443
def hscan_iter(self, name, match=..., count=...): ...
269444
def zscan(self, name, cursor=..., match=..., count=..., score_cast_func=...): ...
270445
def zscan_iter(self, name, match=..., count=..., score_cast_func=...): ...
@@ -346,27 +521,27 @@ class Redis(object):
346521
def pfmerge(self, dest: _Key, *sources: _Key) -> bool: ...
347522
def hdel(self, name: _Key, *keys: _Key) -> int: ...
348523
def hexists(self, name: _Key, key: _Key) -> bool: ...
349-
def hget(self, name: _Key, key: _Key) -> Optional[bytes]: ...
350-
def hgetall(self, name: _Key) -> Dict[bytes, bytes]: ...
524+
def hget(self, name: _Key, key: _Key) -> Optional[_StrType]: ...
525+
def hgetall(self, name: _Key) -> Dict[_StrType, _StrType]: ...
351526
def hincrby(self, name: _Key, key: _Key, amount: int = ...) -> int: ...
352527
def hincrbyfloat(self, name: _Key, key: _Key, amount: float = ...) -> float: ...
353-
def hkeys(self, name: _Key) -> List[bytes]: ...
528+
def hkeys(self, name: _Key) -> List[_StrType]: ...
354529
def hlen(self, name: _Key) -> int: ...
355530
def hset(
356531
self, name: _Key, key: Optional[_Key], value: Optional[_Value], mapping: Optional[Mapping[_Value, _Value]] = ...
357532
) -> int: ...
358533
def hsetnx(self, name: _Key, key: _Key, value: _Value) -> int: ...
359534
def hmset(self, name: _Key, mapping: Mapping[_Value, _Value]) -> bool: ...
360-
def hmget(self, name: _Key, keys: Union[_Key, Iterable[_Key]], *args: _Key) -> List[Optional[bytes]]: ...
361-
def hvals(self, name: _Key) -> List[bytes]: ...
535+
def hmget(self, name: _Key, keys: Union[_Key, Iterable[_Key]], *args: _Key) -> List[Optional[_StrType]]: ...
536+
def hvals(self, name: _Key) -> List[_StrType]: ...
362537
def publish(self, channel: _Key, message: _Key) -> int: ...
363538
def eval(self, script, numkeys, *keys_and_args): ...
364539
def evalsha(self, sha, numkeys, *keys_and_args): ...
365540
def script_exists(self, *args): ...
366541
def script_flush(self): ...
367542
def script_kill(self): ...
368543
def script_load(self, script): ...
369-
def register_script(self, script: Union[Text, bytes]) -> Script: ...
544+
def register_script(self, script: Union[Text, _StrType]) -> Script: ...
370545
def pubsub_channels(self, pattern: _Key = ...) -> List[Text]: ...
371546
def pubsub_numsub(self, *args: _Key) -> List[Tuple[Text, int]]: ...
372547
def pubsub_numpat(self) -> int: ...

0 commit comments

Comments
 (0)