Skip to content

Commit 380e44e

Browse files
committed
Move setattr out of non-test code
1 parent be42a70 commit 380e44e

File tree

4 files changed

+47
-35
lines changed

4 files changed

+47
-35
lines changed

email_validator/deliverability.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, Optional
1+
from typing import Any, List, Optional, Tuple, TypedDict
22

33
import ipaddress
44

@@ -18,7 +18,14 @@ def caching_resolver(*, timeout: Optional[int] = None, cache: Any = None, dns_re
1818
return resolver
1919

2020

21-
def validate_email_deliverability(domain: str, domain_i18n: str, timeout: Optional[int] = None, dns_resolver: Optional[dns.resolver.Resolver] = None) -> Dict[str, str]:
21+
DeliverabilityInfo = TypedDict("DeliverabilityInfo", {
22+
"mx": List[Tuple[int, str]],
23+
"mx_fallback_type": Optional[str],
24+
"unknown-deliverability": str,
25+
}, total=False)
26+
27+
28+
def validate_email_deliverability(domain: str, domain_i18n: str, timeout: Optional[int] = None, dns_resolver: Optional[dns.resolver.Resolver] = None) -> DeliverabilityInfo:
2229
# Check that the domain resolves to an MX record. If there is no MX record,
2330
# try an A or AAAA record which is a deprecated fallback for deliverability.
2431
# Raises an EmailUndeliverableError on failure. On success, returns a dict
@@ -36,7 +43,7 @@ def validate_email_deliverability(domain: str, domain_i18n: str, timeout: Option
3643
elif timeout is not None:
3744
raise ValueError("It's not valid to pass both timeout and dns_resolver.")
3845

39-
deliverability_info: Dict[str, Any] = {}
46+
deliverability_info: DeliverabilityInfo = {}
4047

4148
try:
4249
try:
@@ -115,7 +122,6 @@ def is_global_addr(address: Any) -> bool:
115122
for rec in response:
116123
value = b"".join(rec.strings)
117124
if value.startswith(b"v=spf1 "):
118-
deliverability_info["spf"] = value.decode("ascii", errors='replace')
119125
if value == b"v=spf1 -all":
120126
raise EmailUndeliverableError(f"The domain name {domain_i18n} does not send email.")
121127
except dns.resolver.NoAnswer:

email_validator/exceptions_types.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,11 @@ class ValidatedEmail:
6060

6161
"""If no MX records are actually specified in DNS and instead are inferred, through an obsolete
6262
mechanism, from A or AAAA records, the value is the type of DNS record used instead (`A` or `AAAA`)."""
63-
mx_fallback_type: str
63+
mx_fallback_type: Optional[str]
6464

6565
"""The display name in the original input text, unquoted and unescaped, or None."""
6666
display_name: Optional[str]
6767

68-
"""Tests use this constructor."""
69-
def __init__(self, **kwargs: Any) -> None:
70-
for k, v in kwargs.items():
71-
setattr(self, k, v)
72-
7368
def __repr__(self) -> str:
7469
return f"<ValidatedEmail {self.normalized}>"
7570

email_validator/validate_email.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ def validate_email(
152152
deliverability_info = validate_email_deliverability(
153153
ret.ascii_domain, ret.domain, timeout, dns_resolver
154154
)
155-
for key, value in deliverability_info.items():
156-
setattr(ret, key, value)
155+
mx = deliverability_info.get("mx")
156+
if mx is not None:
157+
ret.mx = mx
158+
ret.mx_fallback_type = deliverability_info.get("mx_fallback_type")
157159

158160
return ret

tests/test_syntax.py

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1+
from typing import Any
2+
13
import pytest
24

35
from email_validator import EmailSyntaxError, \
46
validate_email, \
57
ValidatedEmail
68

79

10+
def MakeValidatedEmail(**kwargs: Any) -> ValidatedEmail:
11+
ret = ValidatedEmail()
12+
for k, v in kwargs.items():
13+
setattr(ret, k, v)
14+
return ret
15+
16+
817
@pytest.mark.parametrize(
918
'email_input,output',
1019
[
1120
(
1221
'Abc@example.tld',
13-
ValidatedEmail(
22+
MakeValidatedEmail(
1423
local_part='Abc',
1524
ascii_local_part='Abc',
1625
smtputf8=False,
@@ -22,7 +31,7 @@
2231
),
2332
(
2433
'Abc.123@test-example.com',
25-
ValidatedEmail(
34+
MakeValidatedEmail(
2635
local_part='Abc.123',
2736
ascii_local_part='Abc.123',
2837
smtputf8=False,
@@ -34,7 +43,7 @@
3443
),
3544
(
3645
'user+mailbox/department=shipping@example.tld',
37-
ValidatedEmail(
46+
MakeValidatedEmail(
3847
local_part='user+mailbox/department=shipping',
3948
ascii_local_part='user+mailbox/department=shipping',
4049
smtputf8=False,
@@ -46,7 +55,7 @@
4655
),
4756
(
4857
"!#$%&'*+-/=?^_`.{|}~@example.tld",
49-
ValidatedEmail(
58+
MakeValidatedEmail(
5059
local_part="!#$%&'*+-/=?^_`.{|}~",
5160
ascii_local_part="!#$%&'*+-/=?^_`.{|}~",
5261
smtputf8=False,
@@ -58,7 +67,7 @@
5867
),
5968
(
6069
'jeff@臺網中心.tw',
61-
ValidatedEmail(
70+
MakeValidatedEmail(
6271
local_part='jeff',
6372
ascii_local_part='jeff',
6473
smtputf8=False,
@@ -70,7 +79,7 @@
7079
),
7180
(
7281
'"quoted local part"@example.org',
73-
ValidatedEmail(
82+
MakeValidatedEmail(
7483
local_part='"quoted local part"',
7584
ascii_local_part='"quoted local part"',
7685
smtputf8=False,
@@ -82,7 +91,7 @@
8291
),
8392
(
8493
'"de-quoted.local.part"@example.org',
85-
ValidatedEmail(
94+
MakeValidatedEmail(
8695
local_part='de-quoted.local.part',
8796
ascii_local_part='de-quoted.local.part',
8897
smtputf8=False,
@@ -94,7 +103,7 @@
94103
),
95104
(
96105
'MyName <me@example.org>',
97-
ValidatedEmail(
106+
MakeValidatedEmail(
98107
local_part='me',
99108
ascii_local_part='me',
100109
smtputf8=False,
@@ -107,7 +116,7 @@
107116
),
108117
(
109118
'My Name <me@example.org>',
110-
ValidatedEmail(
119+
MakeValidatedEmail(
111120
local_part='me',
112121
ascii_local_part='me',
113122
smtputf8=False,
@@ -120,7 +129,7 @@
120129
),
121130
(
122131
r'"My.\"Na\\me\".Is" <"me \" \\ me"@example.org>',
123-
ValidatedEmail(
132+
MakeValidatedEmail(
124133
local_part=r'"me \" \\ me"',
125134
ascii_local_part=r'"me \" \\ me"',
126135
smtputf8=False,
@@ -157,7 +166,7 @@ def test_email_valid(email_input: str, output: ValidatedEmail) -> None:
157166
[
158167
(
159168
'伊昭傑@郵件.商務',
160-
ValidatedEmail(
169+
MakeValidatedEmail(
161170
local_part='伊昭傑',
162171
smtputf8=True,
163172
ascii_domain='xn--5nqv22n.xn--lhr59c',
@@ -167,7 +176,7 @@ def test_email_valid(email_input: str, output: ValidatedEmail) -> None:
167176
),
168177
(
169178
'राम@मोहन.ईन्फो',
170-
ValidatedEmail(
179+
MakeValidatedEmail(
171180
local_part='राम',
172181
smtputf8=True,
173182
ascii_domain='xn--l2bl7a9d.xn--o1b8dj2ki',
@@ -177,7 +186,7 @@ def test_email_valid(email_input: str, output: ValidatedEmail) -> None:
177186
),
178187
(
179188
'юзер@екзампл.ком',
180-
ValidatedEmail(
189+
MakeValidatedEmail(
181190
local_part='юзер',
182191
smtputf8=True,
183192
ascii_domain='xn--80ajglhfv.xn--j1aef',
@@ -187,7 +196,7 @@ def test_email_valid(email_input: str, output: ValidatedEmail) -> None:
187196
),
188197
(
189198
'θσερ@εχαμπλε.ψομ',
190-
ValidatedEmail(
199+
MakeValidatedEmail(
191200
local_part='θσερ',
192201
smtputf8=True,
193202
ascii_domain='xn--mxahbxey0c.xn--xxaf0a',
@@ -197,7 +206,7 @@ def test_email_valid(email_input: str, output: ValidatedEmail) -> None:
197206
),
198207
(
199208
'葉士豪@臺網中心.tw',
200-
ValidatedEmail(
209+
MakeValidatedEmail(
201210
local_part='葉士豪',
202211
smtputf8=True,
203212
ascii_domain='xn--fiqq24b10vi0d.tw',
@@ -207,7 +216,7 @@ def test_email_valid(email_input: str, output: ValidatedEmail) -> None:
207216
),
208217
(
209218
'葉士豪@臺網中心.台灣',
210-
ValidatedEmail(
219+
MakeValidatedEmail(
211220
local_part='葉士豪',
212221
smtputf8=True,
213222
ascii_domain='xn--fiqq24b10vi0d.xn--kpry57d',
@@ -217,7 +226,7 @@ def test_email_valid(email_input: str, output: ValidatedEmail) -> None:
217226
),
218227
(
219228
'jeff葉@臺網中心.tw',
220-
ValidatedEmail(
229+
MakeValidatedEmail(
221230
local_part='jeff葉',
222231
smtputf8=True,
223232
ascii_domain='xn--fiqq24b10vi0d.tw',
@@ -227,7 +236,7 @@ def test_email_valid(email_input: str, output: ValidatedEmail) -> None:
227236
),
228237
(
229238
'ñoñó@example.tld',
230-
ValidatedEmail(
239+
MakeValidatedEmail(
231240
local_part='ñoñó',
232241
smtputf8=True,
233242
ascii_domain='example.tld',
@@ -237,7 +246,7 @@ def test_email_valid(email_input: str, output: ValidatedEmail) -> None:
237246
),
238247
(
239248
'我買@example.tld',
240-
ValidatedEmail(
249+
MakeValidatedEmail(
241250
local_part='我買',
242251
smtputf8=True,
243252
ascii_domain='example.tld',
@@ -247,7 +256,7 @@ def test_email_valid(email_input: str, output: ValidatedEmail) -> None:
247256
),
248257
(
249258
'甲斐黒川日本@example.tld',
250-
ValidatedEmail(
259+
MakeValidatedEmail(
251260
local_part='甲斐黒川日本',
252261
smtputf8=True,
253262
ascii_domain='example.tld',
@@ -257,7 +266,7 @@ def test_email_valid(email_input: str, output: ValidatedEmail) -> None:
257266
),
258267
(
259268
'чебурашкаящик-с-апельсинами.рф@example.tld',
260-
ValidatedEmail(
269+
MakeValidatedEmail(
261270
local_part='чебурашкаящик-с-апельсинами.рф',
262271
smtputf8=True,
263272
ascii_domain='example.tld',
@@ -267,7 +276,7 @@ def test_email_valid(email_input: str, output: ValidatedEmail) -> None:
267276
),
268277
(
269278
'उदाहरण.परीक्ष@domain.with.idn.tld',
270-
ValidatedEmail(
279+
MakeValidatedEmail(
271280
local_part='उदाहरण.परीक्ष',
272281
smtputf8=True,
273282
ascii_domain='domain.with.idn.tld',
@@ -277,7 +286,7 @@ def test_email_valid(email_input: str, output: ValidatedEmail) -> None:
277286
),
278287
(
279288
'ιωάννης@εεττ.gr',
280-
ValidatedEmail(
289+
MakeValidatedEmail(
281290
local_part='ιωάννης',
282291
smtputf8=True,
283292
ascii_domain='xn--qxaa9ba.gr',

0 commit comments

Comments
 (0)