Skip to content

Commit 35d9a18

Browse files
committed
The module-level attributes added in d64b291 to set global defaults were not working
This fixes the problem and restores the documentation that was previously reverted in b87f8d3.
1 parent 8afa90d commit 35d9a18

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

Diff for: README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,16 @@ The `validate_email` function also accepts the following keyword arguments
140140

141141
`allow_smtputf8=True`: Set to `False` to prohibit internationalized addresses that would
142142
require the
143-
[SMTPUTF8](https://tools.ietf.org/html/rfc6531) extension.
143+
[SMTPUTF8](https://tools.ietf.org/html/rfc6531) extension. You can also set `email_validator.ALLOW_SMTPUTF8` to `False` to turn it off for all calls by default.
144144

145-
`check_deliverability=True`: Set to `False` to skip DNS record checks for the domain. It is recommended to pass `False` when performing validation for login pages since re-validation of the domain by querying DNS at every login is probably undesirable.
145+
`check_deliverability=True`: Set to `False` to skip DNS record checks for the domain. It is recommended to pass `False` when performing validation for login pages since re-validation of the domain by querying DNS at every login is probably undesirable. You can also set `email_validator.CHECK_DELIVERABILITY` to `False` to turn this off for all calls by default.
146146

147147
`allow_empty_local=False`: Set to `True` to allow an empty local part (i.e.
148148
`@example.com`), e.g. for validating Postfix aliases.
149149

150150
`dns_resolver=None`: Pass an instance of [dns.resolver.Resolver](https://dnspython.readthedocs.io/en/latest/resolver-class.html) to control the DNS resolver including setting a timeout and [a cache](https://dnspython.readthedocs.io/en/latest/resolver-caching.html). The `caching_resolver` function shown above is a helper function to construct a dns.resolver.Resolver with a [LRUCache](https://dnspython.readthedocs.io/en/latest/resolver-caching.html#dns.resolver.LRUCache). Reuse the same resolver instance across calls to `validate_email` to make use of the cache.
151151

152-
`test_environment=False`: DNS-based deliverability checks are disabled and `test` and `subdomain.test` domain names are permitted (see below).
152+
`test_environment=False`: DNS-based deliverability checks are disabled and `test` and `subdomain.test` domain names are permitted (see below). You can also set `email_validator.TEST_ENVIRONMENT` to `True` to turn it on for all calls by default.
153153

154154
### DNS timeout and cache
155155

@@ -166,10 +166,11 @@ while True:
166166

167167
### Test addresses
168168

169-
This library rejects email addresess that use the [Special Use Domain Names](https://www.iana.org/assignments/special-use-domain-names/special-use-domain-names.xhtml) `invalid`, `localhost`, `test`, and some others by raising `EmailUndeliverableError`. This is to protect your system from abuse: You probably don't want a user to be able to cause an email to be sent to `localhost`. However, in your non-production test environments you may want to use `@test` or `@myname.test` email addresses. There are two ways you can allow this:
169+
This library rejects email addresess that use the [Special Use Domain Names](https://www.iana.org/assignments/special-use-domain-names/special-use-domain-names.xhtml) `invalid`, `localhost`, `test`, and some others by raising `EmailUndeliverableError`. This is to protect your system from abuse: You probably don't want a user to be able to cause an email to be sent to `localhost`. However, in your non-production test environments you may want to use `@test` or `@myname.test` email addresses. There are three ways you can allow this:
170170

171-
A. Add `test_environment=True` to the call to `validate_email` (see above).
172-
B. Remove the special-use domain name that you want to use from `email_validator.SPECIAL_USE_DOMAIN_NAMES`:
171+
1. Add `test_environment=True` to the call to `validate_email` (see above).
172+
2. Set `email_validator.TEST_ENVIRONMENT` to `True`.
173+
3. Remove the special-use domain name that you want to use from `email_validator.SPECIAL_USE_DOMAIN_NAMES`:
173174

174175
```python
175176
import email_validator

Diff for: email_validator/__init__.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ def __get_length_reason(addr, utf8=False, limit=EMAIL_MAX_LENGTH):
251251
return reason.format(prefix, diff, suffix)
252252

253253

254-
def caching_resolver(timeout=DEFAULT_TIMEOUT, cache=None):
254+
def caching_resolver(timeout=None, cache=None):
255+
if timeout is None:
256+
timeout = DEFAULT_TIMEOUT
255257
resolver = dns.resolver.Resolver()
256258
resolver.cache = cache or dns.resolver.LRUCache()
257259
resolver.lifetime = timeout # timeout, in seconds
@@ -260,11 +262,11 @@ def caching_resolver(timeout=DEFAULT_TIMEOUT, cache=None):
260262

261263
def validate_email(
262264
email,
263-
allow_smtputf8=ALLOW_SMTPUTF8,
265+
allow_smtputf8=None,
264266
allow_empty_local=False,
265-
check_deliverability=CHECK_DELIVERABILITY,
266-
test_environment=TEST_ENVIRONMENT,
267-
timeout=DEFAULT_TIMEOUT,
267+
check_deliverability=None,
268+
test_environment=None,
269+
timeout=None,
268270
dns_resolver=None
269271
):
270272
"""
@@ -273,6 +275,16 @@ def validate_email(
273275
but if bytes it must be ASCII-only.
274276
"""
275277

278+
# Fill in default values of arguments.
279+
if allow_smtputf8 is None:
280+
allow_smtputf8 = ALLOW_SMTPUTF8
281+
if check_deliverability is None:
282+
check_deliverability = CHECK_DELIVERABILITY
283+
if test_environment is None:
284+
test_environment = TEST_ENVIRONMENT
285+
if timeout is None:
286+
timeout = DEFAULT_TIMEOUT
287+
276288
# Allow email to be a str or bytes instance. If bytes,
277289
# it must be ASCII because that's how the bytes work
278290
# on the wire with SMTP.
@@ -579,6 +591,8 @@ def validate_email_domain_part(domain, test_environment=False):
579591
def validate_email_deliverability(domain, domain_i18n, timeout=DEFAULT_TIMEOUT, dns_resolver=None):
580592
# Check that the domain resolves to an MX record. If there is no MX record,
581593
# try an A or AAAA record which is a deprecated fallback for deliverability.
594+
# (Note that changing the DEFAULT_TIMEOUT module-level attribute
595+
# will not change the default value of this method's timeout argument.)
582596

583597
# If no dns.resolver.Resolver was given, get dnspython's default resolver.
584598
# Override the default resolver's timeout. This may affect other uses of

0 commit comments

Comments
 (0)