diff --git a/pymongo/srv_resolver.py b/pymongo/srv_resolver.py index 6d988cd09..fa880f031 100644 --- a/pymongo/srv_resolver.py +++ b/pymongo/srv_resolver.py @@ -25,10 +25,15 @@ from pymongo.errors import ConfigurationError if TYPE_CHECKING: from dns import resolver -else: - resolver = lazy_import("dns.resolver") -_HAVE_DNSPYTHON = True + _HAVE_DNSPYTHON = True +else: + try: + resolver = lazy_import("dns.resolver") + + _HAVE_DNSPYTHON = True + except ImportError: + _HAVE_DNSPYTHON = False # dnspython can return bytes or str from various parts diff --git a/test/test_client.py b/test/test_client.py index fb56f5057..4679e563b 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -110,7 +110,6 @@ from pymongo.server_description import ServerDescription from pymongo.server_selectors import readable_server_selector, writable_server_selector from pymongo.server_type import SERVER_TYPE from pymongo.settings import TOPOLOGY_TYPE -from pymongo.srv_resolver import _HAVE_DNSPYTHON from pymongo.topology import _ErrorContext from pymongo.topology_description import TopologyDescription from pymongo.write_concern import WriteConcern @@ -455,7 +454,6 @@ class ClientUnitTest(unittest.TestCase): self.assertEqual(clopts.replica_set_name, "newname") self.assertEqual(clopts.read_preference, ReadPreference.SECONDARY_PREFERRED) - @unittest.skipUnless(_HAVE_DNSPYTHON, "DNS-related tests need dnspython to be installed") def test_connection_timeout_ms_propagates_to_DNS_resolver(self): # Patch the resolver. from pymongo.srv_resolver import _resolve @@ -1755,7 +1753,6 @@ class TestClient(IntegrationTest): with self.assertRaises(InvalidOperation): coll.insert_many([{} for _ in range(5)]) - @unittest.skipUnless(_HAVE_DNSPYTHON, "DNS-related tests need dnspython to be installed") def test_service_name_from_kwargs(self): client = MongoClient( "mongodb+srv://user:password@test22.test.build.10gen.cc", @@ -1776,7 +1773,6 @@ class TestClient(IntegrationTest): ) self.assertEqual(client._topology_settings.srv_service_name, "customname") - @unittest.skipUnless(_HAVE_DNSPYTHON, "DNS-related tests need dnspython to be installed") def test_srv_max_hosts_kwarg(self): client = MongoClient("mongodb+srv://test1.test.build.10gen.cc/") self.assertGreater(len(client.topology_description.server_descriptions()), 1) @@ -1787,11 +1783,6 @@ class TestClient(IntegrationTest): ) self.assertEqual(len(client.topology_description.server_descriptions()), 2) - @unittest.skipIf(_HAVE_DNSPYTHON, "dnspython must not be installed") - def test_srv_no_dnspython_error(self): - with self.assertRaisesRegex(ConfigurationError, 'The "dnspython" module must be'): - MongoClient("mongodb+srv://test1.test.build.10gen.cc/") - @unittest.skipIf( client_context.load_balancer or client_context.serverless, "loadBalanced clients do not run SDAM", diff --git a/test/test_dns.py b/test/test_dns.py index 7b74eb3f7..9a78e451d 100644 --- a/test/test_dns.py +++ b/test/test_dns.py @@ -28,7 +28,6 @@ from test.utils import wait_until from pymongo.common import validate_read_preference_tags from pymongo.errors import ConfigurationError from pymongo.mongo_client import MongoClient -from pymongo.srv_resolver import _HAVE_DNSPYTHON from pymongo.uri_parser import parse_uri, split_hosts @@ -65,8 +64,6 @@ class TestDNSSharded(unittest.TestCase): def create_test(test_case): def run_test(self): - if not _HAVE_DNSPYTHON: - raise unittest.SkipTest("DNS tests require the dnspython module") uri = test_case["uri"] seeds = test_case.get("seeds") num_seeds = test_case.get("numSeeds", len(seeds or [])) @@ -161,7 +158,6 @@ create_tests(TestDNSSharded) class TestParsingErrors(unittest.TestCase): - @unittest.skipUnless(_HAVE_DNSPYTHON, "DNS tests require the dnspython module") def test_invalid_host(self): self.assertRaisesRegex( ConfigurationError, @@ -190,7 +186,6 @@ class TestParsingErrors(unittest.TestCase): class TestCaseInsensitive(IntegrationTest): - @unittest.skipUnless(_HAVE_DNSPYTHON, "DNS tests require the dnspython module") def test_connect_case_insensitive(self): client = MongoClient("mongodb+srv://TEST1.TEST.BUILD.10GEN.cc/") self.addCleanup(client.close) diff --git a/test/test_srv_polling.py b/test/test_srv_polling.py index 636487012..18de261da 100644 --- a/test/test_srv_polling.py +++ b/test/test_srv_polling.py @@ -95,8 +95,6 @@ class TestSrvPolling(unittest.TestCase): CONNECTION_STRING = "mongodb+srv://test1.test.build.10gen.cc" def setUp(self): - if not _HAVE_DNSPYTHON: - raise unittest.SkipTest("SRV polling tests require the dnspython module") # Patch timeouts to ensure short rescan SRV interval. self.client_knobs = client_knobs( heartbeat_frequency=WAIT_TIME, @@ -150,6 +148,7 @@ class TestSrvPolling(unittest.TestCase): return True def run_scenario(self, dns_response, expect_change): + self.assertEqual(_HAVE_DNSPYTHON, True) if callable(dns_response): dns_resolver_response = dns_response else: diff --git a/test/test_uri_spec.py b/test/test_uri_spec.py index 8c2fd2d04..33a22330f 100644 --- a/test/test_uri_spec.py +++ b/test/test_uri_spec.py @@ -28,7 +28,6 @@ from test import clear_warning_registry, unittest from pymongo.common import INTERNAL_URI_OPTION_NAME_MAP, validate from pymongo.compression_support import _HAVE_SNAPPY -from pymongo.srv_resolver import _HAVE_DNSPYTHON from pymongo.uri_parser import SRV_SCHEME, parse_uri CONN_STRING_TEST_PATH = os.path.join( @@ -98,8 +97,6 @@ def create_test(test, test_workdir): compressors = (test.get("options") or {}).get("compressors", []) if "snappy" in compressors and not _HAVE_SNAPPY: self.skipTest("This test needs the snappy module.") - if test["uri"].startswith(SRV_SCHEME) and not _HAVE_DNSPYTHON: - self.skipTest("This test needs dnspython package.") valid = True warning = False expected_warning = test.get("warning", False)