Skip to content

Commit b4152ea

Browse files
committed
rewrite comments and improve test
1 parent 6b18db3 commit b4152ea

2 files changed

Lines changed: 24 additions & 22 deletions

File tree

‎hypothesis/src/hypothesis/strategies/_internal/datetime.py‎

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,10 @@ def datetime_does_not_exist(value):
8888

8989

9090
def _num_days_in_month(year, month):
91-
"""Equivalent to ``monthrange(year, month)[1]`` for valid inputs.
91+
"""Branchless equivalent of ``monthrange(year, month)[1]`` for valid inputs.
9292
93-
Written using only arithmetic and (in)equality, with no branching, ``and``,
94-
``or``, or indexing on ``month``/``year``. This keeps the result symbolic
95-
under symbolic-execution backends (which would otherwise concretize the year
96-
and month the moment they hit a branch or a list lookup), while remaining a
97-
handful of cheap integer ops for the blackbox engine.
93+
Written using only arithmetic and (in)equality, with no branching or indexing.
94+
This avoids concretizing the input or adding more path constraints than necessary.
9895
"""
9996
leap = (year % 4 == 0) * (1 - (year % 100 == 0) * (year % 400 != 0))
10097
is_feb = month == 2
@@ -108,25 +105,27 @@ def draw_capped_multipart(
108105
assert isinstance(min_value, (dt.date, dt.time, dt.datetime))
109106
assert type(min_value) == type(max_value)
110107
assert min_value <= max_value
108+
109+
# cap_{low, high} records whether every field drawn so far has equalled
110+
# ``min_value``'s / ``max_value``'s, i.e. whether that bound is still "active" and
111+
# constrains the next field.
112+
#
113+
# cap_{low, high} are conceptually booleans. We define them as integers and interpret
114+
# boolean operations on them as multiplication, so that we don't concretize or
115+
# branch under symbolic backends. See
116+
# https://github.com/HypothesisWorks/hypothesis/issues/4759.
117+
cap_low = 1
118+
cap_high = 1
111119
result = {}
112-
# ``cap_low``/``cap_high`` are 0/1 integers (not booleans) recording whether
113-
# every field drawn so far has equalled ``min_value``'s / ``max_value``'s,
114-
# i.e. whether that bound is still "active" and constrains the next field.
115-
# We combine them into the bounds with arithmetic rather than branching, so
116-
# that the *same* code stays fully symbolic under symbolic-execution
117-
# backends (important for cross-backend database replay) yet compiles down
118-
# to a few integer ops for the blackbox engine. The draws themselves --
119-
# their count, order, bounds, and ``shrink_towards`` -- are byte-for-byte
120-
# identical to the previous branchy implementation.
121-
cap_low = cap_high = 1
122120
for name in duration_names:
123121
natural_low = getattr(dt.datetime.min, name)
124122
if name == "day":
125123
natural_high = _num_days_in_month(result["year"], result["month"])
126124
else:
127125
natural_high = getattr(dt.datetime.max, name)
128-
# low = min_value.<name> if cap_low else natural_low
129-
# high = max_value.<name> if cap_high else natural_high
126+
# equivalent to:
127+
# low = min_value.<name> if cap_low else natural_low
128+
# high = max_value.<name> if cap_high else natural_high
130129
low = natural_low + cap_low * (getattr(min_value, name) - natural_low)
131130
high = natural_high + cap_high * (getattr(max_value, name) - natural_high)
132131
if name == "year":

‎hypothesis/tests/cover/test_datetimes.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@
1313

1414
import pytest
1515

16-
from hypothesis import HealthCheck, given, settings
16+
from hypothesis import HealthCheck, example, given, settings
17+
from hypothesis import strategies as st
1718
from hypothesis.strategies import dates, datetimes, timedeltas, times
1819
from hypothesis.strategies._internal.datetime import _num_days_in_month
1920

2021
from tests.common.debug import assert_simple_property, find_any, minimal
2122

2223

23-
@pytest.mark.parametrize("year", [1, 1900, 2000, 2004, 2023, 2024, 2100, 9999])
24+
@example(1900)
25+
@example(2000)
26+
@example(2004)
27+
@example(2100)
28+
@given(st.integers(dt.MINYEAR, dt.MAXYEAR))
2429
def test_num_days_in_month_matches_monthrange(year):
25-
# The branchless _num_days_in_month must agree with the stdlib for every
26-
# month, including the century/400-year leap rules around February.
2730
for month in range(1, 13):
2831
assert _num_days_in_month(year, month) == monthrange(year, month)[1]
2932

0 commit comments

Comments
 (0)