Skip to content

Commit a2f4e4e

Browse files
Add deprecations-
1 parent b8367e7 commit a2f4e4e

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

Lib/_pydatetime.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import time as _time
1010
import math as _math
1111
import sys
12+
13+
import warnings
1214
from operator import index as _index
1315

1416
def _cmp(x, y):
@@ -452,7 +454,7 @@ def _parse_hh_mm_ss_ff(tstr):
452454

453455
return time_comps
454456

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

498+
if is_expanded and ":" not in tzstr and len(tzstr) > 2:
499+
import warnings
500+
warnings.warn(
501+
"Support for partially expanded formats are deprecated in "
502+
"accordance with ISO 8601:2 and will be removed in 3.15",
503+
DeprecationWarning
504+
)
505+
496506
tz_comps = _parse_hh_mm_ss_ff(tzstr)
497507

498508
if all(x == 0 for x in tz_comps):
@@ -1933,6 +1943,11 @@ def fromisoformat(cls, date_string):
19331943
# Split this at the separator
19341944
try:
19351945
separator_location = _find_isoformat_datetime_separator(date_string)
1946+
if separator_location != len(date_string) and date_string[separator_location] != 'T':
1947+
import warnings
1948+
warnings.warn("Support of date/time separators other than "
1949+
"[\"T\"] is deprecated in accordance with ISO "
1950+
"8601:2 and will be removed in 3.15", DeprecationWarning)
19361951
dstr = date_string[0:separator_location]
19371952
tstr = date_string[(separator_location+1):]
19381953

@@ -1943,7 +1958,10 @@ def fromisoformat(cls, date_string):
19431958

19441959
if tstr:
19451960
try:
1946-
time_components, became_next_day, error_from_components = _parse_isoformat_time(tstr)
1961+
is_expanded = date_string[4] == "-"
1962+
time_components, became_next_day, error_from_components = (
1963+
_parse_isoformat_time(tstr, is_expanded)
1964+
)
19471965
except ValueError:
19481966
raise ValueError(
19491967
f'Invalid isoformat string: {date_string!r}') from None

Modules/_datetimemodule.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,14 @@ parse_isoformat_time(const char *dtstr, size_t dtlen, int *hour, int *minute,
11301130
int tzsign = (*tzinfo_pos == '-') ? -1 : 1;
11311131
tzinfo_pos++;
11321132
int tzhour = 0, tzminute = 0, tzsecond = 0;
1133+
1134+
if (tzinfo_pos + 2 < p_end && tzinfo_pos[2] != ':' && strlen(tzinfo_pos) > 2) {
1135+
PyErr_WarnEx(PyExc_DeprecationWarning,
1136+
"Support for partially expanded formats is deprecated in "
1137+
"accordance with ISO 8601:2 and will be removed in 3.15",
1138+
1); // Level 1 warning
1139+
}
1140+
11331141
rv = parse_hh_mm_ss_ff(tzinfo_pos, p_end, &tzhour, &tzminute, &tzsecond,
11341142
tzmicrosecond);
11351143

@@ -5893,6 +5901,11 @@ datetime_fromisoformat(PyObject *cls, PyObject *dtstr)
58935901
const Py_ssize_t separator_location = _find_isoformat_datetime_separator(
58945902
dt_ptr, len);
58955903

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

58975910
const char *p = dt_ptr;
58985911

0 commit comments

Comments
 (0)