Skip to content

Commit a584257

Browse files
authored
Improvements for more PEP compliance (duplicate imports, raise from, else's) (#125)
1. Removed duplicate imports 2. Added `raise from` for better exception tracing 3. Removed `else` in the check that handles the exception
1 parent 5d72f53 commit a584257

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

email_validator/deliverability.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ def validate_email_deliverability(domain: str, domain_i18n: str, timeout: Option
7272
deliverability_info["mx"] = [(0, str(r)) for r in response]
7373
deliverability_info["mx_fallback_type"] = "AAAA"
7474

75-
except dns.resolver.NoAnswer:
75+
except dns.resolver.NoAnswer as e:
7676
# If there was no MX, A, or AAAA record, then mail to
7777
# this domain is not deliverable, although the domain
7878
# name has other records (otherwise NXDOMAIN would
7979
# have been raised).
80-
raise EmailUndeliverableError(f"The domain name {domain_i18n} does not accept email.")
80+
raise EmailUndeliverableError(f"The domain name {domain_i18n} does not accept email.") from e
8181

8282
# Check for a SPF (RFC 7208) reject-all record ("v=spf1 -all") which indicates
8383
# no emails are sent from this domain (similar to a Null MX record
@@ -96,10 +96,10 @@ def validate_email_deliverability(domain: str, domain_i18n: str, timeout: Option
9696
# No TXT records means there is no SPF policy, so we cannot take any action.
9797
pass
9898

99-
except dns.resolver.NXDOMAIN:
99+
except dns.resolver.NXDOMAIN as e:
100100
# The domain name does not exist --- there are no records of any sort
101101
# for the domain name.
102-
raise EmailUndeliverableError(f"The domain name {domain_i18n} does not exist.")
102+
raise EmailUndeliverableError(f"The domain name {domain_i18n} does not exist.") from e
103103

104104
except dns.resolver.NoNameservers:
105105
# All nameservers failed to answer the query. This might be a problem
@@ -122,6 +122,6 @@ def validate_email_deliverability(domain: str, domain_i18n: str, timeout: Option
122122
# Unhandled conditions should not propagate.
123123
raise EmailUndeliverableError(
124124
"There was an error while checking if the domain name in the email address is deliverable: " + str(e)
125-
)
125+
) from e
126126

127127
return deliverability_info

email_validator/exceptions_types.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ def __getattr__(self, key):
8080

8181
@property
8282
def email(self):
83-
import warnings
8483
warnings.warn("ValidatedEmail.email is deprecated and will be removed, use ValidatedEmail.normalized instead", DeprecationWarning)
8584
return self.normalized
8685

email_validator/syntax.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ def split_email(email):
2626
# Since backslash-escaping is no longer needed because
2727
# the quotes are removed, remove backslash-escaping
2828
# to return in the normalized form.
29-
import re
3029
local_part = re.sub(r"\\(.)", "\\1", local_part)
3130

3231
return local_part, domain_part, True
@@ -72,14 +71,14 @@ def validate_email_local_part(local: str, allow_smtputf8: bool = True, allow_emp
7271
if len(local) == 0:
7372
if not allow_empty_local:
7473
raise EmailSyntaxError("There must be something before the @-sign.")
75-
else:
76-
# The caller allows an empty local part. Useful for validating certain
77-
# Postfix aliases.
78-
return {
79-
"local_part": local,
80-
"ascii_local_part": local,
81-
"smtputf8": False,
82-
}
74+
75+
# The caller allows an empty local part. Useful for validating certain
76+
# Postfix aliases.
77+
return {
78+
"local_part": local,
79+
"ascii_local_part": local,
80+
"smtputf8": False,
81+
}
8382

8483
# Check the length of the local part by counting characters.
8584
# (RFC 5321 4.5.3.1.1)
@@ -191,8 +190,8 @@ def validate_email_local_part(local: str, allow_smtputf8: bool = True, allow_emp
191190
# want to have an unhandled exception later.
192191
try:
193192
local.encode("utf8")
194-
except ValueError:
195-
raise EmailSyntaxError("The email address contains an invalid character.")
193+
except ValueError as e:
194+
raise EmailSyntaxError("The email address contains an invalid character.") from e
196195

197196
# If this address passes only by the quoted string form, re-quote it
198197
# and backslash-escape quotes and backslashes (removing any unnecessary
@@ -330,7 +329,7 @@ def validate_email_domain_name(domain, test_environment=False, globally_delivera
330329
try:
331330
domain = idna.uts46_remap(domain, std3_rules=False, transitional=False)
332331
except idna.IDNAError as e:
333-
raise EmailSyntaxError(f"The part after the @-sign contains invalid characters ({e}).")
332+
raise EmailSyntaxError(f"The part after the @-sign contains invalid characters ({e}).") from e
334333

335334
# The domain part is made up dot-separated "labels." Each label must
336335
# have at least one character and cannot start or end with dashes, which
@@ -374,11 +373,11 @@ def validate_email_domain_name(domain, test_environment=False, globally_delivera
374373
# the length check is applied to a string that is different from the
375374
# one the user supplied. Also I'm not sure if the length check applies
376375
# to the internationalized form, the IDNA ASCII form, or even both!
377-
raise EmailSyntaxError("The email address is too long after the @-sign.")
376+
raise EmailSyntaxError("The email address is too long after the @-sign.") from e
378377

379378
# Other errors seem to not be possible because the call to idna.uts46_remap
380379
# would have already raised them.
381-
raise EmailSyntaxError(f"The part after the @-sign contains invalid characters ({e}).")
380+
raise EmailSyntaxError(f"The part after the @-sign contains invalid characters ({e}).") from e
382381

383382
# Check the syntax of the string returned by idna.encode.
384383
# It should never fail.
@@ -440,7 +439,7 @@ def validate_email_domain_name(domain, test_environment=False, globally_delivera
440439
try:
441440
domain_i18n = idna.decode(ascii_domain.encode('ascii'))
442441
except idna.IDNAError as e:
443-
raise EmailSyntaxError(f"The part after the @-sign is not valid IDNA ({e}).")
442+
raise EmailSyntaxError(f"The part after the @-sign is not valid IDNA ({e}).") from e
444443

445444
# Check for invalid characters after normalization. These
446445
# should never arise. See the similar checks above.
@@ -518,7 +517,7 @@ def validate_email_domain_literal(domain_literal):
518517
try:
519518
addr = ipaddress.IPv4Address(domain_literal)
520519
except ValueError as e:
521-
raise EmailSyntaxError(f"The address in brackets after the @-sign is not valid: It is not an IPv4 address ({e}) or is missing an address literal tag.")
520+
raise EmailSyntaxError(f"The address in brackets after the @-sign is not valid: It is not an IPv4 address ({e}) or is missing an address literal tag.") from e
522521

523522
# Return the IPv4Address object and the domain back unchanged.
524523
return {
@@ -531,7 +530,7 @@ def validate_email_domain_literal(domain_literal):
531530
try:
532531
addr = ipaddress.IPv6Address(domain_literal[5:])
533532
except ValueError as e:
534-
raise EmailSyntaxError(f"The IPv6 address in brackets after the @-sign is not valid ({e}).")
533+
raise EmailSyntaxError(f"The IPv6 address in brackets after the @-sign is not valid ({e}).") from e
535534

536535
# Return the IPv6Address object and construct a normalized
537536
# domain literal.

email_validator/validate_email.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ def validate_email(
4949
if not isinstance(email, str):
5050
try:
5151
email = email.decode("ascii")
52-
except ValueError:
53-
raise EmailSyntaxError("The email address is not valid ASCII.")
52+
except ValueError as e:
53+
raise EmailSyntaxError("The email address is not valid ASCII.") from e
5454

5555
# Split the address into the local part (before the @-sign)
5656
# and the domain part (after the @-sign). Normally, there

0 commit comments

Comments
 (0)