Skip to content

Commit e6e8f07

Browse files
authored
Lazy load dns.resolver (#93)
I usually `email_validator` through `pydantic` which doesn't check deliverability. The import of email_validator always trigger the import of `dns.resolver` which will try to import `httpx`, `requests`, etc. This import time can impact negatively the startup time of some apps. I propose here to postpone the import of `.deliverability` to the first time it is needed.
1 parent 364d2d1 commit e6e8f07

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

email_validator/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
# -*- coding: utf-8 -*-
22

33
# Export the main method, helper methods, and the public data types.
4-
from .validate_email import validate_email # noqa: F401
5-
from .deliverability import caching_resolver # noqa: F401
64
from .exceptions_types import * # noqa: F401,F403
5+
from .validate_email import validate_email # noqa: F401
6+
7+
8+
def caching_resolver(*args, **kwargs):
9+
# Lazy load `deliverability` as it is slow to import (due to dns.resolver)
10+
from .deliverability import caching_resolver
11+
12+
return caching_resolver(*args, **kwargs)
13+
714

815
# These global attributes are a part of the library's API and can be
916
# changed by library users.

email_validator/validate_email.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from .exceptions_types import EmailSyntaxError, ValidatedEmail
22
from .syntax import validate_email_local_part, validate_email_domain_part, get_length_reason
3-
from .deliverability import validate_email_deliverability
43
from .rfc_constants import EMAIL_MAX_LENGTH
54

65

@@ -115,6 +114,9 @@ def validate_email(
115114
if check_deliverability and not test_environment:
116115
# Validate the email address's deliverability using DNS
117116
# and update the return dict with metadata.
117+
118+
# Lazy load `deliverability` as it is slow to import (due to dns.resolver)
119+
from .deliverability import validate_email_deliverability
118120
deliverability_info = validate_email_deliverability(
119121
ret["domain"], ret["domain_i18n"], timeout, dns_resolver
120122
)

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ keywords = email address validator
2424
[options]
2525
packages = find:
2626
install_requires =
27-
dnspython>=2.0.0
27+
dnspython>=2.0.0 # optional if deliverability check isn't needed
2828
idna>=2.0.0
2929
python_requires = >=3.6
3030

0 commit comments

Comments
 (0)