Title: datetime.fromisoformat() raises AssertionError instead of ValueError in the pure-Python implementation · Issue #152060 · python/cpython · GitHub
Open Graph Title: datetime.fromisoformat() raises AssertionError instead of ValueError in the pure-Python implementation · Issue #152060 · python/cpython
X Title: datetime.fromisoformat() raises AssertionError instead of ValueError in the pure-Python implementation · Issue #152060 · python/cpython
Description: Bug report Bug description: The pure-Python datetime.fromisoformat() raises a bare AssertionError for some malformed strings, where the C accelerator correctly raises ValueError. fromisoformat is documented to raise ValueError for invali...
Open Graph Description: Bug report Bug description: The pure-Python datetime.fromisoformat() raises a bare AssertionError for some malformed strings, where the C accelerator correctly raises ValueError. fromisoformat is d...
X Description: Bug report Bug description: The pure-Python datetime.fromisoformat() raises a bare AssertionError for some malformed strings, where the C accelerator correctly raises ValueError. fromisoformat is d...
Opengraph URL: https://github.com/python/cpython/issues/152060
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"datetime.fromisoformat() raises AssertionError instead of ValueError in the pure-Python implementation","articleBody":"# Bug report\n\n### Bug description:\n\nThe pure-Python `datetime.fromisoformat()` raises a bare `AssertionError` for some\nmalformed strings, where the C accelerator correctly raises `ValueError`. `fromisoformat`\nis documented to raise `ValueError` for invalid input only.\n\n```python\n>>> import _pydatetime, _datetime\n>>> _pydatetime.datetime.fromisoformat('2020-2020')\nTraceback (most recent call last):\n ...\nAssertionError\n>>> _datetime.datetime.fromisoformat('2020-2020')\nTraceback (most recent call last):\n ...\nValueError: Invalid isoformat string: '2020-2020'\n```\n\n`'2020-1234'` triggers the same divergence.\n\nBecause the failing check is an `assert`, it is stripped under `python -O`, so the\nbehaviour also differs by build flag:\n\n```\n$ python -O -c \"import _pydatetime; _pydatetime.datetime.fromisoformat('2020-2020')\"\nValueError: Invalid isoformat string: '2020-2020'\n```\n\n### Root cause\n\nFor a 9-character input such as `'2020-2020'`, `datetime.fromisoformat()` calls\n`_find_isoformat_datetime_separator()`, which (seeing a `-` at index 4 and a non-`W` at\nindex 5) returns 10 — the `YYYY-MM-DD` branch. The date portion slice is then the full\n9-character string, and `_parse_isoformat_date()` trips its\n`assert len(dtstr) in (7, 8, 10)`. The caller's `except ValueError` does not catch\n`AssertionError`, so it escapes.\n\nThis is the sibling assert site to gh-151770 (an assert reached via the `24:00`\nmidnight-rollover path with an out-of-range month), which was fixed in #151771. The\npresent site is a different location and a different input class.\n\n### Suggested fix\n\nReplace the bare assert in `_parse_isoformat_date()` with an explicit `ValueError` whose\nmessage matches the C implementation. `date.fromisoformat()` already length-checks before\ncalling `_parse_isoformat_date()`, so it is unaffected; `datetime.fromisoformat()`\nre-wraps the `ValueError` with the full input string, producing the same message as the C\naccelerator.\n\nI have a patch and a regression test (added to `test_fromisoformat_fails_datetime`, which\nruns under both the C and pure-Python test classes) ready.\n\n### CPython versions tested on:\n\n3.16 (main, built from source)\n\n### Operating systems tested on:\n\nmacOS\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-152061\n* gh-152081\n* gh-152786\n* gh-152787\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/tonghuaroot","@type":"Person","name":"tonghuaroot"},"datePublished":"2026-06-24T10:46:13Z","url":"https://github.com/python/cpython/issues/152060"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:14887073-ba2c-4e87-258c-26a7a564077d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9484:19AA62:399FD6D:4924556:6B01F79B |
| html-safe-nonce | 85ffb00baa477544d1341a96208cde7b2aa6d50af4e671ce80ccdce362375135 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NDg0OjE5QUE2MjozOTlGRDZEOjQ5MjQ1NTY6NkIwMUY3OUIiLCJ2aXNpdG9yX2lkIjoiNzE2NjE4MTc5NzM1MTUyMDE1NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 30b05147c7b495dd22c830707a5a2c318e94f87cf333c9e7006942f71d42929a |
| hovercard-subject-tag | issue:4734058361 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/152060/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1e60b9ce3a07b5300721bf5d48546c361ad18a34f9e5010fdbc680ca079358f4/python/cpython/issues/152060 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1e60b9ce3a07b5300721bf5d48546c361ad18a34f9e5010fdbc680ca079358f4/python/cpython/issues/152060 |
| og:image:alt | Bug report Bug description: The pure-Python datetime.fromisoformat() raises a bare AssertionError for some malformed strings, where the C accelerator correctly raises ValueError. fromisoformat is d... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tonghuaroot |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width