Skip to content

gh-127260: Make more consistent to raising errors of fromisoformat methods in both implementations #130134

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

Merged
merged 5 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def _parse_isoformat_date(dtstr):


def _parse_hh_mm_ss_ff(tstr):
# Parses things of the form HH[:?MM[:?SS[{.,}fff[fff]]]]
# Parses things of the form HH[:?MM[:?SS[{.,:}fff[fff]]]]
len_str = len(tstr)

time_comps = [0, 0, 0, 0]
Expand All @@ -430,8 +430,8 @@ def _parse_hh_mm_ss_ff(tstr):
pos += has_sep

if pos < len_str:
if tstr[pos] not in '.,':
raise ValueError("Invalid microsecond component")
if tstr[pos] not in '.,:':
raise ValueError("Invalid microsecond separator")
else:
pos += 1

Expand Down Expand Up @@ -489,7 +489,7 @@ def _parse_isoformat_time(tstr):
# HH:MM:SS len: 8
# HH:MM:SS.f+ len: 10+

if len(tzstr) in (0, 1, 3):
if len(tzstr) in (0, 1, 3) or tstr[tz_pos-1] == 'Z':
raise ValueError("Malformed time zone string")

tz_comps = _parse_hh_mm_ss_ff(tzstr)
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -3533,6 +3533,7 @@ def test_fromisoformat_fails_datetime(self):
'2009-04-32T24:00:00.000000', # Day is invalid before wrapping due to 24:00
'2009-13-01T24:00:00.000000', # Month is invalid before wrapping due to 24:00
'9999-12-31T24:00:00.000000', # Year is invalid after wrapping due to 24:00
'2009-04-19T12:30Z12:00', # Extra time zone info after Z
]

for bad_str in bad_strs:
Expand Down Expand Up @@ -4658,6 +4659,8 @@ def test_fromisoformat_time_examples(self):
('00:00:00.000', self.theclass(0, 0)),
('000000.000000', self.theclass(0, 0)),
('00:00:00.000000', self.theclass(0, 0)),
('00:00:00,100000', self.theclass(0, 0, 0, 100000)),
('00:00:00:100000', self.theclass(0, 0, 0, 100000)),
('1200', self.theclass(12, 0)),
('12:00', self.theclass(12, 0)),
('120000', self.theclass(12, 0)),
Expand Down Expand Up @@ -4725,6 +4728,7 @@ def test_fromisoformat_fails(self):
'12:30:45.123456+12:00:30a', # Extra at end of full time
'12.5', # Decimal mark at end of hour
'12:30,5', # Decimal mark at end of minute
'12:30:45.123456Z12:00', # Extra time zone info after Z
]

for bad_str in bad_strs:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Make the occurrence of :meth:`datetime.time.fromisoformat` and
:meth:`datetime.datetime.fromisoformat` methods errors more consistent in
both implementations. Patch by Semyon Moroz.
Loading