Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions email_validator/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`)."""
Expand Down
4 changes: 1 addition & 3 deletions email_validator/validate_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions tests/test_deliverability.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('[email protected]', 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)
Expand Down