Skip to content

Conversation

@Kludex
Copy link
Member

@Kludex Kludex commented Feb 28, 2021

Change Summary

Add PastDate and FutureDate types.

Related issue number

closes #769

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/README.md for details)

@Kludex Kludex marked this pull request as draft February 28, 2021 18:59
@codecov
Copy link

codecov bot commented Feb 28, 2021

Codecov Report

Merging #2425 (016b585) into master (7b7e705) will not change coverage.
The diff coverage is 100.00%.

❗ Current head 016b585 differs from pull request most recent head 1a79b61. Consider uploading reports for the commit 1a79b61 to get more accurate results

@@            Coverage Diff            @@
##            master     #2425   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           25        25           
  Lines         5109      5141   +32     
  Branches      1050      1052    +2     
=========================================
+ Hits          5109      5141   +32     
Impacted Files Coverage Δ
pydantic/__init__.py 100.00% <ø> (ø)
pydantic/errors.py 100.00% <100.00%> (ø)
pydantic/types.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update aca18a9...1a79b61. Read the comment docs.

@Kludex
Copy link
Member Author

Kludex commented Feb 28, 2021

@samuelcolvin Do I need to add it to the schema_mapping.py? Just to confirm.

EDIT: nvm.

@Kludex Kludex marked this pull request as ready for review February 28, 2021 20:28
@Kludex
Copy link
Member Author

Kludex commented Mar 4, 2021

@PrettyWood Do you have more comments?

Thank you for the previous one btw :)

Copy link
Collaborator

@PrettyWood PrettyWood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise looks good :)

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DATE TYPES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


class PastDate(date):
Copy link
Collaborator

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 = date

and add two lines in tests/mypy/modules/success.py in PydanticTypes class

Copy link
Member Author

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 :)

Copy link
Collaborator

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 😊

@PrettyWood PrettyWood added the awaiting author revision awaiting changes from the PR author label Mar 15, 2021
@Kludex
Copy link
Member Author

Kludex commented Mar 15, 2021

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)

🤔

Copy link
Collaborator

@PrettyWood PrettyWood left a 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 ;)

@PrettyWood PrettyWood added ready for review and removed awaiting author revision awaiting changes from the PR author labels Mar 15, 2021
@PrettyWood
Copy link
Collaborator

@Kludex you can rebase on master to have the new CI running :)

@PrettyWood PrettyWood added awaiting author revision awaiting changes from the PR author and removed ready for review labels Apr 5, 2021
@Kludex
Copy link
Member Author

Kludex commented Apr 5, 2021

@Kludex you can rebase on master to have the new CI running :)

Done :) Thank you!

Copy link
Collaborator

@PrettyWood PrettyWood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect thanks

@PrettyWood PrettyWood removed the awaiting author revision awaiting changes from the PR author label Apr 5, 2021
'DecimalWholeDigitsError',
'DateTimeError',
'DateError',
'_DateValueError',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'_DateValueError',


class DateNotInThePastError(_DateValueError):
code = 'date.not_in_the_past'
msg_template = 'date "{date}" is not in the past'
Copy link
Member

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.

Copy link
Member Author

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))

(datetime.today() + timedelta(1), str(date.today() + timedelta(1))),
),
)
def test_past_date_validation_fails(value, expected):
Copy link
Member

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.

Copy link
Member Author

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! :)

(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))),
Copy link
Member

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. 😨

@samuelcolvin
Copy link
Member

please update.

@github-actions github-actions bot added awaiting author revision awaiting changes from the PR author and removed ready for review labels May 9, 2021
@Kludex
Copy link
Member Author

Kludex commented May 24, 2021

@samuelcolvin Changes applied.

Comment on lines 443 to 445
class _DateValueError(PydanticValueError):
def __init__(self) -> None:
super().__init__()
Copy link
Collaborator

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

@PrettyWood PrettyWood added ready for review and removed awaiting author revision awaiting changes from the PR author labels Jun 1, 2021
@Kludex
Copy link
Member Author

Kludex commented Jun 1, 2021

Thanks @PrettyWood 😗

@PrettyWood
Copy link
Collaborator

@Kludex Can you just rebase/merge with master for the CI to run again properly? I reckon this PR can be merged now :)

@PrettyWood PrettyWood merged commit a4cb4ee into pydantic:master Jun 4, 2021
@PrettyWood
Copy link
Collaborator

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exotic Type: DatePast and DateFuture

4 participants