Skip to content

gh-115783: Deprecate support for invalid ISO formats in datetime.fromisoformat #131522

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
11 changes: 6 additions & 5 deletions Doc/library/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1076,13 +1076,12 @@ Other constructors, all class methods:
ISO 8601 format, with the following exceptions:

1. Time zone offsets may have fractional seconds.
2. The ``T`` separator may be replaced by any single unicode character.
3. Fractional hours and minutes are not supported.
4. Reduced precision dates are not currently supported (``YYYY-MM``,
2. Fractional hours and minutes are not supported.
3. Reduced precision dates are not currently supported (``YYYY-MM``,
``YYYY``).
5. Extended date representations are not currently supported
4. Extended date representations are not currently supported
(``±YYYYYY-MM-DD``).
6. Ordinal dates are not currently supported (``YYYY-OOO``).
5. Ordinal dates are not currently supported (``YYYY-OOO``).

Examples::

Expand Down Expand Up @@ -1111,6 +1110,8 @@ Other constructors, all class methods:
.. versionchanged:: 3.11
Previously, this method only supported formats that could be emitted by
:meth:`date.isoformat` or :meth:`datetime.isoformat`.
.. versionchanged:: next
Separators other than ``T`` are deprecated.


.. classmethod:: datetime.fromisocalendar(year, week, day)
Expand Down
20 changes: 18 additions & 2 deletions Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def _parse_hh_mm_ss_ff(tstr):

return time_comps

def _parse_isoformat_time(tstr):
def _parse_isoformat_time(tstr, is_expanded=False):
# Format supported is HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]
len_str = len(tstr)
if len_str < 2:
Expand Down Expand Up @@ -493,6 +493,14 @@ def _parse_isoformat_time(tstr):
if len(tzstr) in (0, 1, 3) or tstr[tz_pos-1] == 'Z':
raise ValueError("Malformed time zone string")

if is_expanded and ":" not in tzstr and len(tzstr) > 2:
import warnings
warnings.warn(
"Support for partially expanded formats are deprecated in "
"accordance with ISO 8601:2 and will be removed in 3.15",
DeprecationWarning
)

tz_comps = _parse_hh_mm_ss_ff(tzstr)

if all(x == 0 for x in tz_comps):
Expand Down Expand Up @@ -1933,6 +1941,11 @@ def fromisoformat(cls, date_string):
# Split this at the separator
try:
separator_location = _find_isoformat_datetime_separator(date_string)
if separator_location != len(date_string) and date_string[separator_location] != 'T':
import warnings
warnings.warn("Support of date/time separators other than "
"[\"T\"] is deprecated in accordance with ISO "
"8601:2 and will be removed in 3.15", DeprecationWarning)
dstr = date_string[0:separator_location]
tstr = date_string[(separator_location+1):]

Expand All @@ -1943,7 +1956,10 @@ def fromisoformat(cls, date_string):

if tstr:
try:
time_components, became_next_day, error_from_components = _parse_isoformat_time(tstr)
is_expanded = date_string[4] == "-"
time_components, became_next_day, error_from_components = (
_parse_isoformat_time(tstr, is_expanded)
)
except ValueError:
raise ValueError(
f'Invalid isoformat string: {date_string!r}') from None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate support for invalid ISO formats in :func:`datetime.datetime.fromisoformat`
13 changes: 13 additions & 0 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,14 @@ parse_isoformat_time(const char *dtstr, size_t dtlen, int *hour, int *minute,
int tzsign = (*tzinfo_pos == '-') ? -1 : 1;
tzinfo_pos++;
int tzhour = 0, tzminute = 0, tzsecond = 0;

if (tzinfo_pos + 2 < p_end && tzinfo_pos[2] != ':' && strlen(tzinfo_pos) > 2) {
PyErr_WarnEx(PyExc_DeprecationWarning,
"Support for partially expanded formats is deprecated in "
"accordance with ISO 8601:2 and will be removed in 3.15",
1);
}

rv = parse_hh_mm_ss_ff(tzinfo_pos, p_end, &tzhour, &tzminute, &tzsecond,
tzmicrosecond);

Expand Down Expand Up @@ -5893,6 +5901,11 @@ datetime_fromisoformat(PyObject *cls, PyObject *dtstr)
const Py_ssize_t separator_location = _find_isoformat_datetime_separator(
dt_ptr, len);

if (separator_location != -1 && dt_ptr[separator_location] != 'T') {
PyErr_WarnEx(PyExc_DeprecationWarning,
"Support of date/time separators other than [\"T\"] is deprecated in "
"accordance with ISO 8601:2 and will be removed in 3.15", 1);
}

const char *p = dt_ptr;

Expand Down
Loading