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 6 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
16 changes: 14 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,15 @@ 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,
stacklevel=2,
)

tz_comps = _parse_hh_mm_ss_ff(tzstr)

if all(x == 0 for x in tz_comps):
Expand Down Expand Up @@ -1943,7 +1952,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`
8 changes: 7 additions & 1 deletion Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,13 @@ 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", 2);
}

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

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


const char *p = dt_ptr;

int year = 0, month = 0, day = 0;
Expand Down
Loading