Skip to content

Commit

Permalink
Add test + release notes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Mar 15, 2023
1 parent 64822f5 commit 36375d5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ their individual contributions.
* `James Lamb <https://github.com/jameslamb>`_
* `Jenny Rouleau <https://github.com/jennyrou>`_
* `Jens Heinrich <https://github.com/JensHeinrich>`_
* `Jens Tröger <https://github.com/jenstroeger>`_
* `Jeremy Thurgood <https://github.com/jerith>`_
* `J.J. Green <http://soliton.vm.bytemark.co.uk/pub/jjg/>`_
* `JP Viljoen <https://github.com/froztbyte>`_ (froztbyte@froztbyte.net)
Expand Down
8 changes: 8 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
RELEASE_TYPE: minor

This release adds an optional ``domains=`` parameter to the
:func:`~hypothesis.strategies.emails` strategy, and excludes
the special-use :wikipedia:`.arpa` domain from the default
strategy (:issue:`3567`).

Thanks to Jens Tröger for reporting and fixing this bug!
12 changes: 6 additions & 6 deletions hypothesis-python/src/hypothesis/strategies/_internal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2014,28 +2014,28 @@ def deferred(definition: Callable[[], SearchStrategy[Ex]]) -> SearchStrategy[Ex]

def domains():
import hypothesis.provisional

return hypothesis.provisional.domains()


@defines_strategy(force_reusable_values=True)
def emails(*, domains: SearchStrategy[str] = LazyStrategy(domains, (), {})) -> SearchStrategy[str]:
def emails(
*, domains: SearchStrategy[str] = LazyStrategy(domains, (), {})
) -> SearchStrategy[str]:
"""A strategy for generating email addresses as unicode strings. The
address format is specified in :rfc:`5322#section-3.4.1`. Values shrink
towards shorter local-parts and host domains.
If ``domains`` is given then it must be a strategy that generates domain
names for the emails. By default, the internal :func:`hypothesis.provisional.domains` strategy
is used that does *not* produce any `special-use domain names
<https://www.iana.org/assignments/special-use-domain-names/special-use-domain-names.xhtml>`_
(which yield invalid email addresses).
names for the emails, defaulting to :func:`~hypothesis.provisional.domains`.
This strategy is useful for generating "user data" for tests, as
mishandling of email addresses is a common source of bugs.
"""
local_chars = string.ascii_letters + string.digits + "!#$%&'*+-/=^_`{|}~"
local_part = text(local_chars, min_size=1, max_size=64)
# TODO: include dot-atoms, quoted strings, escaped chars, etc in local part
return builds("{}@{}".format, local_part, domains()).filter(
return builds("{}@{}".format, local_part, domains).filter(
lambda addr: len(addr) <= 254
)

Expand Down
10 changes: 8 additions & 2 deletions hypothesis-python/tests/nocover/test_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@
# obtain one at https://mozilla.org/MPL/2.0/.

from hypothesis import given
from hypothesis.strategies import emails
from hypothesis.strategies import emails, just


@given(emails())
def test_is_valid_email(address):
def test_is_valid_email(address: str):
local, at_, domain = address.rpartition("@")
assert len(address) <= 254
assert at_ == "@"
assert local
assert domain
assert not domain.lower().endswith(".arpa")


@given(emails(domains=just("mydomain.com")))
def test_can_restrict_email_domains(address: str):
assert address.endswith("@mydomain.com")

0 comments on commit 36375d5

Please sign in to comment.