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

Mutable arguments and lazy definition of sampled_from interact poorly #2507

Closed
DRMacIver opened this issue Jul 23, 2020 · 0 comments · Fixed by #2515
Closed

Mutable arguments and lazy definition of sampled_from interact poorly #2507

DRMacIver opened this issue Jul 23, 2020 · 0 comments · Fixed by #2515

Comments

@DRMacIver
Copy link
Member

I discovered an unpleasant surprise when debugging #2505

Consider the following two tests:

from hypothesis import given, strategies as st

@given(st.data())
def test_mutability_1(data):
    x = [1]
    s = st.sampled_from(x)
    x.append(2)
    assert data.draw(s) != 2


@given(st.data())
def test_mutability_2(data):
    x = [1]
    s = st.sampled_from(x)
    assert data.draw(s) != 2
    x.append(2)
    assert data.draw(s) != 2

Currently the first one fails and the second one passes.

Why? Well because in the first one the lazy strategy containing the values of x is instantiated at the point of the draw after the append(2). in the second one it's instantiated before it.

I think sampled from is the only place where this is currently a significant issue, so this can probably be fixed by removing the defined_strategy from sampled_from, but we need to handle some of its special casing seperately to make sure it has a good repr then.

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 a pull request may close this issue.

1 participant