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
Initial implementation of ConstrainedDate, condate #3740
Conversation
seems like using Would appreciate some guidance on whether this approach makes sense and how to add necessary changes to _hypothesis_plugin.py |
@Zac-HD any ideas? |
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.
Suggested fix below, though I haven't tested it. I'd also recommend renaming the class ConstrainedScalarMeta
, because a date (or datetime) is definitely not a number.
Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
Unsubscribing because this looks like standard "read CI logs and fix errors" territory; ping me again if you have a Hypothesis question. |
Thanks for the help and changes, I've now got initial version working and all tests pass. Some follow up questions for @samuelcolvin
Thanks! |
please review |
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.
Looking good, some things to change.
docs/usage/types.md
Outdated
### Arguments to `condate` | ||
The following arguments are available when using the `condate` type function | ||
|
||
- `strict_formats: List[str] = None`: list of datetime.datetime.strpformat style date formats that will be accepted |
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.
Let's remove this, it's not supported in V2 so it'll be confusing if we add it now in V1.
(the bounds constraints are already implemented in V2).
pydantic/types.py
Outdated
@@ -1087,32 +1090,78 @@ def to(self, unit: str) -> float: | |||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DATE TYPES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |||
|
|||
if TYPE_CHECKING: | |||
PastDate = date | |||
FutureDate = date | |||
PastDate = datetime.date |
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.
Best to revert these changes.
tests/test_condate.py
Outdated
@@ -0,0 +1,75 @@ | |||
from datetime import date, datetime |
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.
can you move this into test_datetime_parse.py
, We could do with rearranging tests, putting some tests in this file will cause more confusion.
pydantic/types.py
Outdated
update_not_none(field_schema, exclusiveMinimum=cls.gt, exclusiveMaximum=cls.lt, minimum=cls.ge, maximum=cls.le) | ||
|
||
@classmethod | ||
def strict_date_validator(cls, value: Any) -> datetime.date: |
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.
as per my above comment, remove this.
please update. |
you'll also need to rebase or merge master to get all a green tick. |
Hopefully that covers everything you mentioned :) Sorry if some of this overlaps with changes already envisioned for V2 And thanks for the great work! |
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.
Looking good but a few more things to fix.
… schema_json, add hypothesis test, etc
Hopefully nearly there now :) |
this is great, thank you so much. |
Change Summary
Initial implementation of ConstrainedDate, condate, with tests and docs
ConstrainedDate piggybacks off
ConstrainedNumberMeta
andnumber_size_validator
to implement the gt/ge/lt/le logicPerhaps those should be renamed to hint that they are not just applicable to numbers?
if accepted, more doc changes will be needed to highlight that date validation can be constrained
also if accepted suggest similar classes for datetime, time may be needed?
Many thanks for great library!
(p.s. I replaced date with datetime.date throughout types.py to avoid inevitable confusion that otherwise arises between datetime module vs datetime class)
Related issue number
fix #3739
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)