Skip to content

Commit 13cdd0b

Browse files
authored
Merge pull request #4760 from Zac-HD/datetime-symbolic-4759
Improve symbolic execution for dates, times, and datetimes
2 parents 2c6dfdb + e48846d commit 13cdd0b

13 files changed

Lines changed: 130 additions & 72 deletions

File tree

‎.github/workflows/main.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ jobs:
253253
# Note that the versions below are updated by `update_pyodide_versions()` in our weekly cronjob.
254254
# The versions of pyodide-build and the Pyodide runtime may differ.
255255
PYODIDE_VERSION: 0.29.4
256-
PYODIDE_BUILD_VERSION: 0.34.3
256+
PYODIDE_BUILD_VERSION: 0.34.4
257257
# pyodide 0.29.0 (latest at time of writing) doesn't yet support 3.14
258258
PYTHON_VERSION: 3.13.2
259259
steps:

‎hypothesis/RELEASE.rst‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch rewrites the internal date- and time-drawing helper to use plain
4+
arithmetic instead of branching on the values it draws. The generated
5+
distribution is unchanged, but :func:`~hypothesis.strategies.dates`,
6+
:func:`~hypothesis.strategies.datetimes`, and
7+
:func:`~hypothesis.strategies.times` are now much more efficient under
8+
symbolic-execution backends such as :pypi:`crosshair-tool`, which can now
9+
solve for a specific date directly rather than enumerating candidates
10+
(:issue:`4759`).

‎hypothesis/pyproject.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ pandas = ["pandas>=1.1"]
114114
pytest = ["pytest>=4.6"]
115115
dpcontracts = ["dpcontracts>=0.4"]
116116
redis = ["redis>=3.0.0"]
117-
crosshair = ["hypothesis-crosshair>=0.0.28", "crosshair-tool>=0.0.104"]
117+
crosshair = ["hypothesis-crosshair>=0.0.28", "crosshair-tool>=0.0.106"]
118118
# zoneinfo is an odd one: every dependency is platform-conditional.
119119
zoneinfo = ["tzdata>=2026.2; sys_platform == 'win32' or sys_platform == 'emscripten'"]
120120
# We only support Django versions with upstream support - see
@@ -124,7 +124,7 @@ zoneinfo = ["tzdata>=2026.2; sys_platform == 'win32' or sys_platform == 'emscrip
124124
django = ["django>=5.2"]
125125
watchdog = ["watchdog>=4.0.0"]
126126
# Avoid changing this by hand. This is automatically updated by update_changelog_and_version
127-
all = ["black>=20.8b0", "click>=7.0", "crosshair-tool>=0.0.104", "django>=5.2", "dpcontracts>=0.4", "hypothesis-crosshair>=0.0.28", "lark>=0.10.1", "libcst>=0.3.16", "numpy>=1.21.6", "pandas>=1.1", "pytest>=4.6", "python-dateutil>=1.4", "pytz>=2014.1", "redis>=3.0.0", "rich>=9.0.0", "tzdata>=2026.2; sys_platform == 'win32' or sys_platform == 'emscripten'", "watchdog>=4.0.0"]
127+
all = ["black>=20.8b0", "click>=7.0", "crosshair-tool>=0.0.106", "django>=5.2", "dpcontracts>=0.4", "hypothesis-crosshair>=0.0.28", "lark>=0.10.1", "libcst>=0.3.16", "numpy>=1.21.6", "pandas>=1.1", "pytest>=4.6", "python-dateutil>=1.4", "pytz>=2014.1", "redis>=3.0.0", "rich>=9.0.0", "tzdata>=2026.2; sys_platform == 'win32' or sys_platform == 'emscripten'", "watchdog>=4.0.0"]
128128

129129
[tool.setuptools.dynamic]
130130
version = {attr = "hypothesis.version.__version__"}

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

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import operator as op
1313
import warnings
1414
import zoneinfo
15-
from calendar import monthrange
1615
from functools import cache, partial
1716
from importlib import resources
1817
from pathlib import Path
@@ -88,26 +87,54 @@ def datetime_does_not_exist(value):
8887
return value != roundtrip
8988

9089

90+
def _num_days_in_month(year, month):
91+
"""Branchless equivalent of ``monthrange(year, month)[1]`` for valid inputs.
92+
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.
95+
"""
96+
leap = (year % 4 == 0) * (1 - (year % 100 == 0) * (year % 400 != 0))
97+
is_feb = month == 2
98+
is_30_day = 1 - (month != 4) * (month != 6) * (month != 9) * (month != 11)
99+
return 31 - is_30_day - is_feb * (3 - leap)
100+
101+
91102
def draw_capped_multipart(
92103
data, min_value, max_value, duration_names=DATENAMES + TIMENAMES
93104
):
94105
assert isinstance(min_value, (dt.date, dt.time, dt.datetime))
95106
assert type(min_value) == type(max_value)
96107
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
97119
result = {}
98-
cap_low, cap_high = True, True
99120
for name in duration_names:
100-
low = getattr(min_value if cap_low else dt.datetime.min, name)
101-
high = getattr(max_value if cap_high else dt.datetime.max, name)
102-
if name == "day" and not cap_high:
103-
_, high = monthrange(**result)
121+
natural_low = getattr(dt.datetime.min, name)
122+
if name == "day":
123+
natural_high = _num_days_in_month(result["year"], result["month"])
124+
else:
125+
natural_high = getattr(dt.datetime.max, name)
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
129+
low = natural_low + cap_low * (getattr(min_value, name) - natural_low)
130+
high = natural_high + cap_high * (getattr(max_value, name) - natural_high)
104131
if name == "year":
105132
val = data.draw_integer(low, high, shrink_towards=2000)
106133
else:
107134
val = data.draw_integer(low, high)
108135
result[name] = val
109-
cap_low = cap_low and val == low
110-
cap_high = cap_high and val == high
136+
cap_low = cap_low * (val == low)
137+
cap_high = cap_high * (val == high)
111138
if hasattr(min_value, "fold"):
112139
# The `fold` attribute is ignored in comparison of naive datetimes.
113140
# In tz-aware datetimes it would require *very* invasive changes to

‎hypothesis/tests/cover/test_datetimes.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,27 @@
99
# obtain one at https://mozilla.org/MPL/2.0/.
1010

1111
import datetime as dt
12+
from calendar import monthrange
1213

1314
import pytest
1415

15-
from hypothesis import HealthCheck, given, settings
16+
from hypothesis import HealthCheck, example, given, settings, strategies as st
1617
from hypothesis.strategies import dates, datetimes, timedeltas, times
18+
from hypothesis.strategies._internal.datetime import _num_days_in_month
1719

1820
from tests.common.debug import assert_simple_property, find_any, minimal
1921

2022

23+
@example(1900)
24+
@example(2000)
25+
@example(2004)
26+
@example(2100)
27+
@given(st.integers(dt.MINYEAR, dt.MAXYEAR))
28+
def test_num_days_in_month_matches_monthrange(year):
29+
for month in range(1, 13):
30+
assert _num_days_in_month(year, month) == monthrange(year, month)[1]
31+
32+
2133
def test_can_find_positive_delta():
2234
assert minimal(timedeltas(), lambda x: x.days > 0) == dt.timedelta(1)
2335

‎hypothesis/tests/cover/test_observability.py‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ def do_it_all(l, a, x, data):
8484
1 / ((x or 1) % 7)
8585

8686

87-
@xfail_on_crosshair(Why.other, strict=False) # flakey BackendCannotProceed ??
8887
@skipif_threading # captures observations from other threads
8988
def test_observability():
9089
with capture_observations() as ls:
@@ -202,10 +201,7 @@ def f(data):
202201
tc for tc in observations if tc.type == "test_case" and tc.status == "passed"
203202
]
204203
assert test_cases
205-
# TODO crosshair has a soundness bug with assume. remove branch when fixed
206-
# https://github.com/pschanely/hypothesis-crosshair/issues/34
207-
if not crosshair:
208-
assert {tc.representation for tc in test_cases} == {expected}
204+
assert {tc.representation for tc in test_cases} == {expected}
209205

