Skip to content

Commit 8dca9e1

Browse files
committed
Restructure timezones() so its draws can be re-encoded
timezone_keys() used to draw a zone and then flatmap into a choice of its available prefixed variants, and timezones() mapped ZoneInfo over the result - neither of which _invert can see through. Both are now a one_of over per-prefix branches, each a sampled_from of keys with the prefix (and, for timezones(), the ZoneInfo constructor) folded into its transformations: the branch selector is the prefix choice, so it still shrinks towards an unprefixed key - now by the branch ordering itself - and each branch re-encodes as a single index. The distribution is essentially unchanged: both shapes give unprefixed keys one third of the mass when both prefixes are available. The two-choice encoding is reordered from (zone, prefix) to (prefix, zone), so stored examples re-encode; inverting a ZoneInfo relies on instances being cached per key, since ZoneInfo compares by identity - which also means timezones(no_cache=True) values still cannot be re-encoded. With the default timezones() strategy now invertible, aware-bounded datetimes invert out of the box, and the frame-decision inverter from the previous commit is exercised by default. Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KbvBiusz22hjM4bxRcAtEB
1 parent 7e309df commit 8dca9e1

2 files changed

Lines changed: 85 additions & 37 deletions

File tree

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

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
OneOfStrategy,
3030
SampledFromStrategy,
3131
SearchStrategy,
32+
one_of,
3233
)
3334
from hypothesis.strategies._internal.utils import defines_strategy
3435

@@ -902,6 +903,37 @@ def _valid_key_cacheable(tzpath, key):
902903
return False
903904

904905

