-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat: add PastDate and FutureDate types #2425
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2425 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 25 25
Lines 5109 5141 +32
Branches 1050 1052 +2
=========================================
+ Hits 5109 5141 +32
Continue to review full report at Codecov.
|
|
@samuelcolvin Do I need to add it to the EDIT: nvm. |
|
@PrettyWood Do you have more comments? Thank you for the previous one btw :) |
PrettyWood
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.
Otherwise looks good :)
pydantic/types.py
Outdated
| # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DATE TYPES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
|
|
||
| class PastDate(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.
I reckon you should add
if TYPE_CHECKING:
PastDate = date
FutureDate = dateand add two lines in tests/mypy/modules/success.py in PydanticTypes class
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.
What's the purpose of the PydanticTypes?
Addressed, btw :)
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.
Just a dumb model with most custom pydantic types to ensure mypy is happy 😊
|
FastAPI test failing due to: E ImportError: cannot import name 'RowProxy' from 'sqlalchemy.engine.result' (/opt/hostedtoolcache/Python/3.7.10/x64/lib/python3.7/site-packages/sqlalchemy/engine/result.py)🤔 |
PrettyWood
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.
LGTM ! Thanks for making the changes.
Note: the CI error with fastapi has nothing to do with your PR don't worry ;)
|
@Kludex you can rebase on master to have the new CI running :) |
Done :) Thank you! |
PrettyWood
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.
Perfect thanks
pydantic/errors.py
Outdated
| 'DecimalWholeDigitsError', | ||
| 'DateTimeError', | ||
| 'DateError', | ||
| '_DateValueError', |
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.
| '_DateValueError', |
pydantic/errors.py
Outdated
|
|
||
| class DateNotInThePastError(_DateValueError): | ||
| code = 'date.not_in_the_past' | ||
| msg_template = 'date "{date}" is not in the past' |
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 #1421, we generally don't include the invalid value in error contexts. In v2 we will always include it.
For now in this case, to keep things simple and consistent with other errors, I think we should remove this and replace it with just date is not in the past.
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.
Do I still pass the value to the error?
class _DateValueError(PydanticValueError):
def __init__(self, *, date: date) -> None:
super().__init__(date=str(date))
tests/test_types.py
Outdated
| (datetime.today() + timedelta(1), str(date.today() + timedelta(1))), | ||
| ), | ||
| ) | ||
| def test_past_date_validation_fails(value, expected): |
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.
the second argument will not be required after the change requested above, but note for future reference: the second argument is not "expected" rather date_str or similar.
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.
Based on this comment, I'll assume the answer for my previous question is "no".
Also, thanks! :)
tests/test_types.py
Outdated
| (date.today(), str(date.today())), | ||
| (date.today() - timedelta(1), str(date.today() - timedelta(1))), | ||
| (datetime.today(), str(date.today())), | ||
| (datetime.today() - timedelta(1), str(date.today() - timedelta(1))), |
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.
Please include one explicit case '2064-06-01' or something so we have a hardcoded case.
If pydantic is still alive in 2064, and anyone is still alive to use it, they can fix the test then. 😨
|
please update. |
|
@samuelcolvin Changes applied. |
pydantic/errors.py
Outdated
| class _DateValueError(PydanticValueError): | ||
| def __init__(self) -> None: | ||
| super().__init__() |
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.
You probably don't need this anymore and can make DateNotInThePastError and DateNotInTheFutureError inherit from PydanticValueError
|
Thanks @PrettyWood 😗 |
|
@Kludex Can you just rebase/merge with master for the CI to run again properly? I reckon this PR can be merged now :) |
|
Thanks! |
Change Summary
Add
PastDateandFutureDatetypes.Related issue number
closes #769
Checklist
changes/<pull request or issue id>-<github username>.mdfile added describing change(see changes/README.md for details)