210206

211207
@xfail_on_crosshair(Why.other)

‎hypothesis/tests/crosshair/test_crosshair.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
99
# obtain one at https://mozilla.org/MPL/2.0/.
1010

11+
import datetime as dt
12+
1113
import crosshair
1214
import pytest
1315
from hypothesis_crosshair_provider.crosshair_provider import CrossHairPrimitiveProvider
@@ -224,3 +226,14 @@ def test(n):
224226
def test_event_with_realization(value):
225227
event(value)
226228
float(value)
229+
230+
231+
def test_crosshair_can_hit_a_specific_date():
232+
# See https://github.com/HypothesisWorks/hypothesis/issues/4759.
233+
@given(st.dates())
234+
@settings(backend="crosshair", database=None)
235+
def f(d):
236+
assert d != dt.date(2030, 2, 14)
237+
238+
with pytest.raises(AssertionError):
239+
f()

‎requirements/coverage.txt‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
#
77
annotated-types==0.7.0
88
# via -r requirements/coverage.in
9-
black==26.5.0
9+
black==26.5.1
1010
# via -r requirements/coverage.in
11-
click==8.3.3
11+
click==8.4.1
1212
# via
1313
# -r requirements/coverage.in
1414
# black
15-
coverage[toml]==7.14.0
15+
coverage[toml]==7.14.1
1616
# via pytest-cov
1717
dpcontracts==0.6.0
1818
# via -r requirements/coverage.in
1919
execnet==2.1.2
2020
# via pytest-xdist
21-
fakeredis==2.35.1
21+
fakeredis==2.36.0
2222
# via -r requirements/coverage.in
2323
iniconfig==2.3.0
2424
# via pytest
@@ -28,7 +28,7 @@ libcst==1.8.6
2828
# via -r requirements/coverage.in
2929
mypy-extensions==1.1.0
3030
# via black
31-
numpy==2.4.5
31+
numpy==2.4.6
3232
# via
3333
# -r requirements/coverage.in
3434
# pandas
@@ -42,7 +42,7 @@ pathspec==1.1.1
4242
# via black
4343
pexpect==4.9.0
4444
# via -r requirements/test.in
45-
platformdirs==4.9.6
45+
platformdirs==4.10.0
4646
# via black
4747
pluggy==1.6.0
4848
# via
@@ -74,15 +74,15 @@ pytz==2026.2
7474
# via -r requirements/coverage.in
7575
pyyaml==6.0.3
7676
# via libcst
77-
redis==7.4.0
77+
redis==8.0.0
7878
# via fakeredis
7979
six==1.17.0
8080
# via python-dateutil
8181
sortedcontainers==2.4.0
8282
# via
8383
# fakeredis
8484
# hypothesis (hypothesis/pyproject.toml)
85-
syrupy==5.2.0
85+
syrupy==5.3.1
8686
# via -r requirements/test.in
8787
typing-extensions==4.15.0
8888
# via -r requirements/coverage.in