906+
def _timezone_key_strategies(*, allow_prefix):
907+
"""SampledFromStrategy branches for IANA keys: plain keys first, then one
908+
branch per allowed prefix, with the prefix applied as a sampled_from
909+
transformation. one_of's branch selector is therefore the prefix choice,
910+
which shrinks towards - and can be re-encoded as - an unprefixed key."""
911+
with warnings.catch_warnings():
912+
try:
913+
warnings.simplefilter("ignore", EncodingWarning)
914+
except NameError: # pragma: no cover
915+
pass
916+
# On Python 3.12 (and others?), `available_timezones()` opens files
917+
# without specifying an encoding - which our selftests make an error.
918+
available_timezones = ("UTC", *sorted(zoneinfo.available_timezones()))
919+
920+
# TODO: filter out alias and deprecated names if disallowed
921+
922+
def valid_key(key):
923+
return key == "UTC" or _valid_key_cacheable(zoneinfo.TZPATH, key)
924+
925+
# TODO: work out how to place a higher priority on "weird" timezones
926+
# For details see https://github.com/HypothesisWorks/hypothesis/issues/2414
927+
plain = [key for key in available_timezones if valid_key(key)]
928+
branches = [sampled_from(plain)]
929+
if allow_prefix:
930+
for prefix in ("posix", "right"):
931+
keys = [key for key in plain if valid_key(f"{prefix}/{key}")]
932+
if keys:
933+
branches.append(sampled_from(keys).map(f"{prefix}/{{}}".format))
934+
return branches
935+
936+
905937
@defines_strategy(force_reusable_values=True)
906938
def timezone_keys(
907939
*,
@@ -940,37 +972,7 @@ def timezone_keys(
940972
# check_type(bool, allow_alias, "allow_alias")
941973
# check_type(bool, allow_deprecated, "allow_deprecated")
942974
check_type(bool, allow_prefix, "allow_prefix")
943-
944-
with warnings.catch_warnings():
945-
try:
946-
warnings.simplefilter("ignore", EncodingWarning)
947-
except NameError: # pragma: no cover
948-
pass
949-
# On Python 3.12 (and others?), `available_timezones()` opens files
950-
# without specifying an encoding - which our selftests make an error.
951-
available_timezones = ("UTC", *sorted(zoneinfo.available_timezones()))
952-
953-
# TODO: filter out alias and deprecated names if disallowed
954-
955-
# When prefixes are allowed, we first choose a key and then flatmap to get our
956-
# choice with one of the available prefixes. That in turn means that we need
957-
# some logic to determine which prefixes are available for a given key:
958-
959-
def valid_key(key):
960-
return key == "UTC" or _valid_key_cacheable(zoneinfo.TZPATH, key)
961-
962-
# TODO: work out how to place a higher priority on "weird" timezones
963-
# For details see https://github.com/HypothesisWorks/hypothesis/issues/2414
964-
strategy = sampled_from([key for key in available_timezones if valid_key(key)])
965-
966-
if not allow_prefix:
967-
return strategy
968-
969-
def sample_with_prefixes(zone):
970-
keys_with_prefixes = (zone, f"posix/{zone}", f"right/{zone}")
971-
return sampled_from([key for key in keys_with_prefixes if valid_key(key)])
972-
973-
return strategy.flatmap(sample_with_prefixes)
975+
return one_of(_timezone_key_strategies(allow_prefix=allow_prefix))
974976

975977

976978
@defines_strategy(force_reusable_values=True)
@@ -989,8 +991,11 @@ def timezones(*, no_cache: bool = False) -> SearchStrategy["zoneinfo.ZoneInfo"]:
989991
``pip install hypothesis[zoneinfo]`` installs it, if and only if needed.
990992
"""
991993
check_type(bool, no_cache, "no_cache")
992-
return timezone_keys().map(
993-
zoneinfo.ZoneInfo.no_cache if no_cache else zoneinfo.ZoneInfo
994+
ctor = zoneinfo.ZoneInfo.no_cache if no_cache else zoneinfo.ZoneInfo
995+
# Mapping each sampled_from branch folds ctor into its transformations,
996+
# keeping the whole strategy re-encodable (unlike mapping the one_of).
997+
return one_of(
998+
[keys.map(ctor) for keys in _timezone_key_strategies(allow_prefix=True)]
994999
)
9951000

9961001

‎hypothesis/tests/cover/test_invert.py‎

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from hypothesis.internal.conjecture.data import ConjectureData
2525
from hypothesis.internal.conjecture.junkdrawer import equal_values
2626
from hypothesis.strategies._internal.lazy import LazyStrategy
27+
from hypothesis.strategies._internal.strategies import one_of
2728

2829
pytestmark = pytest.mark.skipif(
2930
settings().backend == "crosshair", reason="cannot _invert symbolic values"
@@ -239,11 +240,53 @@ def test_aware_datetimes_reproduce_imaginary_values_in_wall_clock_frame():
239240
assert_roundtrip(strategy, dt.datetime(2024, 3, 10, 2, 30, tzinfo=_NY))
240241

241242

242-
def test_aware_datetimes_with_default_timezones_do_not_invert():
243-
# the default timezones() strategy cannot re-encode its tzinfo draw
244-
strategy = st.datetimes(min_value=dt.datetime(2020, 1, 1, tzinfo=_UTC))
243+
@given(st.data())
244+
def test_aware_datetimes_with_default_timezones(data):
245+
strategy = st.datetimes(
246+
dt.datetime(2020, 1, 1, tzinfo=_UTC),
247+
dt.datetime(2021, 1, 1, tzinfo=_UTC),
248+
)
249+
check_roundtrip_many(strategy, data)
250+
251+
252+
@given(st.data())
253+
def test_timezone_keys(data):
254+
check_roundtrip_many(st.timezone_keys(), data)
255+
256+
257+
@given(st.data())
258+
def test_timezones(data):
259+
# inverting a ZoneInfo relies on instances being cached per key, so that
260+
# plain == (which ZoneInfo does not override) compares equal
261+
assert zoneinfo.ZoneInfo("America/New_York") is zoneinfo.ZoneInfo(
262+
"America/New_York"
263+
)
264+
check_roundtrip_many(st.timezones(), data)
265+
266+
267+
def test_prefixed_timezone_keys_invert(monkeypatch):
268+
# This environment may not ship posix/ or right/ tzdata files, so treat
269+
# every key as valid: each prefix then forms its own one_of branch, whose
270+
# selector re-encodes - and shrinks - the prefix choice.
271+
import hypothesis.strategies._internal.datetime as dtmodule
272+
273+
monkeypatch.setattr(dtmodule, "_valid_key_cacheable", lambda tzpath, key: True)
274+
branches = dtmodule._timezone_key_strategies(allow_prefix=True)
275+
assert len(branches) == 3
276+
strategy = one_of(branches)
277+
assert strategy._invert("UTC") == (0, 0)
278+
assert strategy._invert("posix/UTC") == (1, 0)
279+
assert strategy._invert("right/UTC") == (2, 0)
280+
assert_roundtrip(strategy, "posix/UTC")
281+
with pytest.raises(CannotInvert):
282+
strategy._invert("posix/no/such/zone")
283+
284+
285+
def test_no_cache_timezones_do_not_invert():
286+
# ZoneInfo.no_cache instances compare by identity, so a fresh instance
287+
# per draw can never equal the value being inverted
245288
with pytest.raises(CannotInvert):
246-
strategy._invert(dt.datetime(2020, 6, 1, tzinfo=_UTC))
289+
st.timezones(no_cache=True)._invert(zoneinfo.ZoneInfo("UTC"))
247290

248291

249292
@given(st.data())

0 commit comments

Comments
 (0)