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
make hypothesis aware of conlist length constraints #2876
Conversation
@Zac-HD can you please help me make this PR work? |
Ah, I just saw the note up the top of
What's the status of those Cython changes? And if I can't modify pydantic to make hypothesis understand |
I'm sorry, but I can't help make this work. Believe me, if I could it would already be done! Based on cython/cython#3537 and cython/cython#4005, it looks like the relevant changes to Cython have been made, but not yet released. I haven't actually tried building anything with Cython master (or It does sound like the same issue - if you have time (unfortunately I don't at the moment), you could try this patch and/or Re: workarounds, registering a strategy for the model you want using |
Those links are for For the workaround, you're saying to register the class whose attribute is a conlist?
Oh boy. My real model's going to have about 20 or 30 different |
Cython is not an interpreter, it's used to compile libraries such as Pydantic as native-code extensions for CPython. See this code in
Shouldn't be too hard, since you have access to the |
Ah I see. Once those other issues are resolved, is the code in this PR sufficient to to resolve #2876 ? |
Here's my workaround script. Zac, can you please tell me the best way to do the if statement condition? I couldn't find anything better than
|
Shorter workaround - note use of import pydantic
from pydantic import conlist
from typing import List, get_type_hints
from hypothesis import given, strategies as st
class Problem(pydantic.BaseModel):
x: int
y: List[str]
z: conlist(str, min_items=4, max_items=5)
a: conlist(conlist(float, min_items=1, max_items=2), max_items=1)
kwargs = {
k: st.lists(strat_from_type(t.item_type), min_size=t.min_items or 0, max_size=t.max_items)
for k, t in get_type_hints(Problem).items()
if issubclass(t, ConstrainedListValue) # let builds() handle the others
}
strat = st.builds(Problem, **kwargs)
st.register_type_strategy(Problem, strat) # you can work out other integrations or helper functions As a more general tip, it looks like you should check out - def build(**kwargs):
- return kwargs
- strat = st.builds(build, **{ ... })
+ strat = st.fixed_dictionaries({ ... }) |
I already tried something similar.
I think it comes from here.
Where How do I make |
Ah, sorry - we'll need to check |
Hi @mdavis-xyz |
The status is that the PR doesn't work because of a bug/limitation with cython. We need to wait until Cython releases a version with those patches. |
If this is now working, we can review it and consider merging, if it's still blocked by cython, let's close it and wait for V2 when this stuff should be fixed, partly via annotated-types. |
Still failing :-(. Let's wait to fix the problem in V2. |
Change Summary
This change should make hypothesis respect
min_items
andmax_items
inconlist
.But it doesn't currently, and I can't figure out why. I need help finishing this PR off.
Related issue number
#2875
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)
please review