Skip to content

Commit fb7f2b2

Browse files
committed
make ssl module optional
Signed-off-by: Joel Dice <joel.dice@fermyon.com>
1 parent b94b105 commit fb7f2b2

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

redis/asyncio/client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import copy
33
import inspect
44
import re
5-
import ssl
65
import warnings
76
from typing import (
87
TYPE_CHECKING,
@@ -71,13 +70,21 @@
7170
from redis.typing import ChannelT, EncodableT, KeyT
7271
from redis.utils import (
7372
HIREDIS_AVAILABLE,
73+
SSL_AVAILABLE,
7474
_set_info_logger,
7575
deprecated_function,
7676
get_lib_version,
7777
safe_str,
7878
str_if_bytes,
7979
)
8080

81+
if SSL_AVAILABLE:
82+
import ssl
83+
from ssl import TLSVersion
84+
else:
85+
ssl = None
86+
TLSVersion = None
87+
8188
PubSubHandler = Callable[[Dict[str, str]], Awaitable[None]]
8289
_KeyT = TypeVar("_KeyT", bound=KeyT)
8390
_ArgT = TypeVar("_ArgT", KeyT, EncodableT)
@@ -227,7 +234,7 @@ def __init__(
227234
ssl_ca_certs: Optional[str] = None,
228235
ssl_ca_data: Optional[str] = None,
229236
ssl_check_hostname: bool = False,
230-
ssl_min_version: Optional[ssl.TLSVersion] = None,
237+
ssl_min_version: Optional[TLSVersion] = None,
231238
max_connections: Optional[int] = None,
232239
single_connection_client: bool = False,
233240
health_check_interval: int = 0,

redis/asyncio/cluster.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import collections
33
import random
44
import socket
5-
import ssl
65
import warnings
76
from typing import (
87
Any,
@@ -70,13 +69,21 @@
7069
)
7170
from redis.typing import AnyKeyT, EncodableT, KeyT
7271
from redis.utils import (
72+
SSL_AVAILABLE,
7373
deprecated_function,
7474
dict_merge,
7575
get_lib_version,
7676
safe_str,
7777
str_if_bytes,
7878
)
7979

80+
if SSL_AVAILABLE:
81+
import ssl
82+
from ssl import TLSVersion
83+
else:
84+
ssl = None
85+
TLSVersion = None
86+
8087
TargetNodesT = TypeVar(
8188
"TargetNodesT", str, "ClusterNode", List["ClusterNode"], Dict[Any, "ClusterNode"]
8289
)
@@ -272,7 +279,7 @@ def __init__(
272279
ssl_certfile: Optional[str] = None,
273280
ssl_check_hostname: bool = False,
274281
ssl_keyfile: Optional[str] = None,
275-
ssl_min_version: Optional[ssl.TLSVersion] = None,
282+
ssl_min_version: Optional[TLSVersion] = None,
276283
protocol: Optional[int] = 2,
277284
address_remap: Optional[Callable[[str, int], Tuple[str, int]]] = None,
278285
cache_enabled: bool = False,

redis/asyncio/connection.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@
3030

3131
if SSL_AVAILABLE:
3232
import ssl
33-
from ssl import SSLContext
33+
from ssl import SSLContext, TLSVersion
3434

3535
else:
3636
ssl = None
3737
SSLContext = None
38+
TLSVersion = None
3839

3940
# the functionality is available in 3.11.x but has a major issue before
4041
# 3.11.3. See https://github.com/redis/redis-py/issues/2633
@@ -832,7 +833,7 @@ def __init__(
832833
ssl_ca_certs: Optional[str] = None,
833834
ssl_ca_data: Optional[str] = None,
834835
ssl_check_hostname: bool = False,
835-
ssl_min_version: Optional[ssl.TLSVersion] = None,
836+
ssl_min_version: Optional[TLSVersion] = None,
836837
**kwargs,
837838
):
838839
if not SSL_AVAILABLE:
@@ -903,7 +904,7 @@ def __init__(
903904
ca_certs: Optional[str] = None,
904905
ca_data: Optional[str] = None,
905906
check_hostname: bool = False,
906-
min_version: Optional[ssl.TLSVersion] = None,
907+
min_version: Optional[TLSVersion] = None,
907908
):
908909
if not SSL_AVAILABLE:
909910
raise RedisError("Python wasn't built with SSL support")
@@ -927,7 +928,7 @@ def __init__(
927928
self.ca_data = ca_data
928929
self.check_hostname = check_hostname
929930
self.min_version = min_version
930-
self.context: Optional[ssl.SSLContext] = None
931+
self.context: Optional[SSLContext] = None
931932

932933
def get(self) -> SSLContext:
933934
if not self.context:

0 commit comments

Comments
 (0)