From c6cc0a0e367cc4861818da9d3e34cb449c25f469 Mon Sep 17 00:00:00 2001 From: Iris Date: Tue, 1 Sep 2026 10:24:25 -0700 Subject: [PATCH 1/2] move _have_dnspython --- pymongo/asynchronous/srv_resolver.py | 9 --------- pymongo/synchronous/srv_resolver.py | 9 --------- pymongo/uri_parser_shared.py | 11 ++++++++++- test/asynchronous/test_srv_polling.py | 2 +- test/test_srv_polling.py | 2 +- 5 files changed, 12 insertions(+), 21 deletions(-) diff --git a/pymongo/asynchronous/srv_resolver.py b/pymongo/asynchronous/srv_resolver.py index 2d99ef16c8..43135b8aaa 100644 --- a/pymongo/asynchronous/srv_resolver.py +++ b/pymongo/asynchronous/srv_resolver.py @@ -29,15 +29,6 @@ _IS_SYNC = False -def _have_dnspython() -> bool: - try: - import dns # noqa: F401 - - return True - except ImportError: - return False - - # dnspython can return bytes or str from various parts # of its API depending on version. We always want str. def maybe_decode(text: Union[str, bytes]) -> str: diff --git a/pymongo/synchronous/srv_resolver.py b/pymongo/synchronous/srv_resolver.py index de1a0fc9e6..c44c74cbeb 100644 --- a/pymongo/synchronous/srv_resolver.py +++ b/pymongo/synchronous/srv_resolver.py @@ -29,15 +29,6 @@ _IS_SYNC = True -def _have_dnspython() -> bool: - try: - import dns # noqa: F401 - - return True - except ImportError: - return False - - # dnspython can return bytes or str from various parts # of its API depending on version. We always want str. def maybe_decode(text: Union[str, bytes]) -> str: diff --git a/pymongo/uri_parser_shared.py b/pymongo/uri_parser_shared.py index 62d862bcfa..7f9dd49e7c 100644 --- a/pymongo/uri_parser_shared.py +++ b/pymongo/uri_parser_shared.py @@ -33,7 +33,6 @@ ) from urllib.parse import unquote_plus -from pymongo.asynchronous.srv_resolver import _have_dnspython from pymongo.client_options import _parse_ssl_options from pymongo.common import ( INTERNAL_URI_OPTION_NAME_MAP, @@ -47,6 +46,16 @@ if TYPE_CHECKING: from pymongo.pyopenssl_context import SSLContext + +def _have_dnspython() -> bool: + try: + import dns # noqa: F401 + + return True + except ImportError: + return False + + SCHEME = "mongodb://" SCHEME_LEN = len(SCHEME) SRV_SCHEME = "mongodb+srv://" diff --git a/test/asynchronous/test_srv_polling.py b/test/asynchronous/test_srv_polling.py index 74e6ce969e..b8b9129925 100644 --- a/test/asynchronous/test_srv_polling.py +++ b/test/asynchronous/test_srv_polling.py @@ -27,8 +27,8 @@ import pymongo from pymongo import common -from pymongo.asynchronous.srv_resolver import _have_dnspython from pymongo.errors import ConfigurationError +from pymongo.uri_parser_shared import _have_dnspython from test.asynchronous import AsyncPyMongoTestCase, client_knobs, unittest from test.asynchronous.utils import async_wait_until diff --git a/test/test_srv_polling.py b/test/test_srv_polling.py index 4927b08f3d..e01ae1da7f 100644 --- a/test/test_srv_polling.py +++ b/test/test_srv_polling.py @@ -28,7 +28,7 @@ import pymongo from pymongo import common from pymongo.errors import ConfigurationError -from pymongo.synchronous.srv_resolver import _have_dnspython +from pymongo.uri_parser_shared import _have_dnspython from test import PyMongoTestCase, client_knobs, unittest from test.utils import wait_until From 0380557d6810bbc2226081989d49df783ed31748 Mon Sep 17 00:00:00 2001 From: Iris Date: Wed, 2 Sep 2026 11:15:32 -0700 Subject: [PATCH 2/2] move function to further down the file --- pymongo/uri_parser_shared.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pymongo/uri_parser_shared.py b/pymongo/uri_parser_shared.py index 7f9dd49e7c..6f51bcd92e 100644 --- a/pymongo/uri_parser_shared.py +++ b/pymongo/uri_parser_shared.py @@ -47,15 +47,6 @@ from pymongo.pyopenssl_context import SSLContext -def _have_dnspython() -> bool: - try: - import dns # noqa: F401 - - return True - except ImportError: - return False - - SCHEME = "mongodb://" SCHEME_LEN = len(SCHEME) SRV_SCHEME = "mongodb+srv://" @@ -114,6 +105,15 @@ def _have_dnspython() -> bool: ) +def _have_dnspython() -> bool: + try: + import dns # noqa: F401 + + return True + except ImportError: + return False + + def _unquoted_percent(s: str) -> bool: """Check for unescaped percent signs.