Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/email_validator/exceptions_types.py: 46%

61 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:32 +0000

1import warnings 

2from typing import Optional 

3 

4 

5class EmailNotValidError(ValueError): 

6 """Parent class of all exceptions raised by this module.""" 

7 pass 

8 

9 

10class EmailSyntaxError(EmailNotValidError): 

11 """Exception raised when an email address fails validation because of its form.""" 

12 pass 

13 

14 

15class EmailUndeliverableError(EmailNotValidError): 

16 """Exception raised when an email address fails validation because its domain name does not appear deliverable.""" 

17 pass 

18 

19 

20class ValidatedEmail(object): 

21 """The validate_email function returns objects of this type holding the normalized form of the email address 

22 and other information.""" 

23 

24 """The email address that was passed to validate_email. (If passed as bytes, this will be a string.)""" 

25 original: str 

26 

27 """The normalized email address, which should always be used in preferance to the original address. 

28 The normalized address converts an IDNA ASCII domain name to Unicode, if possible, and performs 

29 Unicode normalization on the local part and on the domain (if originally Unicode). It is the 

30 concatenation of the local_part and domain attributes, separated by an @-sign.""" 

31 normalized: str 

32 

33 """The local part of the email address after Unicode normalization.""" 

34 local_part: str 

35 

36 """The domain part of the email address after Unicode normalization or conversion to 

37 Unicode from IDNA ascii.""" 

38 domain: str 

39 

40 """If the domain part is a domain literal, the IPv4Address or IPv6Address object.""" 

41 domain_address: object 

42 

43 """If not None, a form of the email address that uses 7-bit ASCII characters only.""" 

44 ascii_email: Optional[str] 

45 

46 """If not None, the local part of the email address using 7-bit ASCII characters only.""" 

47 ascii_local_part: Optional[str] 

48 

49 """A form of the domain name that uses 7-bit ASCII characters only.""" 

50 ascii_domain: str 

51 

52 """If True, the SMTPUTF8 feature of your mail relay will be required to transmit messages 

53 to this address. This flag is True just when ascii_local_part is missing. Otherwise it 

54 is False.""" 

55 smtputf8: bool 

56 

57 """If a deliverability check is performed and if it succeeds, a list of (priority, domain) 

58 tuples of MX records specified in the DNS for the domain.""" 

59 mx: list 

60 

61 """If no MX records are actually specified in DNS and instead are inferred, through an obsolete 

62 mechanism, from A or AAAA records, the value is the type of DNS record used instead (`A` or `AAAA`).""" 

63 mx_fallback_type: str 

64 

65 """Tests use this constructor.""" 

66 def __init__(self, **kwargs): 

67 for k, v in kwargs.items(): 

68 setattr(self, k, v) 

69 

70 def __repr__(self): 

71 return f"<ValidatedEmail {self.normalized}>" 

72 

73 """For backwards compatibility, support old field names.""" 

74 def __getattr__(self, key): 

75 if key == "original_email": 

76 return self.original 

77 if key == "email": 

78 return self.normalized 

79 raise AttributeError() 

80 

81 """For backwards compatibility, some fields are also exposed through a dict-like interface. Note 

82 that some of the names changed when they became attributes.""" 

83 def __getitem__(self, key): 

84 warnings.warn("dict-like access to the return value of validate_email is deprecated and may not be supported in the future.", DeprecationWarning, stacklevel=2) 

85 if key == "email": 

86 return self.normalized 

87 if key == "email_ascii": 

88 return self.ascii_email 

89 if key == "local": 

90 return self.local_part 

91 if key == "domain": 

92 return self.ascii_domain 

93 if key == "domain_i18n": 

94 return self.domain 

95 if key == "smtputf8": 

96 return self.smtputf8 

97 if key == "mx": 

98 return self.mx 

99 if key == "mx-fallback": 

100 return self.mx_fallback_type 

101 raise KeyError() 

102 

103 """Tests use this.""" 

104 def __eq__(self, other): 

105 if not isinstance(other, ValidatedEmail): 

106 return False 

107 return ( 

108 self.normalized == other.normalized 

109 and self.local_part == other.local_part 

110 and self.domain == other.domain 

111 and getattr(self, 'ascii_email', None) == getattr(other, 'ascii_email', None) 

112 and getattr(self, 'ascii_local_part', None) == getattr(other, 'ascii_local_part', None) 

113 and getattr(self, 'ascii_domain', None) == getattr(other, 'ascii_domain', None) 

114 and self.smtputf8 == other.smtputf8 

115 and repr(sorted(self.mx) if getattr(self, 'mx', None) else None) 

116 == repr(sorted(other.mx) if getattr(other, 'mx', None) else None) 

117 and getattr(self, 'mx_fallback_type', None) == getattr(other, 'mx_fallback_type', None) 

118 ) 

119 

120 """This helps producing the README.""" 

121 def as_constructor(self): 

122 return "ValidatedEmail(" \ 

123 + ",".join("\n {}={}".format( 

124 key, 

125 repr(getattr(self, key))) 

126 for key in ('email', 'local_part', 'domain', 

127 'ascii_email', 'ascii_local_part', 'ascii_domain', 

128 'smtputf8', 'mx', 'mx_fallback_type') 

129 ) \ 

130 + ")" 

131 

132 """Convenience method for accessing ValidatedEmail as a dict""" 

133 def as_dict(self): 

134 d = self.__dict__ 

135 if d.get('domain_address'): 

136 d['domain_address'] = repr(d['domain_address']) 

137 return d