Skip to content

gh-89157: Make C and Python implementation of datetime.date.fromisoformat consistent. #131007

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 7 commits into from
Apr 24, 2025
Merged
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
6 changes: 5 additions & 1 deletion Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,8 +1051,12 @@ def fromordinal(cls, n):
@classmethod
def fromisoformat(cls, date_string):
"""Construct a date from a string in ISO 8601 format."""

if not isinstance(date_string, str):
raise TypeError('fromisoformat: argument must be str')
raise TypeError('Argument must be a str')

if not date_string.isascii():
raise ValueError('Argument must be an ASCII str')

if len(date_string) not in (7, 8, 10):
raise ValueError(f'Invalid isoformat string: {date_string!r}')
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -2083,6 +2083,7 @@ def test_fromisoformat_fails(self):
'10000-W25-1', # Invalid year
'2020-W25-0', # Invalid day-of-week
'2020-W25-8', # Invalid day-of-week
'٢025-03-09' # Unicode characters
'2009\ud80002\ud80028', # Separators are surrogate codepoints
]

Expand Down Expand Up @@ -3534,7 +3535,7 @@ def test_fromisoformat_fails_datetime(self):
'2009-04-19T03:15:4500:00', # Bad time zone separator
'2009-04-19T03:15:45.123456+24:30', # Invalid time zone offset
'2009-04-19T03:15:45.123456-24:30', # Invalid negative offset
'2009-04-10ᛇᛇᛇᛇᛇ12:15', # Too many unicode separators
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this changed? The problem isn't that there are Unicode characters but that there is more than one of them. The old description was now accurate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a bit late now, it probably should have been asked a month ago when you approved this. In hindsight I agree the previous one was better.

'2009-04-10ᛇᛇᛇᛇᛇ12:15', # Unicode chars
'2009-04\ud80010T12:15', # Surrogate char in date
'2009-04-10T12\ud80015', # Surrogate char in time
'2009-04-19T1', # Incomplete hours
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Make the pure Python implementation of :func:`datetime.date.fromisoformat`,
only accept ASCII strings for consistency with the C implementation.
Loading