Skip to content

Improve date parsing #722

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

Merged
merged 8 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion jsonschema/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,18 @@ def is_regex(instance):
return re.compile(instance)


if hasattr(datetime.date, "fromisoformat"):
_is_date = datetime.date.fromisoformat
else:
def _is_date(instance):
return datetime.datetime.strptime(instance, "%Y-%m-%d")


@_checks_drafts(draft3="date", draft7="date", raises=ValueError)
def is_date(instance):
if not isinstance(instance, str):
return True
return datetime.datetime.strptime(instance, "%Y-%m-%d")
return _is_date(instance)


@_checks_drafts(draft3="time", raises=ValueError)
Expand Down
42 changes: 22 additions & 20 deletions jsonschema/tests/test_jsonschema_test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,24 @@ def narrow_unicode_build(test): # pragma: no cover
return


if sys.version_info < (3, 7):
message = "datetime.date.fromisoformat is new in 3.7+"

def missing_date_fromisoformat(test):
return skip(
message=message,
subject="date",
description="invalidates non-padded month dates",
)(test) or skip(
message=message,
subject="date",
description="invalidates non-padded day dates",
)(test)
else:
def missing_date_fromisoformat(test):
return


TestDraft3 = DRAFT3.to_unittest_testcase(
DRAFT3.tests(),
DRAFT3.format_tests(),
Expand All @@ -97,18 +115,9 @@ def narrow_unicode_build(test): # pragma: no cover
format_checker=draft3_format_checker,
skip=lambda test: (
narrow_unicode_build(test)
or missing_date_fromisoformat(test)
or missing_format(draft3_format_checker)(test)
or complex_email_validation(test)
or skip(
message=bug(685),
subject="date",
description="invalidates non-padded month dates",
)(test)
or skip(
message=bug(685),
subject="date",
description="invalidates non-padded day dates",
)(test)
or skip(
message="Upstream bug in strict_rfc3339",
subject="date-time",
Expand Down Expand Up @@ -158,6 +167,7 @@ def narrow_unicode_build(test): # pragma: no cover
format_checker=draft4_format_checker,
skip=lambda test: (
narrow_unicode_build(test)
or missing_date_fromisoformat(test)
or missing_format(draft4_format_checker)(test)
or complex_email_validation(test)
or skip(
Expand Down Expand Up @@ -237,6 +247,7 @@ def narrow_unicode_build(test): # pragma: no cover
format_checker=draft6_format_checker,
skip=lambda test: (
narrow_unicode_build(test)
or missing_date_fromisoformat(test)
or missing_format(draft6_format_checker)(test)
or complex_email_validation(test)
or skip(
Expand Down Expand Up @@ -337,6 +348,7 @@ def narrow_unicode_build(test): # pragma: no cover
format_checker=draft7_format_checker,
skip=lambda test: (
narrow_unicode_build(test)
or missing_date_fromisoformat(test)
or missing_format(draft7_format_checker)(test)
or complex_email_validation(test)
or skip(
Expand Down Expand Up @@ -368,16 +380,6 @@ def narrow_unicode_build(test): # pragma: no cover
subject="refRemote",
case_description="base URI change - change folder in subschema",
)(test)
or skip(
message=bug(685),
subject="date",
description="invalidates non-padded month dates",
)(test)
or skip(
message=bug(685),
subject="date",
description="invalidates non-padded day dates",
)(test)
or skip(
message="Upstream bug in strict_rfc3339",
subject="date-time",
Expand Down