Best practices for generating classes

1 view
Skip to first unread message

Axel Vestin

unread,
Dec 16, 2025, 12:01:31 PM (13 days ago) Dec 16
to Hypothesis users
Hello! I'm trying out Hypothesis and PBT for a function that filters a list.

Each item in the list has the following information:
An identifying string
A time where it was issued
A time interval during which it is valid

But it may also have the following extra information:
A reference to another item in the list which should be considered cancelled.
A time interval during which the cancellation is in effect.

I wanted to try to use Hypothesis to make sure that all cases that can occur are handled, which would take many test cases using traditional test methods.

For now I tried writing a very naïve strategy that returns a valid time interval and a cancellation time interval, i've appended it below.

What I'm wondering is; How can I write this code such that I let Hypothesis generate a list where the above holds true? Or am I going about this in a wrong way?


@st.composite
def future_complete_cancellation(
draw,
) -> tuple[dt.datetime, dt.datetime, dt.datetime, dt.datetime, dt.datetime]:
current_time = draw(st_time())

issue_time = draw(st_time(min=current_time + dt.timedelta(minutes=1)))
valid_from = draw(st_time(min=issue_time))
valid_to = draw(st_time(min=valid_from + dt.timedelta(minutes=1)))

cancel_start = draw(st_time(min=issue_time, max=valid_from))
cancel_issue_time = draw(st_time(min=issue_time, max=cancel_start))
cancel_end = draw(st_time(min=valid_to))

return (
current_time,
issue_time,
valid_from,
valid_to,
cancel_issue_time,
cancel_start,
cancel_end,
)


@given(future_complete_cancellation())
@settings(max_examples=5000)
def test_future_cancellation(scenario):
(
current_time,
issue_time,
valid_from,
valid_to,
cancel_issue_time,
cancel_start,
cancel_end,
) = scenario

t01 = get_sigmet(
seq_no="T01", issue_time=issue_time, valid_from=valid_from, valid_to=valid_to
)

t02 = get_cancel_sigmet(
seq_no="T02",
cancel_seq_no=t01.sequence_number,
issue_time=cancel_issue_time,
cancel_from=cancel_start,
cancel_to=cancel_end,
)

output = filter_current_sigmets([t01, t02], current_time)

assert len(output) == 0

Reply all
Reply to author
Forward
0 new messages