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

Merge Bundle as subclass of SampledFromStrategy #4084

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

reaganjlee
Copy link
Contributor

@reaganjlee reaganjlee commented Aug 12, 2024

Seems like the initial values of the states get reversed with the new implementation where e.g.

state.fail_fast(a1=a_5, a2=a_4, a3=a_3, a4=a_2, a5=a_1, a6=a_0, b1=b_2, b2=b_1, b3=b_0) become ...(a1=a_0, a2=a_1, a3=a_2, a4=a_3, a5=a_4, a6=a_5, b1=b_0, b2=b_1, b3=b_2). I'll probably just need to just spend more time looking into the Rule strategy and SampledFromStrategy

Closes #3944

@reaganjlee reaganjlee marked this pull request as draft August 12, 2024 02:51
@reaganjlee reaganjlee force-pushed the consumes branch 2 times, most recently from 6c08dc0 to e2e7f93 Compare August 12, 2024 02:59
@Zac-HD
Copy link
Member

Zac-HD commented Aug 19, 2024

👋 thanks for taking this on Reagan!

Quick note: I generally review PRs when either CI is green, or the contributor explicitly requests a review; happy to do that here too. (although I'll be offline from later this week until early September for a camping trip and thus not reviewing then)

@reaganjlee
Copy link
Contributor Author

Of course and thanks for the note! I'll make a few more attempts at this final test and let you know otherwise. Either way hope you have a good time on your trip!

@reaganjlee
Copy link
Contributor Author

@Zac-HD Mind taking a look to see if it's going in the right direction? Please excuse all the extra prints haha

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 pretty good overall! obviously not done yet, but seems promising 🙂

hypothesis-python/src/hypothesis/stateful.py Outdated Show resolved Hide resolved
@reaganjlee
Copy link
Contributor Author

Don't think I fully understand the logs These lines were always and only run by failing examples? I imagine it's something with the test design, though I can't reproduce locally. Besides that, think everything else is good for review

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.

Forgive the somewhat rambly notes, I figured reviewed was better than not 🙂

Overall looks pretty good, though I think it'd be helpful to have a test where we define a consumes bundle which can recursively draw from itself (🤯) no, that makes no sense, bundles just pick a known element so they can't be recursive.

The These lines were always and only run by failing examples thing is from Phase.explain; if you disable that in the settings for these tests it'll stop.

Finally, thanks again Reagan - this has turned out to be a lot tricker than I was expecting, and I really appreciate your contribution here! I'm sure many users of stateful testing will agree with me too.

hypothesis-python/src/hypothesis/stateful.py Outdated Show resolved Hide resolved
hypothesis-python/src/hypothesis/stateful.py Outdated Show resolved Hide resolved
hypothesis-python/RELEASE.rst Outdated Show resolved Hide resolved
hypothesis-python/src/hypothesis/stateful.py Outdated Show resolved Hide resolved
hypothesis-python/src/hypothesis/stateful.py Outdated Show resolved Hide resolved
@reaganjlee
Copy link
Contributor Author

Thanks so much for the fast review! I'll continue editing tomorrow but hope you have a great time on your trip!

@reaganjlee
Copy link
Contributor Author

reaganjlee commented Aug 26, 2024

After taking some time to think about it, I can't seem to make a useful recursive test. Any thoughts here? Maybe a potential use case would be helpful and I can see if I can finish the rest.

Also the last failing tests seems unrelated

@Zac-HD

This comment was marked as outdated.

@reaganjlee

This comment was marked as outdated.

@Zac-HD
Copy link
Member

Zac-HD commented Sep 25, 2024

Having just tried to sketch a 'recursive bundle'... there's no such thing, bundles draw from a flat list of values that can't include invoking bundle draws 🤦. So I think all we need is to see CI passing without those weird timeouts, and then I'll do a from-scratch review to see where we're up to - probably pretty close!

If you want to get something out sooner, I'd be happy to take a refactoring-only PR which does the "delete BundleReference" etc cleanups; that should be pretty straightforward and would shrink this one a bit for easier handling and review. Not doing that is also fine of course!

@reaganjlee reaganjlee force-pushed the consumes branch 7 times, most recently from 2cef0f5 to cefcea8 Compare October 11, 2024 07:32
@reaganjlee reaganjlee marked this pull request as ready for review October 11, 2024 07:37
@reaganjlee
Copy link
Contributor Author

Should be good for review now!

@reaganjlee reaganjlee changed the title WIP: Merge Bundle as subclass of SampledFromStrategy Merge Bundle as subclass of SampledFromStrategy Oct 12, 2024
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.

Another short review, sorry! Tests look good, though we should add one to check that mixing bundles, consumes, map, and filter all work as expected.

hypothesis-python/src/hypothesis/stateful.py Outdated Show resolved Hide resolved
@reaganjlee
Copy link
Contributor Author

@Zac-HD This should be good for another look!

@Zac-HD
Copy link
Member

Zac-HD commented Nov 13, 2024

(I haven't forgotten this! Still on my to-review queue, unfortunately just large and complicated enough that I haven't yet found a good time to sit down with it...)

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.

OK, I think we're almost there - and again, I'm sorry this has taken me so long. Once we've handled the comments below and added some more tests, I'll run some manual tests to confirm that everything works 'in the wild', and then... 🚀

hypothesis-python/src/hypothesis/stateful.py Outdated Show resolved Hide resolved
Comment on lines 549 to 554
if not self.draw_references:
return self.get_transformed_value(reference)

# we need both reference and the value itself to pretty-print deterministically
# and maintain any transformations that is bundle-specific
return VarReferenceMapping(reference, self.get_transformed_value(reference))
Copy link
Member

Choose a reason for hiding this comment

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

Note that SampledFromStrategy._transform can return a special constant filter_not_satisfied; we'll need to handle that possibility - and write some tests that we never actually return that value to the user.

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.

we'll need to rebase so I've skimmed over the tests again and will review implementation after that.

Comment on lines 1331 to 1349
a = Bundle("a")

@initialize(target=a)
def initialize(self):
return multiple(1, 2, 3)

@rule(
a1=a.filter(lambda x: x < 2),
a2=a.filter(lambda x: x > 2),
a3=a,
)
Copy link
Member

Choose a reason for hiding this comment

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

For clarity, I suggest naming the bundle val and the arguments a, b, c - it's easier to misread the error logs when every single name matches a_?\d!

assert isinstance(bun, int)
assert bun >= 0

Machine.TestCase.settings = Settings(stateful_step_count=5, max_examples=10)
Copy link
Member

Choose a reason for hiding this comment

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

I'd leave the default max_examples= here; because swarm testing (disabling a random subset of rules) makes coverage less reliable than you might think for small n.

hypothesis-python/tests/cover/test_stateful.py Outdated Show resolved Hide resolved
hypothesis-python/tests/cover/test_stateful.py Outdated Show resolved Hide resolved
@reaganjlee
Copy link
Contributor Author

@Zac-HD This should be good

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.

Support consumes(some_bundle).filter(...) for stateful tests
2 participants