diff --git a/jsonschema/_format.py b/jsonschema/_format.py index fc44442dc..ce478648f 100644 --- a/jsonschema/_format.py +++ b/jsonschema/_format.py @@ -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) diff --git a/jsonschema/tests/test_jsonschema_test_suite.py b/jsonschema/tests/test_jsonschema_test_suite.py index 177d549b1..0f9698a64 100644 --- a/jsonschema/tests/test_jsonschema_test_suite.py +++ b/jsonschema/tests/test_jsonschema_test_suite.py @@ -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(), @@ -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", @@ -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( @@ -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( @@ -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( @@ -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",