Skip to content

Commit 756de12

Browse files
authored
Merge pull request #722 from helrond/issue-685
Improve date parsing
2 parents ff1ddcf + d9d5fe1 commit 756de12

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

jsonschema/_format.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,18 @@ def is_regex(instance):
319319
return re.compile(instance)
320320

321321

322+
if hasattr(datetime.date, "fromisoformat"):
323+
_is_date = datetime.date.fromisoformat
324+
else:
325+
def _is_date(instance):
326+
return datetime.datetime.strptime(instance, "%Y-%m-%d")
327+
328+
322329
@_checks_drafts(draft3="date", draft7="date", raises=ValueError)
323330
def is_date(instance):
324331
if not isinstance(instance, str):
325332
return True
326-
return datetime.datetime.strptime(instance, "%Y-%m-%d")
333+
return _is_date(instance)
327334

328335

329336
@_checks_drafts(draft3="time", raises=ValueError)

jsonschema/tests/test_jsonschema_test_suite.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ def narrow_unicode_build(test): # pragma: no cover
8787
return
8888

8989

90+
if sys.version_info < (3, 7):
91+
message = "datetime.date.fromisoformat is new in 3.7+"
92+
93+
def missing_date_fromisoformat(test):
94+
return skip(
95+
message=message,
96+
subject="date",
97+
description="invalidates non-padded month dates",
98+
)(test) or skip(
99+
message=message,
100+
subject="date",
101+
description="invalidates non-padded day dates",
102+
)(test)
103+
else:
104+
def missing_date_fromisoformat(test):
105+
return
106+
107+
90108
TestDraft3 = DRAFT3.to_unittest_testcase(
91109
DRAFT3.tests(),
92110
DRAFT3.format_tests(),
@@ -97,18 +115,9 @@ def narrow_unicode_build(test): # pragma: no cover
97115
format_checker=draft3_format_checker,
98116
skip=lambda test: (
99117
narrow_unicode_build(test)
118+
or missing_date_fromisoformat(test)
100119
or missing_format(draft3_format_checker)(test)
101120
or complex_email_validation(test)
102-
or skip(
103-
message=bug(685),
104-
subject="date",
105-
description="invalidates non-padded month dates",
106-
)(test)
107-
or skip(
108-
message=bug(685),
109-
subject="date",
110-
description="invalidates non-padded day dates",
111-
)(test)
112121
or skip(
113122
message="Upstream bug in strict_rfc3339",
114123
subject="date-time",
@@ -158,6 +167,7 @@ def narrow_unicode_build(test): # pragma: no cover
158167
format_checker=draft4_format_checker,
159168
skip=lambda test: (
160169
narrow_unicode_build(test)
170+
or missing_date_fromisoformat(test)
161171
or missing_format(draft4_format_checker)(test)
162172
or complex_email_validation(test)
163173
or skip(
@@ -237,6 +247,7 @@ def narrow_unicode_build(test): # pragma: no cover
237247
format_checker=draft6_format_checker,
238248
skip=lambda test: (
239249
narrow_unicode_build(test)
250+
or missing_date_fromisoformat(test)
240251
or missing_format(draft6_format_checker)(test)
241252
or complex_email_validation(test)
242253
or skip(
@@ -337,6 +348,7 @@ def narrow_unicode_build(test): # pragma: no cover
337348
format_checker=draft7_format_checker,
338349
skip=lambda test: (
339350
narrow_unicode_build(test)
351+
or missing_date_fromisoformat(test)
340352
or missing_format(draft7_format_checker)(test)
341353
or complex_email_validation(test)
342354
or skip(
@@ -368,16 +380,6 @@ def narrow_unicode_build(test): # pragma: no cover
368380
subject="refRemote",
369381
case_description="base URI change - change folder in subschema",
370382
)(test)
371-
or skip(
372-
message=bug(685),
373-
subject="date",
374-
description="invalidates non-padded month dates",
375-
)(test)
376-
or skip(
377-
message=bug(685),
378-
subject="date",
379-
description="invalidates non-padded day dates",
380-
)(test)
381383
or skip(
382384
message="Upstream bug in strict_rfc3339",
383385
subject="date-time",

0 commit comments

Comments
 (0)