15
15
"""Support for SSL in PyMongo."""
16
16
from __future__ import annotations
17
17
18
+ import types
18
19
import warnings
19
- from typing import Optional
20
+ from typing import Any , Optional , Union
20
21
21
22
from pymongo .errors import ConfigurationError
22
23
60
61
BLOCKING_IO_LOOKUP_ERROR = BLOCKING_IO_READ_ERROR
61
62
62
63
if HAVE_PYSSL :
63
- PYSSLError = _pyssl .SSLError
64
- PYBLOCKING_IO_ERRORS = _pyssl .BLOCKING_IO_ERRORS
65
- PYBLOCKING_IO_READ_ERROR = _pyssl .BLOCKING_IO_READ_ERROR
66
- PYBLOCKING_IO_WRITE_ERROR = _pyssl .BLOCKING_IO_WRITE_ERROR
67
- PYBLOCKING_IO_LOOKUP_ERROR = BLOCKING_IO_READ_ERROR
64
+ PYSSLError : Any = _pyssl .SSLError
65
+ PYBLOCKING_IO_ERRORS : Any = _pyssl .BLOCKING_IO_ERRORS
66
+ PYBLOCKING_IO_READ_ERROR : Any = _pyssl .BLOCKING_IO_READ_ERROR
67
+ PYBLOCKING_IO_WRITE_ERROR : Any = _pyssl .BLOCKING_IO_WRITE_ERROR
68
+ PYBLOCKING_IO_LOOKUP_ERROR : Any = BLOCKING_IO_READ_ERROR
68
69
else :
69
70
# just make them the same as SSL so imports won't error
70
71
PYSSLError = _ssl .SSLError
71
- PYBLOCKING_IO_ERRORS = ()
72
+ PYBLOCKING_IO_ERRORS = _ssl . BLOCKING_IO_ERRORS
72
73
PYBLOCKING_IO_READ_ERROR = _ssl .BLOCKING_IO_READ_ERROR
73
74
PYBLOCKING_IO_WRITE_ERROR = _ssl .BLOCKING_IO_WRITE_ERROR
74
75
PYBLOCKING_IO_LOOKUP_ERROR = BLOCKING_IO_READ_ERROR
@@ -82,14 +83,14 @@ def get_ssl_context(
82
83
allow_invalid_hostnames : bool ,
83
84
disable_ocsp_endpoint_check : bool ,
84
85
is_sync : bool ,
85
- ) -> _ssl .SSLContext :
86
+ ) -> Union [ _pyssl . SSLContext , _ssl .SSLContext ]: # type: ignore[name-defined]
86
87
"""Create and return an SSLContext object."""
87
88
if is_sync and HAVE_PYSSL :
88
- ssl_in_use = _pyssl
89
+ ssl_in_use : types . ModuleType = _pyssl
89
90
else :
90
91
ssl_in_use = _ssl
91
92
verify_mode = CERT_NONE if allow_invalid_certificates else CERT_REQUIRED
92
- ctx = _ssl .SSLContext (_ssl .PROTOCOL_SSLv23 )
93
+ ctx = ssl_in_use .SSLContext (ssl_in_use .PROTOCOL_SSLv23 )
93
94
if verify_mode != CERT_NONE :
94
95
ctx .check_hostname = not allow_invalid_hostnames
95
96
else :
@@ -114,9 +115,7 @@ def get_ssl_context(
114
115
if ssl_in_use .IS_PYOPENSSL :
115
116
raise ConfigurationError ("tlsCRLFile cannot be used with PyOpenSSL" )
116
117
# Match the server's behavior.
117
- ctx .verify_flags = getattr ( # type:ignore[attr-defined]
118
- ssl_in_use , "VERIFY_CRL_CHECK_LEAF" , 0
119
- )
118
+ ctx .verify_flags = getattr (ssl_in_use , "VERIFY_CRL_CHECK_LEAF" , 0 )
120
119
ctx .load_verify_locations (crlfile )
121
120
if ca_certs is not None :
122
121
ctx .load_verify_locations (ca_certs )
0 commit comments