Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LazySequenceCopy.pop(i) #3987

Merged
merged 8 commits into from
May 15, 2024

Conversation

tybug
Copy link
Member

@tybug tybug commented May 13, 2024

UniqueSampledListStrategy uses LazySequenceCopy to pop elements from the list. Because LazySequenceCopy only implemented pop-from-end, we swapped the element we wanted to pop to the end before popping. But this leaves the list in a bad state for shrinking: if i = 0, we just swapped the last element, which is the most complicated in shrinking order, to the front of the list, leaving it there for future passes. This means an ir tree of [0, 0, 0] indicating the first three elements of the sequence corresponds to {0, 8, 9} for sets(range(10)).

The solution is properly implementing .pop(i). I've applied this in other places we use the swap trick, though I'm less confident than UniqueSampledListStrategy that this is not a behavioral change.

@tybug tybug requested a review from Zac-HD as a code owner May 13, 2024 15:58
Copy link
Member

@Zac-HD Zac-HD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

Let's wait for #3988, then rebase this on master to fix the merge conflict.

Comment on lines +83 to +86
# this would be more robust as a stateful test, where each rule is a list operation
# on (1) the canonical python list and (2) its LazySequenceCopy. We would assert
# that the return values and lists match after each rule, and the original list
# is unmodified.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this would be nice but is not merge-blocking important.

hypothesis-python/RELEASE.rst Outdated Show resolved Hide resolved
@tybug tybug enabled auto-merge May 14, 2024 04:42
Copy link
Contributor

@jobh jobh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

shrunk = minimal(st.sampled_from(LargeFlag), lambda f: bit_count(f.value) > 1)
# Ideal would be (bit0 | bit1), but:
# minimal(st.sets(st.sampled_from(range(10)), min_size=3)) == {0, 8, 9} # not {0, 1, 2}
assert shrunk == LargeFlag.bit0 | LargeFlag.bit63 # documents actual behaviour
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I remember scratching my head over this!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks to your comment here, it was the only reason I looked into this 🙂

@Zac-HD
Copy link
Member

Zac-HD commented May 15, 2024

Looks like we're no longer triggering the KeyError case from our conjecture-coverage tests:

try:
return self.__data_cache_ir[key]
except KeyError:
pass

@tybug
Copy link
Member Author

tybug commented May 15, 2024

that coverage was hopefully fixed in #3991. I've rebased on master, let's see how it goes

@tybug tybug merged commit d603bd4 into HypothesisWorks:master May 15, 2024
54 checks passed
@tybug tybug deleted the lazy-sequence-copy-pop branch May 15, 2024 02:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants