Skip to content

Commit d64b291

Browse files
committed
Create module attributes to set global default values for keyword argument options
1 parent 91aa260 commit d64b291

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

Diff for: README.md

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

121121
`allow_smtputf8=True`: Set to `False` to prohibit internationalized addresses that would
122122
require the
123-
[SMTPUTF8](https://tools.ietf.org/html/rfc6531) extension.
123+
[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.
124124

125-
`check_deliverability=True`: Set to `False` to skip the domain name MX DNS record check.
125+
`check_deliverability=True`: Set to `False` to skip the domain name MX DNS record check. You can also set `email_validator.CHECK_DELIVERABILITY` to `False` to turn it off for all calls by default.
126126

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

130130
`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.
131131

132-
`test_environment=False`: DNS-based deliverability checks are disabled and `test` and `subdomain.test` domain names are permitted (see below).
132+
`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.
133133

134134
### DNS timeout and cache
135135

@@ -146,10 +146,11 @@ while True:
146146

147147
### Test addresses
148148

149-
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:
149+
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:
150150

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

154155
```python
155156
import email_validator

Diff for: email_validator/__init__.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
import dns.exception
88
import idna # implements IDNA 2008; Python's codec is only IDNA 2003
99

10+
# Default values for keyword arguments.
11+
12+
ALLOW_SMTPUTF8 = True
13+
CHECK_DELIVERABILITY = True
14+
TEST_ENVIRONMENT = False
15+
DEFAULT_TIMEOUT = 15 # secs
1016

1117
# Based on RFC 2822 section 3.2.4 / RFC 5322 section 3.2.3, these
1218
# characters are permitted in email addresses (not taking into
@@ -112,8 +118,6 @@
112118
DOT_ATOM_TEXT = DOT_ATOM_TEXT.decode("ascii")
113119
ATEXT_HOSTNAME = ATEXT_HOSTNAME.decode("ascii")
114120

115-
DEFAULT_TIMEOUT = 15 # secs
116-
117121

118122
class EmailNotValidError(ValueError):
119123
"""Parent class of all exceptions raised by this module."""
@@ -256,10 +260,10 @@ def caching_resolver(timeout=DEFAULT_TIMEOUT, cache=None):
256260

257261
def validate_email(
258262
email,
259-
allow_smtputf8=True,
263+
allow_smtputf8=ALLOW_SMTPUTF8,
260264
allow_empty_local=False,
261-
check_deliverability=True,
262-
test_environment=False,
265+
check_deliverability=CHECK_DELIVERABILITY,
266+
test_environment=TEST_ENVIRONMENT,
263267
timeout=DEFAULT_TIMEOUT,
264268
dns_resolver=None
265269
):

0 commit comments

Comments
 (0)