-
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
Strategies from type hints, and inference of missing arguments to builds() and @given() #643
Conversation
I feel this is one of those instances where writing the docs first might be a helpful strategy. I can't yet tell whether the ultimate effect of this would be:
|
I've worked on some tests here. Unfortunately most tests fail as it tries to test types such as I have a few question about implementation. When I was designing something similar to this (I didn't see the work already done), I quickly jumped to a custom mapping object. It was it made sense to start off with, the simple types mapping from Is there any particular reason you opted to functions instead of a general mapping object with a It seems the effort to go from what is currently implemented on zac's branch to a more object-oriented approach is extremely minimal, as I've been messing around with the module and commit mjsir911/hypothesis@f75fa8a is the point where I just moved around some code to fit the class methods. I do like the current implementation, what does what isn't clear at first but after a bit of time I got it. I think this would integrate nicely with the hypothesis library, the only problems I really foresee is how confusing the typing module seems to be and working around some restrictions inside of it. |
Oh yea and right now the only way to use anything is through backend functions. How do you expect to implement for using. I opted for a |
This is what I meant when I said on IRC that I was making @Zac-HD's life difficult by making the bar for this feature very high. :-) The The best design is probably going to be one which adds an additional decorator or two that specify in some manner which arguments to infer strategies for before you apply given to the problem. ...but this pull request doesn't need to handle any of that. It's just working on the actual conversion logic, with the goal of providing a function that can take a type and return a strategy. |
@mjsir911, I have actually handled the parametrised types - the lookup filter was just a little too severe. Using functions instead of classes in this situation seems to me mostly a matter of style, and I find it easier to experiment by composing functions than editing classes; it also helps encapsulate things into bite-sized pieces of logic. |
dee1bd6
to
abe9890
Compare
4ccaed6
to
bc71d64
Compare
e80dccb
to
a37b570
Compare
Sorry, I've been ill and am now at an academic summer school, so I haven't had time to review this properly. My initial impression is that this needs more work though, sorry. :-( I'm really not comfortable making this public API without the following two features:
I'm very very keen to make sure we don't end up with a sort of "two worlds" approach to strategies, where people mostly just use one or the other way of specifying data. Right now this seems to encourage them by making it quite awkward to switch. if I have e.g some large named tuple which I'm getting with One way to do this might be to pass arbitrary keywords from I'll try to do a detailed review, but it probably won't be possible before next week, sorry. |
Sorry, I see this is partly what your lookup argument is designed to do, but I don't actually think it works as a solution:
|
No need to apologise - I was warned very clearly before I started, and if we end up deciding not to merge anything at all I'll still have learned a lot! After thinking some more,
Does this sound sensible and address your concerns? |
86bd8fe
to
1917c76
Compare
BTW I've referenced this pull request in a recent article (the article isn't really about this pull request, but it was natural to use the features you're working on in the examples). |
Nice! Ironically I have almost no experience with type-driven property-based testing, but the Re-finalised changes, all on top of the changelog commit, are also ready for review again: In fact, I wrote a nifty decorator that makes The big one is converting to a single, global mapping from types to strategies - it's constructed at import time and there's no distinction between user-registered and default-registered types. Purely for ease of rebasing, it also includes the new (non-) handling for typevars and several fixes for robustness against future changes in the typing module and consequential fixes for 2.7/3.5/3.6 compatibility. Finally, it adds nice big comments to explain what's going on. Last, adding tests and moving a function to ensure this pull doesn't slow down calls to |
Confession: I have not done a final review in anything resembling depth. However, other than the things we discussed, I was pretty happy with this last time we looked at it. I'm now on holiday and didn't manage to get a proper review done before I left, and I know you're keen to get this merged, so I am prepared to leave the final merge up to your judgement. The public API is one I'm entirely happy with us committing to, the implementation is looking good, and I generally think we might as well get this out there and see how it goes. Good job, @Zac-HD. 🎉 |
e81a24b
to
a27a2de
Compare
This represents a slightly different approach, using a global mutable backup - so types registered by Hypothesis are treated just like types registered by the user. I've kept it as a separate commit to ease any future investigations :)
I tidied up the history a bit more, so the commit-by-commit view is as useful as possible in case of future debugging. And - after three months of work on this - I'm delighted to see everything go green and the release happen 😄 🎉 |
Closes #293. This pull:
from_type
to look up a strategy that can generate instances of the given typebuilds()
to infer missing arguments based on type hints@given
to infer missing arguments from type hintsregister_type_strategy
to register custom types that can't be automatically derived (based on a known child class or type hints, usingbuilds
)It's been a long time coming, but I think I'm done. (again 😉)