33import sys
44import re
55import unicodedata
6- import dns .resolver , dns .exception
7- import idna # implements IDNA 2008; Python's codec is only IDNA 2003
6+ import dns .resolver
7+ import dns .exception
8+ import idna # implements IDNA 2008; Python's codec is only IDNA 2003
89
910
1011# Based on RFC 2822 section 3.2.4 / RFC 5322 section 3.2.3, these
3233if sys .version_info >= (3 ,):
3334 unicode_class = str
3435else :
35- unicode_class = unicode
36+ unicode_class = unicode # noqa: F821
3637
3738 # turn regexes to unicode (because 'ur' literals are not allowed in Py3)
3839 ATEXT = ATEXT .decode ("ascii" )
@@ -62,10 +63,13 @@ def validate_email(
6263 allow_smtputf8 = True ,
6364 allow_empty_local = False ,
6465 check_deliverability = True ,
65- timeout = DEFAULT_TIMEOUT ):
66-
67- """Validates an email address, raising an EmailNotValidError if the address is not valid or returning a dict of information
68- when the address is valid. The email argument can be a str or a bytes instance, but if bytes it must be ASCII-only."""
66+ timeout = DEFAULT_TIMEOUT ,
67+ ):
68+ """
69+ Validates an email address, raising an EmailNotValidError if the address is not valid or returning a dict of
70+ information when the address is valid. The email argument can be a str or a bytes instance,
71+ but if bytes it must be ASCII-only.
72+ """
6973
7074 # Allow email to be a str or bytes instance. If bytes,
7175 # it must be ASCII because that's how the bytes work
@@ -82,7 +86,7 @@ def validate_email(
8286 raise EmailSyntaxError ("The email address is not valid. It must have exactly one @-sign." )
8387
8488 # Prepare a dict to return on success.
85- ret = { }
89+ ret = {}
8690
8791 # Validate the email address's local part syntax and update the return
8892 # dict with metadata.
@@ -137,7 +141,9 @@ def validate_email_local_part(local, allow_smtputf8=True, allow_empty_local=Fals
137141 m = re .match (DOT_ATOM_TEXT_UTF8 + "$" , local )
138142 if not m :
139143 # It's not a valid internationalized address either. Report which characters were not valid.
140- bad_chars = ', ' .join (sorted (set ( c for c in local if not re .match (u"[" + (ATEXT if not allow_smtputf8 else ATEXT_UTF8 ) + u"]" , c ) )))
144+ bad_chars = ', ' .join (sorted (set (
145+ c for c in local if not re .match (u"[" + (ATEXT if not allow_smtputf8 else ATEXT_UTF8 ) + u"]" , c )
146+ )))
141147 raise EmailSyntaxError ("The email address contains invalid characters before the @-sign: %s." % bad_chars )
142148
143149 # It would be valid if internationalized characters were allowed by the caller.
@@ -229,7 +235,9 @@ def validate_email_domain_part(domain):
229235 if "." not in domain :
230236 raise EmailSyntaxError ("The domain name %s is not valid. It should have a period." % domain_i18n )
231237 if not re .search (r"[A-Za-z]$" , domain ):
232- raise EmailSyntaxError ("The domain name %s is not valid. It is not within a valid top-level domain." % domain_i18n )
238+ raise EmailSyntaxError (
239+ "The domain name %s is not valid. It is not within a valid top-level domain." % domain_i18n
240+ )
233241
234242 # Return the IDNA ASCII-encoded form of the domain, which is how it
235243 # would be transmitted on the wire (except when used with SMTPUTF8
@@ -293,7 +301,9 @@ def validate_email_deliverability(domain, domain_i18n, timeout=DEFAULT_TIMEOUT):
293301
294302 except Exception as e :
295303 # Unhandled conditions should not propagate.
296- raise EmailUndeliverableError ("There was an error while checking if the domain name in the email address is deliverable: " + str (e ))
304+ raise EmailUndeliverableError (
305+ "There was an error while checking if the domain name in the email address is deliverable: " + str (e )
306+ )
297307
298308 return {
299309 "mx" : mtas ,
@@ -302,7 +312,8 @@ def validate_email_deliverability(domain, domain_i18n, timeout=DEFAULT_TIMEOUT):
302312
303313
304314def main ():
305- import sys , json
315+ import sys
316+ import json
306317
307318 if sys .argv [- 1 ] == "--tests" :
308319 # Pass a file of valid/invalid email addresses.
@@ -311,8 +322,10 @@ def main():
311322 for line in sys .stdin :
312323 # Strip newlines and skip blank lines and comments.
313324 line = line .strip ()
314- if line == "" or line [0 ] == "#" : continue
315- if sys .version_info < (3 ,): line = line .decode ("utf8" ) # assume utf8 in input
325+ if line == "" or line [0 ] == "#" :
326+ continue
327+ if sys .version_info < (3 ,):
328+ line = line .decode ("utf8" ) # assume utf8 in input
316329
317330 # Pick up "[valid]"/"[invalid]" lines.
318331 if line == "[valid]" :
@@ -328,12 +341,12 @@ def main():
328341 try :
329342 email = line
330343 validate_email (email , check_deliverability = False )
331- if correct_answer == False :
344+ if correct_answer is False :
332345 # Should have failed.
333346 print (email , "was recognized as valid." )
334347 failed += 1
335348 except EmailNotValidError as e :
336- if correct_answer == True :
349+ if correct_answer is True :
337350 # Should have passed.
338351 print (email , e )
339352 failed += 1
@@ -346,7 +359,8 @@ def main():
346359 for line in sys .stdin :
347360 try :
348361 email = line .strip ()
349- if sys .version_info < (3 ,): email = email .decode ("utf8" ) # assume utf8 in input
362+ if sys .version_info < (3 ,):
363+ email = email .decode ("utf8" ) # assume utf8 in input
350364 validate_email (email , allow_smtputf8 = allow_smtputf8 )
351365 except EmailNotValidError as e :
352366 print (email , e )
@@ -355,17 +369,17 @@ def main():
355369 email = sys .argv [1 ]
356370 allow_smtputf8 = True
357371 check_deliverability = True
358- if sys .version_info < (3 ,): email = email .decode ("utf8" ) # assume utf8 in input
372+ if sys .version_info < (3 ,):
373+ email = email .decode ("utf8" ) # assume utf8 in input
359374 try :
360375 result = validate_email (email , allow_smtputf8 = allow_smtputf8 , check_deliverability = check_deliverability )
361376 print (json .dumps (result , indent = 2 , sort_keys = True , ensure_ascii = False ))
362377 except EmailNotValidError as e :
363378 if sys .version_info < (3 ,):
364- print (unicode (e ).encode ("utf8" ))
379+ print (unicode_class (e ).encode ("utf8" ))
365380 else :
366381 print (e )
367382
368383
369384if __name__ == "__main__" :
370385 main ()
371-
0 commit comments