-
Notifications
You must be signed in to change notification settings - Fork 586
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
Flaky tests caused by mishandling of global PRNG state #1266
Comments
|
I think the proper solution will avoid causing this reseeding pollution in the first place. For example, a new context manager that can be used in @contextlib.context_manager
def deterministic_context(seed=0):
state = random.getstate()
random.seed(seed)
try:
yield
finally:
random.setstate(state)And then ban direct use of Note: this is equivalent to building the |
|
@Zac-HD At that point, I think you'd be happier with an autouse pytest fixture. It's much more foolproof, and I feel sure you won't regret it. In import pytest
import random
@pytest.fixture(autouse=True)
def deterministic_random():
random.seed(0)If you want to assert that seed isn't used, you can take it a bit farther: import pytest
import random
@pytest.yield_fixture(autouse=True)
def deterministic_random():
random.seed(0)
seed = random.seed
del random.seed
yield
random.seed = seed |
To be clear, there's no reseeding involved in the problem above. The test does no seeding at all, but other tests in the module do, so it has differing behavior when run alone or along with all tests in the module. |
|
I mean that the other tests should not have the side-effect of changing the |
|
Note: fixing this issue will remove the last reason to use the Edit: if we integrate that into the internal runner mechanisms, which looks impractical. |
|
@Zac-HD Is there a ticket for that idea? How would I generate a pareto-random number in that world? Right now I can use st.randoms() and random.pareto(). |
|
This issue is the ticket! I've assigned myself to make that clearer 😄 In that world you can use |
Wait, what? It might remove our last use of it, but the |
|
Sorry, looks like I need to actually write out my chain of logic here 😊
Before doing some deeper investigation I had hoped it would be practical to integrate the |
|
I agree that we should reset the random module state after given rather than letting |
|
I'll open a pull to reset state in a few days when life calms down a little 😄 We agree on the actual purpose of hypothesis/hypothesis-python/src/hypothesis/strategies.py Lines 1002 to 1009 in d522674
|
Yes, sorry, that's what I meant by "the |
I've been running into this problem: https://paste.pound-python.org/show/qchV5aFbMkvs0uoYfArd/
This particular test has a
@flakydecorator but no@givendecorator. Despite the decorator, the problem was happening 100% reproducibly for me, and upon debugging, I found that the stdlib random singleton was in a deterministic state. Further, when running the test alone (-k "half and endpoint"), the test passed, and no longer had determinism.This is caused by cross-test pollution. The
@givendecorator results inrandom.seed(0)running, so that the prior test causes the next test to be deterministic even though it wouldn't normally.I'm not sure of the best fix, but this patch fixes the problem for me, and obviates the
@flakydecorator too:The text was updated successfully, but these errors were encountered: