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
Raise if register_random is passed unreferenced object #3509
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice! Thanks for taking this one 😁
@Zac-HD this looks like it is good to go. https://github.com/HypothesisWorks/hypothesis/actions/runs/3500864463/jobs/5863954158#step:6:539 is an unrelated flaky fail case. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤩
A pattern like
will lead to unexpected behavior: Hypothesis will instantly "forget" that it was ever passed an instance of
TorchRandomWrapper
. This is due to the fact thatregister_random
uses a weakref-valued dict, and here we passed it an unreferenced object, which immediately gets garbage collected after being passed toregister_random
.This PR will modify
register_random
to raise under these circumstances and to emit a warning when it looks like the input was referenced only within a temporary scope. It also fixes an overly-narrow type annotation forregister_random
so that it now permits structural subtypes ofrandom.Random
.This is the new behavior: