|
12 | 12 | import operator as op |
13 | 13 | import warnings |
14 | 14 | import zoneinfo |
15 | | -from calendar import monthrange |
16 | 15 | from functools import cache, partial |
17 | 16 | from importlib import resources |
18 | 17 | from pathlib import Path |
@@ -88,26 +87,54 @@ def datetime_does_not_exist(value): |
88 | 87 | return value != roundtrip |
89 | 88 |
|
90 | 89 |
|
| 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 | + |
91 | 102 | def draw_capped_multipart( |
92 | 103 | data, min_value, max_value, duration_names=DATENAMES + TIMENAMES |
93 | 104 | ): |
94 | 105 | assert isinstance(min_value, (dt.date, dt.time, dt.datetime)) |
95 | 106 | assert type(min_value) == type(max_value) |
96 | 107 | 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 |
97 | 119 | result = {} |
98 | | - cap_low, cap_high = True, True |
99 | 120 | 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) |
104 | 131 | if name == "year": |
105 | 132 | val = data.draw_integer(low, high, shrink_towards=2000) |
106 | 133 | else: |
107 | 134 | val = data.draw_integer(low, high) |
108 | 135 | 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) |
111 | 138 | if hasattr(min_value, "fold"): |
112 | 139 | # The `fold` attribute is ignored in comparison of naive datetimes. |
113 | 140 | # In tz-aware datetimes it would require *very* invasive changes to |
|
0 commit comments