-
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
register_type_strategy
doesn't seem to work on parameterized generic types
#3750
Comments
Yep, turns out that we have hard-coded logic for hypothesis/hypothesis-python/src/hypothesis/strategies/_internal/types.py Lines 327 to 347 in f73dab7
We should add a further bit of logic to use the registered strategy for |
Yea, I think I could do another PR, as long as I understand the right approach here.
|
Yep, that looks about right to me 😁 |
I'm looking at
So, it seems to me that the Kinda unrelated: |
After looking into the code further, and in line with my previous comment, I'm now thinking that the solution looks something more like (in
as opposed to just This presumably addresses other generic type cases as well, besides just tuple Of course, making this change naively causes a lot of other test cases to fail, so I'm sure that more needs to be done to get it to use |
That makes sense to me. It's been a while since I was working on the type resolution code, so the tests are probably a more reliable guide than me, but I think we probably usually want to use the registered thing if there is one 🤔. On the other hand, we might want to go with a minimal change that doesn't break existing code...
Missed this at the time, sorry.
|
Oh yea, I definitely don't intend to push something that breaks any tests. |
Is there a place or person I can ask general questions about Hypothesis? |
Best place for user questions is https://stackoverflow.com/questions/tagged/python-hypothesis ; for developer questions ask in an issue or PR comments 🙂 Sorry I've been slow to respond lately, it's just a v busy period at work. Often it's easier for me to comment if you open a PR with failing tests, so I can see exactly what code is having what effect. To answer the previous question about |
Ok, i made this pull request #3762 of the oversimplistic Another option is to only use |
Ok, sorry for all the churn in the other pull request #3762 , I see now that we only need to change logic in
So if we just check whether they are registered, and go with whatever is registered, prior to the normal tuple logic (which does stuff like
|
Oops! Yeah, I think we should either delete that registration, or change it to instead register a function which does all the tuple-resolving logic. |
I think in another recent issue i saw there was some way to have a registered strategy return a "don't know what to do" sentinel in cases where the registered strategy should be ignored, and some fallback should be used instead. Maybe that would be useful here? If no strategy is registered, it falls back to the current special casing logic. |
Ok I found it - d94243b I will have it so if |
Ok i created a pull request #3782 . I think it is mostly the right kind of thing, though I'll need to review the push guidelines before pushing. |
Ok, thanks for fixing this! |
The docs for
register_type_strategy
say that instead of registeringtuple[int]
, I can registertuple
with a function that takes the type and return the strategy. I may be misunderstanding, but I take this to mean that I can registerst.register_type_strategy(tuple, lambda typ: foo)
and then
foo
should be the default strategy for any param of type tuple, ortuple[int]
, ortuple[....]
, etc.However, the code below results in a test failure, and from my investigation it is clear that the registration is not effective, and that
test
is getting any oldtuple[int]
rather than using the strategy that was registered.I'm not sure if I'm using this correctly or if I understand the correct behavior. Is it simply impossible to register a default strategy for tuples that will work on parameterized tuples? If so, the documentation seems to suggest otherwise to me. Also, this seems like a useful feature to have. Or, is it that I am doing something wrong?
Thanks!
The text was updated successfully, but these errors were encountered: