@@ -179,6 +179,11 @@ def test_datetimes(data):
179179_UTC = zoneinfo .ZoneInfo ("UTC" )
180180
181181
182+ class _RaisingTzinfo (dt .tzinfo ):
183+ def utcoffset (self , value ):
184+ raise RuntimeError ("broken tzinfo" )
185+
186+
182187@given (st .data ())
183188def test_aware_datetimes (data ):
184189 tz = data .draw (st .sampled_from ([_UTC , _NY ]))
@@ -507,6 +512,34 @@ def test_roundtrip_explicit(strategy, value):
507512 2024 , 3 , 10 , 2 , 30 , tzinfo = zoneinfo .ZoneInfo ("America/New_York" )
508513 ),
509514 ),
515+ # an imaginary value inside aware instant bounds
516+ (
517+ st .datetimes (
518+ dt .datetime (2024 , 1 , 1 , tzinfo = _NY ),
519+ dt .datetime (2025 , 1 , 1 , tzinfo = _NY ),
520+ timezones = st .just (_NY ),
521+ allow_imaginary = False ,
522+ ),
523+ dt .datetime (2024 , 3 , 10 , 2 , 30 , tzinfo = _NY ),
524+ ),
525+ # inside the instant bounds, but outside the wall-clock drawing window
526+ (
527+ st .datetimes (
528+ dt .datetime (2020 , 10 , 30 , 12 , tzinfo = _NY ),
529+ dt .datetime (2020 , 11 , 1 , 1 , 15 , fold = 1 , tzinfo = _NY ),
530+ timezones = st .just (_NY ),
531+ ),
532+ dt .datetime (2020 , 11 , 1 , 1 , 30 , tzinfo = _NY ),
533+ ),
534+ # a tzinfo whose utcoffset() raises cannot be located at all
535+ (
536+ st .datetimes (
537+ dt .datetime (2020 , 1 , 1 , tzinfo = _UTC ),
538+ dt .datetime (2021 , 1 , 1 , tzinfo = _UTC ),
539+ timezones = st .just (_UTC ),
540+ ),
541+ dt .datetime (2020 , 6 , 1 , tzinfo = _RaisingTzinfo ()),
542+ ),
510543 (st .timedeltas (), "not a timedelta" ),
511544 (st .timedeltas (max_value = dt .timedelta (days = 1 )), dt .timedelta (days = 10 )),
512545 (st .just (42 ), 41 ),
@@ -519,13 +552,75 @@ def test_roundtrip_explicit(strategy, value):
519552 (st .permutations ([1 , 2 , 3 ]), [1 , 2 , 4 ]),
520553 (st .permutations ([1 , 2 , 3 ]), [1 , 2 ]),
521554 (st .builds (_Pair , x = st .integers (), y = st .integers ()), "not a Pair" ),
555+ (st .builds (_Pair , x = st .integers (), y = st .integers ()), _Pair ("bad" , 2 )),
556+ (st .tuples (st .integers ()), ("not an int" ,)),
557+ (st .lists (st .nothing ()), [1 ]),
558+ (st .lists (st .integers (), unique_by = lambda x : x .key ), [1 , 2 ]),
559+ (st .lists (st .sampled_from ([1 , 2 , 3 ]), unique = True ), "not a list" ),
560+ (st .lists (st .sampled_from ([1 , 2 , 3 ]), unique = True , min_size = 2 ), [1 ]),
561+ (st .dictionaries (st .sampled_from ([1 , 2 , 3 ]), st .integers ()), {1 : "x" }),
562+ (st .fixed_dictionaries ({"a" : st .integers ()}), "not a dict" ),
563+ (
564+ st .fixed_dictionaries ({"a" : st .booleans ()}, optional = {"b" : st .integers ()}),
565+ {"a" : True , "b" : "not an int" },
566+ ),
522567 ],
523568)
524569def test_out_of_image_raises (strategy , value ):
525570 with pytest .raises (CannotInvert ):
526571 strategy ._invert (value )
527572
528573
574+ def test_empty_element_strategy_inverts_only_the_empty_list ():
575+ assert st .lists (st .nothing ())._invert ([]) == ()
576+
577+
578+ def test_unique_list_with_tuple_suffixes_rejects_malformed_values ():
579+ # dictionaries() maps dict over a unique list of (key, value) pairs; go
580+ # through the inner list strategy directly to check its shape guards.
581+ dicts = st .dictionaries (st .integers (), st .integers ())
582+ inner = dicts .wrapped_strategy .wrapped_strategy .mapped_strategy
583+ with pytest .raises (CannotInvert ):
584+ inner ._invert ("not a list" )
585+ with pytest .raises (CannotInvert ):
586+ inner ._invert ([1 , 2 ])
587+
588+
589+ def test_aware_datetimes_with_unrepresentable_bound_cannot_invert ():
590+ # every wall time representable in UTC-00:30 is after this strategy's
591+ # maximum instant, so no drawing window exists in that timezone
592+ strategy = st .datetimes (
593+ dt .datetime (1 , 1 , 1 , 0 , 0 , tzinfo = dt .timezone .utc ),
594+ dt .datetime (1 , 1 , 1 , 0 , 20 , tzinfo = dt .timezone .utc ),
595+ timezones = st .just (dt .timezone .utc ),
596+ ).wrapped_strategy
597+ tz = dt .timezone (- dt .timedelta (minutes = 30 ))
598+ value = dt .datetime (1 , 1 , 1 , 0 , 10 , tzinfo = dt .timezone .utc )
599+ with pytest .raises (CannotInvert ):
600+ strategy ._invert_aware_fields (value , tz , imaginary = False )
601+
602+
603+ # Bounds inside the same DST fold are in inverted wall-clock order, so this
604+ # strategy draws in the UTC frame; see the roundtrip test of the same name.
605+ _utc_frame_strategy = st .datetimes (
606+ dt .datetime (2020 , 11 , 1 , 1 , 59 , tzinfo = _NY , fold = 0 ),
607+ dt .datetime (2020 , 11 , 1 , 1 , 1 , tzinfo = _NY , fold = 1 ),
608+ timezones = st .just (_NY ),
609+ ).wrapped_strategy
610+
611+
612+ def test_utc_frame_cannot_encode_imaginary_values ():
613+ value = dt .datetime (2020 , 11 , 1 , 1 , 30 , tzinfo = _NY )
614+ with pytest .raises (CannotInvert ):
615+ _utc_frame_strategy ._invert_aware_fields (value , _NY , imaginary = True )
616+
617+
618+ def test_utc_frame_cannot_encode_values_which_overflow_utc ():
619+ value = dt .datetime .max .replace (tzinfo = dt .timezone (- dt .timedelta (hours = 1 )))
620+ with pytest .raises (CannotInvert ):
621+ _utc_frame_strategy ._invert_aware_fields (value , _NY , imaginary = False )
622+
623+
529624@pytest .mark .parametrize (
530625 "strategy,value" ,
531626 [
0 commit comments