From 30f7561a8fca1453ac593a975c74d9b4bb3c7b20 Mon Sep 17 00:00:00 2001 From: Maarten Sijm <9739541+mpsijm@users.noreply.github.com> Date: Thu, 12 Jun 2025 11:08:02 +0200 Subject: [PATCH] pyopenssl_context.py: Fix incorrect service_identity error names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When there is a mistake in the certificates configuration, PyMongo would give the following error: ``` pymongo.errors.ServerSelectionTimeoutError: module service_identity has no attribute SICertificateError, Timeout: 30s, Topology Description: <…> ``` Because the `except` block expected non-existing errors, the error gets transformed to "module … has no attribute …". The errors in the `service_identity` have never had the `SI` prefix: https://github.com/pyca/service-identity/blob/18.1.0/src/service_identity/exceptions.py It looks like the errors were imported with an alias before, but this aliasing was (only partially) removed when rewriting to lazy imports in this commit: https://github.com/mongodb/mongo-python-driver/commit/42a08c4a34c1bd58296de1ffa2368396fdb12f8f#diff-b277a2f4cfbb5decab333d0b90a08a4ad64b91fb1691ed8412b15949d1aaceee The lazy imports were removed again in this commit, but the error remained: https://github.com/mongodb/mongo-python-driver/commit/49987e6a8a5217de19a425838e16d5c9674a8841#diff-b277a2f4cfbb5decab333d0b90a08a4ad64b91fb1691ed8412b15949d1aaceee Most likely, the `# type: ignore[misc]` comment can now also be removed. --- pymongo/pyopenssl_context.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pymongo/pyopenssl_context.py b/pymongo/pyopenssl_context.py index 0d4f27cf55..08fe99c889 100644 --- a/pymongo/pyopenssl_context.py +++ b/pymongo/pyopenssl_context.py @@ -420,9 +420,9 @@ def wrap_socket( pyopenssl.verify_ip_address(ssl_conn, server_hostname) else: pyopenssl.verify_hostname(ssl_conn, server_hostname) - except ( # type:ignore[misc] - service_identity.SICertificateError, - service_identity.SIVerificationError, + except ( + service_identity.CertificateError, + service_identity.VerificationError, ) as exc: raise _CertificateError(str(exc)) from None return ssl_conn