1515"""Support for SSL in PyMongo."""
1616from __future__ import annotations
1717
18+ import types
1819import warnings
19- from typing import Optional
20+ from typing import Any , Optional , Union
2021
2122from pymongo .errors import ConfigurationError
2223
6061 BLOCKING_IO_LOOKUP_ERROR = BLOCKING_IO_READ_ERROR
6162
6263 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
6869 else :
6970 # just make them the same as SSL so imports won't error
7071 PYSSLError = _ssl .SSLError
71- PYBLOCKING_IO_ERRORS = ()
72+ PYBLOCKING_IO_ERRORS = _ssl . BLOCKING_IO_ERRORS
7273 PYBLOCKING_IO_READ_ERROR = _ssl .BLOCKING_IO_READ_ERROR
7374 PYBLOCKING_IO_WRITE_ERROR = _ssl .BLOCKING_IO_WRITE_ERROR
7475 PYBLOCKING_IO_LOOKUP_ERROR = BLOCKING_IO_READ_ERROR
@@ -82,14 +83,14 @@ def get_ssl_context(
8283 allow_invalid_hostnames : bool ,
8384 disable_ocsp_endpoint_check : bool ,
8485 is_sync : bool ,
85- ) -> _ssl .SSLContext :
86+ ) -> Union [ _pyssl . SSLContext , _ssl .SSLContext ]: # type: ignore[name-defined]
8687 """Create and return an SSLContext object."""
8788 if is_sync and HAVE_PYSSL :
88- ssl_in_use = _pyssl
89+ ssl_in_use : types . ModuleType = _pyssl
8990 else :
9091 ssl_in_use = _ssl
9192 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 )
9394 if verify_mode != CERT_NONE :
9495 ctx .check_hostname = not allow_invalid_hostnames
9596 else :
@@ -114,9 +115,7 @@ def get_ssl_context(
114115 if ssl_in_use .IS_PYOPENSSL :
115116 raise ConfigurationError ("tlsCRLFile cannot be used with PyOpenSSL" )
116117 # 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 )
120119 ctx .load_verify_locations (crlfile )
121120 if ca_certs is not None :
122121 ctx .load_verify_locations (ca_certs )
0 commit comments