Skip to content

Commit 4ea8fcb

Browse files
authored
PYTHON-4407 Eagerly import dnspython (#1622)
1 parent 256f5a2 commit 4ea8fcb

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

pymongo/srv_resolver.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,17 @@
1717

1818
import ipaddress
1919
import random
20-
from typing import TYPE_CHECKING, Any, Optional, Union
20+
from typing import Any, Optional, Union
2121

22-
from pymongo._lazy_import import lazy_import
2322
from pymongo.common import CONNECT_TIMEOUT
2423
from pymongo.errors import ConfigurationError
2524

26-
if TYPE_CHECKING:
25+
try:
2726
from dns import resolver
2827

2928
_HAVE_DNSPYTHON = True
30-
else:
31-
try:
32-
resolver = lazy_import("dns.resolver")
33-
34-
_HAVE_DNSPYTHON = True
35-
except ImportError:
36-
_HAVE_DNSPYTHON = False
29+
except ImportError:
30+
_HAVE_DNSPYTHON = False
3731

3832

3933
# dnspython can return bytes or str from various parts

test/test_srv_polling.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,12 @@ def resolver_response():
345345
AssertionError, self.assert_nodelist_change, modified, client, timeout=WAIT_TIME / 2
346346
)
347347

348+
def test_import_dns_resolver(self):
349+
# Regression test for PYTHON-4407
350+
import dns.resolver
351+
352+
self.assertTrue(hasattr(dns.resolver, "resolve"))
353+
348354

349355
if __name__ == "__main__":
350356
unittest.main()

0 commit comments

Comments
 (0)