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)