Title: date.fromisoformat() silently ignores the last two characters of a 10-character basic-format string · Issue #158500 · python/cpython · GitHub
Open Graph Title: date.fromisoformat() silently ignores the last two characters of a 10-character basic-format string · Issue #158500 · python/cpython
X Title: date.fromisoformat() silently ignores the last two characters of a 10-character basic-format string · Issue #158500 · python/cpython
Description: Bug report Bug description: date.fromisoformat() accepts a 10-character string in basic format and silently drops its last two characters: >>> from datetime import date, datetime >>> date.fromisoformat('2020010112') # e.g. a YYYYMMDDHH s...
Open Graph Description: Bug report Bug description: date.fromisoformat() accepts a 10-character string in basic format and silently drops its last two characters: >>> from datetime import date, datetime >>> date.fromisofo...
X Description: Bug report Bug description: date.fromisoformat() accepts a 10-character string in basic format and silently drops its last two characters: >>> from datetime import date, datetime >>&...
Opengraph URL: https://github.com/python/cpython/issues/158500
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"date.fromisoformat() silently ignores the last two characters of a 10-character basic-format string","articleBody":"# Bug report\n\n### Bug description:\n\n`date.fromisoformat()` accepts a 10-character string in basic format and silently drops its last two characters:\n\n```pycon\n>>> from datetime import date, datetime\n>>> date.fromisoformat('2020010112') # e.g. a YYYYMMDDHH stamp\ndatetime.date(2020, 1, 1)\n>>> date.fromisoformat('2020W011xx')\ndatetime.date(2019, 12, 30)\n>>> datetime.fromisoformat('2020010112') # the datetime parser rejects it\nValueError: Invalid isoformat string: '2020010112'\n```\n\n`date.fromisoformat` admits inputs of length 7, 8 or 10. Both `parse_isoformat_date()` in `Modules/_datetimemodule.c` and `_parse_isoformat_date()` in `Lib/_pydatetime.py` read fixed-width fields from the start of the string, and neither checks that the whole string was consumed. A 10-character string without `-` at index 4 is therefore parsed as the 8-character basic date `YYYYMMDD` (or `YYYYWwwD`), and the trailing characters are ignored.\n\nSuch a string is not an ISO 8601 date, and it is not one of the documented exceptions. Because the result is a plausible date, a truncated or mistyped input (such as a 10-digit `YYYYMMDDHH`) goes unnoticed.\n\nChecked on:\n- 3.11.15 with the C accelerator: the output above;\n- `Lib/_pydatetime.py` from `main` (7eada7c2c6), run under 3.11.15: same results for both strings;\n- `Modules/_datetimemodule.c` on `main`: read, not built. The code path is unchanged.\n\nSuggested fix: after the last field, require that the whole string has been consumed (`p - dtstr == len` in C, `len(dtstr) == pos` in Python). I have a draft patch with tests for both implementations and can open a PR if this is accepted.\n\nFound by differential testing `fromisoformat` against an independent ISO 8601 implementation. I found no existing report; the nearest is gh-152204, which covers other pure-Python-only inputs.\n\n### CPython versions tested on:\n\n3.11, CPython main branch\n\n### Operating systems tested on:\n\nWindows\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-158557\n* gh-160737\n* gh-161501\n* gh-161802\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/fitness-trener","@type":"Person","name":"fitness-trener"},"datePublished":"2026-09-30T12:59:19Z","url":"https://github.com/python/cpython/issues/158500"}
| 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:68d22580-6bfb-1046-5a7d-f430334b004e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9704:3E4205:33B17D3:450E8AC:6B020225 |
| html-safe-nonce | 3cc5d6ac4e738e943be5452427a1764141887b78df87f4c2168b59d203cbd01e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NzA0OjNFNDIwNTozM0IxN0QzOjQ1MEU4QUM6NkIwMjAyMjUiLCJ2aXNpdG9yX2lkIjoiMzU0MjE5MDY0NzU2ODEwNjAyMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 0cb04ba04d563eed4b021eb529cc9ad0365101041473861bb6cf8990af5421aa |
| hovercard-subject-tag | issue:5646824052 |
| 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/158500/issue_layout |
| twitter:image | https://opengraph.githubassets.com/06a6d798ac899b34afd833be3b4b0d7adbefe307c58b8283f3dfaef2b39eb38d/python/cpython/issues/158500 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/06a6d798ac899b34afd833be3b4b0d7adbefe307c58b8283f3dfaef2b39eb38d/python/cpython/issues/158500 |
| og:image:alt | Bug report Bug description: date.fromisoformat() accepts a 10-character string in basic format and silently drops its last two characters: >>> from datetime import date, datetime >>> date.fromisofo... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | fitness-trener |
| 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