‎requirements/crosshair.txt‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ cattrs==26.1.0
1313
# via
1414
# lsprotocol
1515
# pygls
16-
crosshair-tool==0.0.104
16+
crosshair-tool==0.0.106
1717
# via
1818
# -r requirements/crosshair.in
1919
# hypothesis-crosshair
2020
execnet==2.1.2
2121
# via pytest-xdist
22-
hypothesis==6.152.7
22+
hypothesis==6.155.1
2323
# via hypothesis-crosshair
2424
hypothesis-crosshair==0.0.28
2525
# via -r requirements/crosshair.in
@@ -58,9 +58,9 @@ sortedcontainers==2.4.0
5858
# via
5959
# hypothesis
6060
# hypothesis (hypothesis/pyproject.toml)
61-
syrupy==5.2.0
61+
syrupy==5.3.1
6262
# via -r requirements/test.in
63-
typeshed-client==2.11.0
63+
typeshed-client==2.12.0
6464
# via crosshair-tool
6565
typing-extensions==4.15.0
6666
# via
@@ -72,5 +72,5 @@ typing-inspect==0.9.0
7272
# via crosshair-tool
7373
z3-solver==4.16.0.0
7474
# via crosshair-tool
75-
zipp==3.23.1
75+
zipp==4.1.0
7676
# via importlib-metadata

‎requirements/fuzzing.txt‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@ attrs==26.1.0
1212
# via
1313
# outcome
1414
# trio
15-
black==26.5.0
15+
black==26.5.1
1616
# via
1717
# -r requirements/coverage.in
1818
# hypofuzz
1919
# hypothesis
20-
click==8.3.3
20+
click==8.4.1
2121
# via
2222
# -r requirements/coverage.in
2323
# black
2424
# hypothesis
25-
coverage[toml]==7.14.0
25+
coverage[toml]==7.14.1
2626
# via
2727
# hypofuzz
2828
# pytest-cov
2929
dpcontracts==0.6.0
3030
# via -r requirements/coverage.in
3131
execnet==2.1.2
3232
# via pytest-xdist
33-
fakeredis==2.35.1
33+
fakeredis==2.36.0
3434
# via -r requirements/coverage.in
3535
h11==0.16.0
3636
# via
@@ -46,9 +46,9 @@ hyperframe==6.1.0
4646
# via h2
4747
hypofuzz==25.11.1
4848
# via -r requirements/fuzzing.in
49-
hypothesis[cli,watchdog]==6.152.7
49+
hypothesis[cli,watchdog]==6.155.1
5050
# via hypofuzz
51-
idna==3.15
51+
idna==3.18
5252
# via
5353
# anyio
5454
# trio
@@ -66,7 +66,7 @@ mdurl==0.1.2
6666
# via markdown-it-py
6767
mypy-extensions==1.1.0
6868
# via black
69-
numpy==2.4.5
69+
numpy==2.4.6
7070
# via
7171
# -r requirements/coverage.in
7272
# pandas
@@ -82,7 +82,7 @@ pathspec==1.1.1
8282
# via black
8383
pexpect==4.9.0
8484
# via -r requirements/test.in
85-
platformdirs==4.9.6
85+
platformdirs==4.10.0
8686
# via black
8787
pluggy==1.6.0
8888
# via
@@ -121,7 +121,7 @@ pytz==2026.2
121121
# via -r requirements/coverage.in
122122
pyyaml==6.0.3
123123
# via libcst
124-
redis==7.4.0
124+
redis==8.0.0
125125
# via fakeredis
126126
rich==15.0.0
127127
# via hypothesis
@@ -135,9 +135,9 @@ sortedcontainers==2.4.0
135135
# hypothesis
136136
# hypothesis (hypothesis/pyproject.toml)
137137
# trio
138-
starlette==1.0.0
138+
starlette==1.2.1
139139
# via hypofuzz
140-
syrupy==5.2.0
140+
syrupy==5.3.1
141141
# via -r requirements/test.in
142142
trio==0.33.0
143143
# via hypofuzz

0 commit comments

Comments
 (0)