Skip to content

Commit a0a7dfc

Browse files
Liam-DeVoeclaude
authored andcommitted
Report the failing assume() when .map() gives up
MappedStrategy.do_draw raised a bare UnsatisfiedAssumption after its retries, discarding the reason and location of the assume() that actually failed - so observability reported an empty status_reason and no status_reason_location. Re-raise the caught exception instead.
1 parent d010799 commit a0a7dfc

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,9 +1170,17 @@ def do_draw(self, data: ConjectureData) -> MappedTo:
11701170
result, self.pack, args=[x], kwargs={}
11711171
)
11721172
return result
1173-
except UnsatisfiedAssumption:
1173+
except UnsatisfiedAssumption as err:
1174+
# we want to preserve the err.location of the actual exception here if we
1175+
# re-throw it outside of the loop at the end. The alternative is a
1176+
# bare `raise UnsatisfiedAssumption` outside of the loop, which would
1177+
# drop the real err.location for observability.
1178+
#
1179+
# If we catch multiple exceptions here, we'll just report the location
1180+
# of the last one, which is a reasonable tradeoff for a singleton field.
1181+
failed_assumption = err
11741182
data.stop_span(discard=True)
1175-
raise UnsatisfiedAssumption
1183+
raise failed_assumption
11761184

11771185
def _invert(self, value: Any) -> tuple[ChoiceT, ...]:
11781186
# map() is not invertible in general, but a dict-like pack - e.g. the

‎hypothesis/tests/cover/test_observability.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,15 @@ def test_filter_has_status_reason_location(make):
356356
assert gave_up.metadata.status_reason_location == expected
357357

358358

359+
def test_assume_in_map_has_status_reason_location():
360+
def unsatisfiable(x):
361+
assume(len(str(x)) < 0)
362+
363+
expected = line_of(2)
364+
for gave_up in _get_filter_gave_ups(st.integers().map(unsatisfiable)):
365+
assert gave_up.metadata.status_reason_location == expected
366+
367+
359368
def test_sampled_from_transform_without_data_records_nothing():
360369
# covers the data=None branch of SampledFromStrategy._transform
361370
s = st.sampled_from([1, 2]).filter(lambda x: x == 2)

0 commit comments

Comments
 (0)