From 0f580ecbb85ef2e3b1435d987e33257740aab816 Mon Sep 17 00:00:00 2001 From: tony1233200 Date: Fri, 20 Nov 2026 12:21:19 +0000 Subject: [PATCH] Set mx to None when the deliverability check cannot be completed On the DNS timeout / no-nameservers path, validate_email_deliverability returns no "mx" key, and validate_email only assigned ret.mx when a value was present. The returned ValidatedEmail then had no mx attribute at all, so result.mx raised AttributeError instead of returning None as documented in the README. Assign mx unconditionally (as was already done for mx_fallback_type) and widen its annotation to Optional. Fixes #190 --- email_validator/types.py | 5 +++-- email_validator/validate_email.py | 4 +--- tests/test_deliverability.py | 10 ++++++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/email_validator/types.py b/email_validator/types.py index c285d60..31ea815 100644 --- a/email_validator/types.py +++ b/email_validator/types.py @@ -40,8 +40,9 @@ class ValidatedEmail: smtputf8: bool """If a deliverability check is performed and if it succeeds, a list of (priority, domain) - tuples of MX records specified in the DNS for the domain.""" - mx: list[tuple[int, str]] + tuples of MX records specified in the DNS for the domain. May be None if the deliverability + check could not be completed because of a temporary issue like a timeout.""" + mx: Optional[list[tuple[int, str]]] """If no MX records are actually specified in DNS and instead are inferred, through an obsolete mechanism, from A or AAAA records, the value is the type of DNS record used instead (`A` or `AAAA`).""" diff --git a/email_validator/validate_email.py b/email_validator/validate_email.py index f01cce1..eafe348 100644 --- a/email_validator/validate_email.py +++ b/email_validator/validate_email.py @@ -205,9 +205,7 @@ def validate_email( deliverability_info = validate_email_deliverability( ret.ascii_domain, ret.domain, timeout, dns_resolver ) - mx = deliverability_info.get("mx") - if mx is not None: - ret.mx = mx + ret.mx = deliverability_info.get("mx") ret.mx_fallback_type = deliverability_info.get("mx_fallback_type") return ret diff --git a/tests/test_deliverability.py b/tests/test_deliverability.py index e1307c2..94c6b16 100644 --- a/tests/test_deliverability.py +++ b/tests/test_deliverability.py @@ -66,6 +66,16 @@ def test_deliverability_dns_timeout() -> None: assert response.get("unknown-deliverability") == "timeout" +def test_deliverability_dns_timeout_mx_is_none() -> None: + # When the deliverability check cannot be completed because of a + # timeout, the returned object must still have an mx attribute, + # set to None as documented, rather than no attribute at all + # (which made result.mx raise AttributeError). See issue #190. + result = validate_email('me@timeout.com', dns_resolver=RESOLVER) + assert result.mx is None + assert result.mx_fallback_type is None + + def test_timeout_and_resolver() -> None: with pytest.raises(ValueError, match="It's not valid to pass both timeout and dns_resolver."): validate_email_deliverability('timeout.com', 'timeout.com', timeout=1, dns_resolver=RESOLVER)