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

[Bug?] .filter ignored on just strategy #2964

Closed
mristin opened this issue May 16, 2021 · 2 comments · Fixed by #2965
Closed

[Bug?] .filter ignored on just strategy #2964

mristin opened this issue May 16, 2021 · 2 comments · Fixed by #2965
Labels
legibility make errors helpful and Hypothesis grokable

Comments

@mristin
Copy link
Contributor

mristin commented May 16, 2021

Hi,
I hope this has not been reported before. I checked the issues and read the changelog but could not find anything related and this seems to be related to issue #2036.

I noticed an unexpected difference in the behavior when .filter is applied on just strategy between Hypothesis 5.43.3 and Hypothesis 6.12.0 (the former ran on my machine, while the remote CI machine had 6.12.0 installed).

With Hypothesis 5.43.3 (expected behavior):

import hypothesis.strategies
import hypothesis.strategies._internal.strategies

strategy = hypothesis.strategies.just(1).filter(lambda x: x > 10)
assert str(strategy) == "just(1).filter(lambda x: x > 10)"
assert type(strategy) == hypothesis.strategies._internal.strategies.FilteredStrategy

With Hypothesis 6.12.0 (unexpected behavior):

import hypothesis.strategies
import hypothesis.strategies._internal.misc

strategy = hypothesis.strategies.just(1).filter(lambda x: x > 10)
assert str(strategy) == "just(1)"
assert type(strategy) == hypothesis.strategies._internal.misc.JustStrategy

This bug (?) is relevant for icontract-hypothesis when we test instance methods automatically where pre-conditions need to be applied on self. I'd expect the health check to be raised rather than the pre-conditions to be silently ignored.

@Zac-HD
Copy link
Member

Zac-HD commented May 17, 2021

This changed in #2688, version 5.43.7. The preconditions aren't silently ignored - so our bug is limited to an incorrect __repr__ for just() strategies, which I'll fix momentarily. The return type is not part of our public/stable API though - note the _internal! - and so to keep your check working you'll need to add a special case for _transformations on a SampledFromStrategy.

@Zac-HD Zac-HD added the legibility make errors helpful and Hypothesis grokable label May 17, 2021
@mristin
Copy link
Contributor Author

mristin commented May 17, 2021

Thanks a lot, @Zac-HD!

Just to confirm: I tried this in my Python console with Hypothesis 6.12.0 and indeed, the filter is applied:

>>> import hypothesis.strategies as st
>>> st.just(1).filter(lambda x: x > 10)
just(1)
>>> st.just(1).filter(lambda x: x > 10).example()
Traceback (most recent call last):
  ...
hypothesis.errors.Unsatisfiable: Unable to satisfy assumptions of hypothesis example_generating_inner_function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legibility make errors helpful and Hypothesis grokable
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants