-
Notifications
You must be signed in to change notification settings - Fork 657
Add internal _invert method to most strategies
#4743
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
Open
Liam-DeVoe
wants to merge
9
commits into
HypothesisWorks:master
Choose a base branch
from
Liam-DeVoe:add-strategy-invert
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cff3bf5
claude: add internal _invert method to most strategies
Liam-DeVoe 191ad47
claude: apply shed formatting to test_invert.py
Liam-DeVoe a14f517
claude: add conjecture-coverage tests for invert_many
Liam-DeVoe 365605f
claude: fix crosshair CI failures in test_invert.py
Liam-DeVoe 35a53f6
Merge remote-tracking branch 'upstream/master' into add-strategy-invert
Liam-DeVoe d4cc782
claude: skip _invert tests under crosshair backend
Liam-DeVoe 1a079c7
address review
Liam-DeVoe 520a90a
claude: fix test_builds_zero_arg signature for @given+parametrize
Liam-DeVoe 78ca3a3
claude: move test_deep_equal to tests/conjecture and add set/dict mis…
Liam-DeVoe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| RELEASE_TYPE: patch | ||
|
|
||
| Add an internal helper for use in future work. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -16,8 +16,10 @@ | |||||
| from functools import cache, partial | ||||||
| from importlib import resources | ||||||
| from pathlib import Path | ||||||
| from typing import Any | ||||||
|
|
||||||
| from hypothesis.errors import InvalidArgument | ||||||
| from hypothesis.errors import DefinitelyCannotInvert, InvalidArgument | ||||||
| from hypothesis.internal.conjecture.choice import ChoiceT | ||||||
| from hypothesis.internal.validation import check_type, check_valid_interval | ||||||
| from hypothesis.strategies._internal.core import sampled_from | ||||||
| from hypothesis.strategies._internal.misc import just, none, nothing | ||||||
|
|
@@ -159,6 +161,29 @@ def draw_naive_datetime_and_combine(self, data, tz): | |||||
| f"{self.max_value!r} with timezone from {self.tz_strat!r}." | ||||||
| ) | ||||||
|
|
||||||
| def _invert(self, value: Any) -> tuple[ChoiceT, ...]: | ||||||
| if type(value) is not dt.datetime: | ||||||
| raise DefinitelyCannotInvert(f"{value!r} is not a datetime") | ||||||
| naive = value.replace(tzinfo=None) | ||||||
| if not (self.min_value <= naive <= self.max_value): | ||||||
| raise DefinitelyCannotInvert( | ||||||
| f"{value!r} outside [{self.min_value!r}, {self.max_value!r}]" | ||||||
| ) | ||||||
| if not self.allow_imaginary and datetime_does_not_exist(value): | ||||||
| raise DefinitelyCannotInvert(f"{value!r} is an imaginary datetime") | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| tz_inv = self.tz_strat._invert(value.tzinfo) | ||||||
| return ( | ||||||
| *tz_inv, | ||||||
| value.year, | ||||||
| value.month, | ||||||
| value.day, | ||||||
| value.hour, | ||||||
| value.minute, | ||||||
| value.second, | ||||||
| value.microsecond, | ||||||
| value.fold, | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| @defines_strategy(force_reusable_values=True) | ||||||
| def datetimes( | ||||||
|
|
@@ -230,6 +255,26 @@ def do_draw(self, data): | |||||
| tz = data.draw(self.tz_strat) | ||||||
| return dt.time(**result, tzinfo=tz) | ||||||
|
|
||||||
| def _invert(self, value: Any) -> tuple[ChoiceT, ...]: | ||||||
| if type(value) is not dt.time: | ||||||
| raise DefinitelyCannotInvert(f"{value!r} is not a time") | ||||||
| naive = value.replace(tzinfo=None) | ||||||
| if not (self.min_value <= naive <= self.max_value): | ||||||
| raise DefinitelyCannotInvert( | ||||||
| f"{value!r} outside [{self.min_value!r}, {self.max_value!r}]" | ||||||
| ) | ||||||
| # draw_capped_multipart emits fold *before* tz_strat draws (because | ||||||
| # min_value=dt.time has fold), but do_draw draws tz AFTER. | ||||||
|
Comment on lines
+266
to
+267
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am confused by this comment. |
||||||
| tz_inv = self.tz_strat._invert(value.tzinfo) | ||||||
| return ( | ||||||
| value.hour, | ||||||
| value.minute, | ||||||
| value.second, | ||||||
| value.microsecond, | ||||||
| value.fold, | ||||||
| *tz_inv, | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| @defines_strategy(force_reusable_values=True) | ||||||
| def times( | ||||||
|
|
@@ -271,6 +316,15 @@ def do_draw(self, data): | |||||
| **draw_capped_multipart(data, self.min_value, self.max_value, DATENAMES) | ||||||
| ) | ||||||
|
|
||||||
| def _invert(self, value: Any) -> tuple[ChoiceT, ...]: | ||||||
| if type(value) is not dt.date: | ||||||
| raise DefinitelyCannotInvert(f"{value!r} is not a date") | ||||||
| if not (self.min_value <= value <= self.max_value): | ||||||
| raise DefinitelyCannotInvert( | ||||||
| f"{value!r} outside [{self.min_value!r}, {self.max_value!r}]" | ||||||
| ) | ||||||
| return (value.year, value.month, value.day) | ||||||
|
|
||||||
| def filter(self, condition): | ||||||
| if ( | ||||||
| isinstance(condition, partial) | ||||||
|
|
@@ -343,6 +397,15 @@ def do_draw(self, data): | |||||
| high_bound = high_bound and val == high | ||||||
| return dt.timedelta(**result) | ||||||
|
|
||||||
| def _invert(self, value: Any) -> tuple[ChoiceT, ...]: | ||||||
| if type(value) is not dt.timedelta: | ||||||
| raise DefinitelyCannotInvert(f"{value!r} is not a timedelta") | ||||||
| if not (self.min_value <= value <= self.max_value): | ||||||
| raise DefinitelyCannotInvert( | ||||||
| f"{value!r} outside [{self.min_value!r}, {self.max_value!r}]" | ||||||
| ) | ||||||
| return (value.days, value.seconds, value.microseconds) | ||||||
|
|
||||||
|
|
||||||
| @defines_strategy(force_reusable_values=True) | ||||||
| def timedeltas( | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Thinking about the experience of debugging "why doesn't this invert???", I think we might want to use an explicit loop here, so that we can
exc.add_note(f"at {idx=} of {value!r}, strategy={self}"); raiseand thus see the trace through the tree of strategies to the specific problem.Annoying now, I know, but I think investing in the pattern and maybe a helper fn will pay off later.