From 423137cefe091b63166bb0a4531fef8b9c4eaf0a Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Thu, 1 Feb 2018 17:20:06 +0000 Subject: [PATCH] allow pything dates and times (#122) * allow pything dates and times * update history --- HISTORY.rst | 4 ++++ pydantic/datetime_parse.py | 12 +++++++++--- pydantic/version.py | 2 +- tests/test_datetime_parse.py | 2 ++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index e67a926..13ac9c6 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -3,6 +3,10 @@ History ------- +v0.6.4 (2018-02-01) +................... +* allow python date and times objects #122 + v0.6.3 (2017-11-26) ................... * fix direct install without ``README.rst`` present diff --git a/pydantic/datetime_parse.py b/pydantic/datetime_parse.py index f5b7f76..7bcb13a 100644 --- a/pydantic/datetime_parse.py +++ b/pydantic/datetime_parse.py @@ -80,13 +80,16 @@ def from_unix_seconds(seconds: int) -> datetime: return dt.replace(tzinfo=timezone.utc) -def parse_date(value: StrIntFloat) -> date: +def parse_date(value: Union[date, StrIntFloat]) -> date: """ Parse a string and return a datetime.date. Raise ValueError if the input is well formatted but not a valid date. Raise ValueError if the input isn't well formatted. """ + if isinstance(value, date): + return value + number = get_numeric(value) if number: return from_unix_seconds(number).date() @@ -99,7 +102,7 @@ def parse_date(value: StrIntFloat) -> date: return date(**kw) -def parse_time(value: StrIntFloat) -> time: +def parse_time(value: Union[time, StrIntFloat]) -> time: """ Parse a string and return a datetime.time. @@ -108,6 +111,9 @@ def parse_time(value: StrIntFloat) -> time: Raise ValueError if the input is well formatted but not a valid time. Raise ValueError if the input isn't well formatted, in particular if it contains an offset. """ + if isinstance(value, time): + return value + match = time_re.match(value) if not match: raise ValueError('Invalid time format') @@ -119,7 +125,7 @@ def parse_time(value: StrIntFloat) -> time: return time(**kw) -def parse_datetime(value: StrIntFloat) -> datetime: +def parse_datetime(value: Union[datetime, StrIntFloat]) -> datetime: """ Parse a string and return a datetime.datetime. diff --git a/pydantic/version.py b/pydantic/version.py index 96f9300..03ad616 100644 --- a/pydantic/version.py +++ b/pydantic/version.py @@ -2,4 +2,4 @@ from distutils.version import StrictVersion __all__ = ['VERSION'] -VERSION = StrictVersion('0.6.3') +VERSION = StrictVersion('0.6.4') diff --git a/tests/test_datetime_parse.py b/tests/test_datetime_parse.py index 3bf69aa..1b35437 100644 --- a/tests/test_datetime_parse.py +++ b/tests/test_datetime_parse.py @@ -25,6 +25,7 @@ def create_tz(minutes): (1494012444, date(2017, 5, 5)), ('2012-04-23', date(2012, 4, 23)), ('2012-4-9', date(2012, 4, 9)), + (date(2012, 4, 9), date(2012, 4, 9)), # Invalid inputs ('x20120423', ValueError), ('2012-04-56', ValueError), @@ -43,6 +44,7 @@ def test_date_parsing(value, result): ('10:10', time(10, 10)), ('10:20:30.400', time(10, 20, 30, 400000)), ('4:8:16', time(4, 8, 16)), + (time(4, 8, 16), time(4, 8, 16)), # Invalid inputs ('091500', ValueError), ('09:15:90', ValueError),