Skip to content
Open
Show file tree
Hide file tree
Changes from all 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=3,
)

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
2 changes: 2 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -3390,6 +3390,7 @@ def test_fromisoformat_timespecs(self):
dt_rt = self.theclass.fromisoformat(dtstr)
self.assertEqual(dt, dt_rt)

@warnings_helper.ignore_warnings(category=DeprecationWarning)
def test_fromisoformat_datetime_examples(self):
UTC = timezone.utc
BST = timezone(timedelta(hours=1), 'BST')
Expand Down Expand Up @@ -4706,6 +4707,7 @@ def test_fromisoformat_fractions(self):

self.assertEqual(actual, expected)

@warnings_helper.ignore_warnings(category=DeprecationWarning)
def test_fromisoformat_time_examples(self):
examples = [
('0000', self.theclass(0, 0)),
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`
7 changes: 7 additions & 0 deletions 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", 3);
}

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

Expand Down
Loading