Skip to content

Commit

Permalink
Calculate number of days
Browse files Browse the repository at this point in the history
Some months have fewer than 31 days!  Shocking but true.
  • Loading branch information
Zac-HD committed Jun 3, 2019
1 parent 389d47c commit ce4caba
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
4 changes: 4 additions & 0 deletions hypothesis-python/RELEASE.rst
@@ -0,0 +1,4 @@
RELEASE_TYPE: patch

This patch makes :func:`~hypothesis.strategies.datetimes` more efficient,
as it now handles short months correctly by construction instead of filtering.
3 changes: 3 additions & 0 deletions hypothesis-python/src/hypothesis/searchstrategy/datetime.py
Expand Up @@ -18,6 +18,7 @@
from __future__ import absolute_import, division, print_function

import datetime as dt
from calendar import monthrange

from hypothesis.internal.conjecture import utils
from hypothesis.searchstrategy.strategies import SearchStrategy
Expand Down Expand Up @@ -50,6 +51,8 @@ def _attempt_one_draw(self, data):
for name in ("year", "month", "day", "hour", "minute", "second", "microsecond"):
low = getattr(self.min_dt if cap_low else dt.datetime.min, name)
high = getattr(self.max_dt if cap_high else dt.datetime.max, name)
if name == "day" and not cap_high:
_, high = monthrange(**result)
if name == "year":
val = utils.integer_range(data, low, high, 2000)
else:
Expand Down
25 changes: 4 additions & 21 deletions hypothesis-python/tests/cover/test_datetimes.py
Expand Up @@ -23,9 +23,7 @@

from hypothesis import given
from hypothesis.internal.compat import hrange
from hypothesis.internal.conjecture.data import ConjectureData, Status, StopTest
from hypothesis.searchstrategy.datetime import DatetimeStrategy
from hypothesis.strategies import binary, dates, datetimes, none, timedeltas, times
from hypothesis.strategies import dates, datetimes, timedeltas, times
from tests.common.debug import find_any, minimal


Expand Down Expand Up @@ -88,21 +86,6 @@ def test_bordering_on_a_leap_year():
assert x.year == 2004


def test_DatetimeStrategy_draw_may_fail():
def is_failure_inducing(b):
try:
return strat._attempt_one_draw(ConjectureData.for_buffer(b)) is None
except StopTest:
return False

strat = DatetimeStrategy(dt.datetime.min, dt.datetime.max, none())
failure_inducing = minimal(binary(), is_failure_inducing, timeout_after=30)
data = ConjectureData.for_buffer(failure_inducing * 100)
with pytest.raises(StopTest):
data.draw(strat)
assert data.status == Status.INVALID


def test_can_find_after_the_year_2000():
assert minimal(dates(), lambda x: x.year > 2000).year == 2001

Expand All @@ -111,9 +94,9 @@ def test_can_find_before_the_year_2000():
assert minimal(dates(), lambda x: x.year < 2000).year == 1999


def test_can_find_each_month():
for month in hrange(1, 13):
find_any(dates(), lambda x: x.month == month)
@pytest.mark.parametrize("month", hrange(1, 13))
def test_can_find_each_month(month):
find_any(dates(), lambda x: x.month == month)


def test_min_year_is_respected():
Expand Down

0 comments on commit ce4caba

Please sign in to comment.