Skip to content

Commit 26b0617

Browse files
committed
fix: Python 3.11 date.fromisoformat() allows extra formats
Python 3.11 and later allow additional ISO8601 formats in `datetime` module ISO8601 parsing. These new formats are not RFC3339 section 5.6 compliant. Especially `datetime.date.fromisoformat()` now allows strings like: * `20230328` (2023-03-28) * `2022W527` (2023-01-01) * `2023-W01` (2023-01-02) * `2023-W13-2` (2023-03-28) Fix by doing a regular expression check before passing the value to `datetime` module. This made the original `.isascii()` check unnecessary. See: * https://docs.python.org/3/whatsnew/3.11.html#datetime * python/cpython@1303f8c927 * https://docs.python.org/3.11/library/datetime.html#datetime.date.fromisoformat * https://www.rfc-editor.org/rfc/rfc3339#section-5.6 Tests covering the invalid values to be sent to json-schema-org/JSON-Schema-Test-Suite Fixes python-jsonschema#1056.
1 parent d09feb6 commit 26b0617

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

jsonschema/_format.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
typing.Type[Exception], typing.Tuple[typing.Type[Exception], ...],
1818
]
1919

20+
_RE_DATE = re.compile(r"^\d{4}-\d{2}-\d{2}$", re.ASCII)
21+
2022

2123
class FormatChecker:
2224
"""
@@ -396,7 +398,7 @@ def is_regex(instance: object) -> bool:
396398
def is_date(instance: object) -> bool:
397399
if not isinstance(instance, str):
398400
return True
399-
return bool(instance.isascii() and datetime.date.fromisoformat(instance))
401+
return bool(_RE_DATE.fullmatch(instance) and datetime.date.fromisoformat(instance))
400402

401403

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

0 commit comments

Comments
 (0)