-
-
Notifications
You must be signed in to change notification settings - Fork 142
Allow users to override hardcoded hypothesis strategies
#2066
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
Allow users to override hardcoded hypothesis strategies
#2066
Conversation
Did not know about this earlier. Move in the tests to `test_laws_resolution.py`, since they are testing `laws`, not anything about `hypothesis`.
Want to make this pure and user-overridable.
This package seems to put callers above callees.
This way, the deeper code doesn't need to know about defaults and overrides. It just deals with `Settings`.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #2066 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 80 81 +1
Lines 2485 2576 +91
Branches 437 44 -393
==========================================
+ Hits 2485 2576 +91 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
sobolevn
left a comment
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.
Thanks, the work you are doing is really impressive. Please, let me know when to make a new release.
| Checking laws is not compatible with ``pytest-xdist``, | ||
| because we use a lot of global mutable state there. | ||
| Please, use ``returns_lawful`` marker | ||
| to exclude them from ``pytest-xdist`` execution plan. |
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.
We can also make a note about running them on a single node.
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.
Ah, this was an existing paragraph that I moved up. I don't have anything to add here.
| check_all_laws( | ||
| _Wrapper, | ||
| container_strategy=st.builds(_Wrapper, st.integers()), | ||
| other_strategies=other_strategies, |
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.
maybe register_strategies? other feels a bit off to me.
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.
Agreed. How about type_strategies?
Thanks! I appreciate your speedy responses too. Really helps keep the cycle time low. I'll test-drive it for another week or so, just to make sure I don't run into any more issues that need fixing :) |
sobolevn
left a comment
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.
Thank you!
@sobolevn I didn't run into any more problems :) We should be good for a new release. Thanks! |
|
Awesome! Scheduled for tomorrow. |
|
|
Override all the things!
The previous PR (#2061) allowed users to specify the container strategy. However, there's still a snag: the plugin hardcodes special strategies for
TypeVarandCallable. These cannot be overridden by users, which is a problem when a law needs some generic value other than the container under test.So, this PR allows users to override any of the strategies used by the plugin. It decouples the test-generation code from the hardcoded strategies by using a first-class value of
Settings. The hardcoded strategies are in the default values ofSettingsand can be overridden without trouble.Rationale
Why accept extra strategies within
check_all_laws? Why not ask the user to register the strategies withhypothesisin the user's test file?Our
hypothesisplugin doesn't run tests. It creates tests and sticks them in the user's module.So, even if I try:
that sets the context only when defining the tests, not when running them. When the tests are run, the types are no longer mapped to the strategies. We could ask the user to set the context when
pytestruns the tests, but that would be error-prone and tedious for the user. So, we have to set the context inside the created test. Hence this PR.Why accept extra strategies?
Because the alternative is to write them to the
hypothesisglobal variable, and global variables are evil.Why can't the user register a strategy for
KindNdirectly? It usesTypeVarTuple, whichhypothesisdoesn't understand. So,hypothesisthinksKindNis not a "subclass" ofKindN[int, str, bool], because the number of type arguments don't match. Sad.As a workaround, we need to write those kinds of laws as
x: Callable[[], KindN[F, ...]]instead ofx: KindN[F, ...]. That way, at least ourCallableoverride can catch these types.Implementation
laws.py:Settingsvalue.Settingsinto a default value (with the hardcoded strategies) and a user-provided value.Monoidwould have been useful for this - hint hint :) )Settingsinline. I believe it will be auto-included in the docs page.Test Plan
Run
test_custom_strategy_for_callable.pyafter adding print statements to see the generated examples. (I don't know of other ways to see the examples in this case, since there is so much dynamically added context.)Before
The parameter
container: KindN[_F_Applicative, ...]is always bound to a_Wrappereven though_Wrapperis not even anApplicative! (This is because_Wrapper <: KindN, sadly.)After
Pass in a strategy for
Callable. Now, we getcontainerasMaybes andResults.Checklist
CHANGELOG.md