-
Notifications
You must be signed in to change notification settings - Fork 587
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
Support annotated_types
in st.from_type
#3780
Conversation
hypothesis-python/src/hypothesis/strategies/_internal/annotated_types.py
Outdated
Show resolved
Hide resolved
517543f
to
90cffb5
Compare
I think we should carefully avoid changing how strategies are inferred, and only use filters for this. Otherwise, we'll break the ability to register custom strategies for any of the types we expect might be annotated, such as datetimes! Implementation-wise, we'd limit our changes to: hypothesis/hypothesis-python/src/hypothesis/strategies/_internal/types.py Lines 368 to 369 in 058420a
such that the only new code we'd have looks something like: annotated_type, *constraints = args
strategy = st.from_type(annotated_type)
for pred in constraints_to_filter_predicates:
strategy = strategy.filter(pred) # rewriting usually makes this efficient
return strategy The contribution of this PR would thus be to implement Once merged, I'll write up an issue for the feature-rewriting we'll want to add to make e.g. length bounds more efficient - the trick is that we can convert the constraints to callable objects which can also be recognised by the various filter methods. I think we can also rewrite length-constraining lambdas, so long as we keep the predicate just in case len was redefined... but that's for later. |
This sounds reasonable, the only issue is having timezones as a filter to As you can see what I do currently is only special case times and datetimes to get the base strategy, otherwise I switch to |
Ouch, yeah. Let's dodge that by just not supporting I'm keen to work it out eventually, but imo it's much better to ship the parts we won't regret asap and spend as long as we need on the others. |
Let me know if the actual implementation suits you, I'll then add tests |
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.
Looks reasonable! I'll review again once we've got tests and a changelog entry so that CI passes 😁
hypothesis-python/src/hypothesis/strategies/_internal/annotated_types.py
Outdated
Show resolved
Hide resolved
b6d088f
to
f2d0c9b
Compare
(Still need to figure out how to test all constraints) We probably need to tweak CI to test with |
hypothesis-python/src/hypothesis/strategies/_internal/annotated_types.py
Outdated
Show resolved
Hide resolved
hypothesis-python/src/hypothesis/strategies/_internal/annotated_types.py
Outdated
Show resolved
Hide resolved
hypothesis-python/src/hypothesis/strategies/_internal/annotated_types.py
Outdated
Show resolved
Hide resolved
4aae5a0
to
75ec43b
Compare
75ec43b
to
a6188be
Compare
3736d6f
to
49784cd
Compare
44be8eb
to
ba929b9
Compare
f7a85c8
to
99000b8
Compare
Thanks for finishing the implementation @Zac-HD! I'll try to think about solutions to:
|
Fixes #3356.
This is very much work in progress for now