1010def get_length_reason (addr , utf8 = False , limit = EMAIL_MAX_LENGTH ):
1111 """Helper function to return an error message related to invalid length."""
1212 diff = len (addr ) - limit
13- reason = "({}{} character{} too many)"
1413 prefix = "at least " if utf8 else ""
1514 suffix = "s" if diff > 1 else ""
16- return reason . format ( prefix , diff , suffix )
15+ return f"( { prefix } { diff } character { suffix } too many)"
1716
1817
1918def safe_character_display (c ):
@@ -23,9 +22,9 @@ def safe_character_display(c):
2322
2423 # Construct a hex string in case the unicode name doesn't exist.
2524 if ord (c ) < 0xFFFF :
26- h = "U+{:04x}" . format ( ord (c )) .upper ()
25+ h = f "U+{ ord (c ):04x } " .upper ()
2726 else :
28- h = "U+{:08x}" . format ( ord (c )) .upper ()
27+ h = f "U+{ ord (c ):08x } " .upper ()
2928
3029 # Return the character name or, if it has no name, the hex string.
3130 return unicodedata .name (c , h )
@@ -55,7 +54,7 @@ def validate_email_local_part(local, allow_smtputf8=True, allow_empty_local=Fals
5554 # instead.
5655 if len (local ) > LOCAL_PART_MAX_LENGTH :
5756 reason = get_length_reason (local , limit = LOCAL_PART_MAX_LENGTH )
58- raise EmailSyntaxError ("The email address is too long before the @-sign {}." . format ( reason ) )
57+ raise EmailSyntaxError (f "The email address is too long before the @-sign { reason } ." )
5958
6059 # Check the local part against the non-internationalized regular expression.
6160 # Most email addresses match this regex so it's probably fastest to check this first.
@@ -231,7 +230,7 @@ def validate_email_domain_part(domain, test_environment=False, globally_delivera
231230 try :
232231 domain = idna .uts46_remap (domain , std3_rules = False , transitional = False )
233232 except idna .IDNAError as e :
234- raise EmailSyntaxError ("The part after the @-sign contains invalid characters ({})." . format ( str ( e )) )
233+ raise EmailSyntaxError (f "The part after the @-sign contains invalid characters ({ e } )." )
235234
236235 # The domain part is made up period-separated "labels." Each label must
237236 # have at least one character and cannot start or end with dashes, which
@@ -274,7 +273,7 @@ def validate_email_domain_part(domain, test_environment=False, globally_delivera
274273 # one the user supplied. Also I'm not sure if the length check applies
275274 # to the internationalized form, the IDNA ASCII form, or even both!
276275 raise EmailSyntaxError ("The email address is too long after the @-sign." )
277- raise EmailSyntaxError ("The part after the @-sign contains invalid characters (%s )." % str ( e ) )
276+ raise EmailSyntaxError (f "The part after the @-sign contains invalid characters ({ e } )." )
278277
279278 # Check the syntax of the string returned by idna.encode.
280279 # It should never fail.
@@ -291,14 +290,14 @@ def validate_email_domain_part(domain, test_environment=False, globally_delivera
291290 # is never reached for internationalized domains.)
292291 if len (ascii_domain ) > DOMAIN_MAX_LENGTH :
293292 reason = get_length_reason (ascii_domain , limit = DOMAIN_MAX_LENGTH )
294- raise EmailSyntaxError ("The email address is too long after the @-sign {}." . format ( reason ) )
293+ raise EmailSyntaxError (f "The email address is too long after the @-sign { reason } ." )
295294
296295 # Also check the label length limit.
297296 # (RFC 1035 2.3.1)
298297 for label in ascii_domain .split ("." ):
299298 if len (label ) > DNS_LABEL_LENGTH_LIMIT :
300299 reason = get_length_reason (label , limit = DNS_LABEL_LENGTH_LIMIT )
301- raise EmailSyntaxError ("After the @-sign, periods cannot be separated by so many characters {}." . format ( reason ) )
300+ raise EmailSyntaxError (f "After the @-sign, periods cannot be separated by so many characters { reason } ." )
302301
303302 if globally_deliverable :
304303 # All publicly deliverable addresses have domain named with at least
@@ -337,7 +336,7 @@ def validate_email_domain_part(domain, test_environment=False, globally_delivera
337336 try :
338337 domain_i18n = idna .decode (ascii_domain .encode ('ascii' ))
339338 except idna .IDNAError as e :
340- raise EmailSyntaxError ("The part after the @-sign is not valid IDNA ({})." . format ( str ( e )) )
339+ raise EmailSyntaxError (f "The part after the @-sign is not valid IDNA ({ e } )." )
341340
342341 # Check for invalid characters after normalization. These
343342 # should never arise. See the similar checks above.
0 commit comments