Skip to content

Commit

Permalink
PYTHON-4407 Eagerly import dnspython (#1622)
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 committed Apr 26, 2024
1 parent 256f5a2 commit 4ea8fcb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
14 changes: 4 additions & 10 deletions pymongo/srv_resolver.py
Expand Up @@ -17,23 +17,17 @@

import ipaddress
import random
from typing import TYPE_CHECKING, Any, Optional, Union
from typing import Any, Optional, Union

from pymongo._lazy_import import lazy_import
from pymongo.common import CONNECT_TIMEOUT
from pymongo.errors import ConfigurationError

if TYPE_CHECKING:
try:
from dns import resolver

_HAVE_DNSPYTHON = True
else:
try:
resolver = lazy_import("dns.resolver")

_HAVE_DNSPYTHON = True
except ImportError:
_HAVE_DNSPYTHON = False
except ImportError:
_HAVE_DNSPYTHON = False


# dnspython can return bytes or str from various parts
Expand Down
6 changes: 6 additions & 0 deletions test/test_srv_polling.py
Expand Up @@ -345,6 +345,12 @@ def resolver_response():
AssertionError, self.assert_nodelist_change, modified, client, timeout=WAIT_TIME / 2
)

def test_import_dns_resolver(self):
# Regression test for PYTHON-4407
import dns.resolver

self.assertTrue(hasattr(dns.resolver, "resolve"))


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

0 comments on commit 4ea8fcb

Please sign in to comment.