Title: Inconsistent behavior of `fromisoformat` methods in `datetime` module implementations · Issue #127260 · python/cpython · GitHub
Open Graph Title: Inconsistent behavior of `fromisoformat` methods in `datetime` module implementations · Issue #127260 · python/cpython
X Title: Inconsistent behavior of `fromisoformat` methods in `datetime` module implementations · Issue #127260 · python/cpython
Description: Bug report Bug description: 1. Incorrect timezone validation in _pydatetime (solved) As far as I understand, the documentation says that Z char should mean that tzinfo is timezone.utc, so there cannot be any time zone fields after it. Ba...
Open Graph Description: Bug report Bug description: 1. Incorrect timezone validation in _pydatetime (solved) As far as I understand, the documentation says that Z char should mean that tzinfo is timezone.utc, so there can...
X Description: Bug report Bug description: 1. Incorrect timezone validation in _pydatetime (solved) As far as I understand, the documentation says that Z char should mean that tzinfo is timezone.utc, so there can...
Opengraph URL: https://github.com/python/cpython/issues/127260
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Inconsistent behavior of `fromisoformat` methods in `datetime` module implementations","articleBody":"# Bug report\n\n## Bug description:\n\n### 1. Incorrect timezone validation in `_pydatetime` (solved)\nAs far as I understand, the [documentation](https://docs.python.org/3.14/library/datetime.html) says that `Z` char should mean that `tzinfo` is `timezone.utc`, so there cannot be any time zone fields after it.\nBased on this, `_pydatetime` implementation is incorrect, right?\n\n```python\n\u003e\u003e\u003e import _datetime, _pydatetime\n\u003e\u003e\u003e _pydatetime.datetime.fromisoformat('2020-01-01T00:00Z00:50')\ndatetime.datetime(2020, 1, 1, 0, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=3000)))\n\u003e\u003e\u003e _datetime.datetime.fromisoformat('2020-01-01T00:00Z00:50')\nTraceback (most recent call last):\n File \"\u003cpython-input-54\u003e\", line 1, in \u003cmodule\u003e\n _datetime.datetime.fromisoformat('2020-01-01T00:00Z00:50')\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^\nValueError: Invalid isoformat string: '2020-01-01T00:00Z00:50'\n```\n\n### 2. Miss the wrong millisecond separator in `_datetime` (solved)\nIn `_pydatetime` the separator for milliseconds must be either a period `.` or a comma `,`.\nShould we allow colon `:` as millisecond separator?\n\n```python\n\u003e\u003e\u003e import _datetime, _pydatetime\n\u003e\u003e\u003e _datetime.datetime.fromisoformat('2020-01-01T00:00:01:1')\ndatetime.datetime(2020, 1, 1, 0, 0, 1, 100000)\n\u003e\u003e\u003e _pydatetime.datetime.fromisoformat('2020-01-01T00:00:01:1')\nTraceback (most recent call last):\n File \"\u003cpython-input-119\u003e\", line 1, in \u003cmodule\u003e\n _pydatetime.datetime.fromisoformat('2020-01-01T00:00:01:1')\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^\n File \".../cpython/Lib/_pydatetime.py\", line 1969, in fromisoformat\n \"Return local time tuple compatible with time.localtime().\"\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n dst = self.dst()\n ^^^^^^^^^^^^^^^^\nValueError: Invalid isoformat string: '2020-01-01T00:00:01:1'\n```\n\n### 3. The first errors caught can be different\nIf these errors occur separately, then both implementations are able to detect them, but when there are several problems, the methods may behave differently. In this case `_pydatetime` first detected an error due to the separator, and `_datetime` first detected an error in exceeding the limits.\n\n```python\n\u003e\u003e\u003e import _datetime, _pydatetime\n\u003e\u003e\u003e _pydatetime.datetime.fromisoformat('2009-04-19T03:15:45+10:90.11')\nTraceback (most recent call last):\n File \"\u003cpython-input-40\u003e\", line 1, in \u003cmodule\u003e\n _pydatetime.datetime.fromisoformat('2009-04-19T03:15:45+10:90.11')\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \".../cpython/Lib/_pydatetime.py\", line 1969, in fromisoformat\n f'Invalid isoformat string: {date_string!r}') from None\n else:\nValueError: Invalid isoformat string: '2009-04-19T03:15:45+10:90.11'\n\u003e\u003e\u003e _datetime.datetime.fromisoformat('2009-04-19T03:15:45+10:90.11')\nTraceback (most recent call last):\n File \"\u003cpython-input-41\u003e\", line 1, in \u003cmodule\u003e\n _datetime.datetime.fromisoformat('2009-04-19T03:15:45+10:90.11')\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\nValueError: minute must be in 0..59\n```\n\n---\nAlso also an issue has already been created about the fact that some errors have different output, here: \n* [datetime error message is different between _pydatetime.py and _datetimemodule.c #109798](https://github.com/python/cpython/issues/109798)\n\n*I'll send a PR.*\n\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\n_No response_\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-130134\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/donbarbos","@type":"Person","name":"donbarbos"},"datePublished":"2024-11-25T15:23:25.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/127260/cpython/issues/127260"}
| 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:d73899ab-1cb1-a74a-8535-34b1668e4e1a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 88C4:26177F:361C98:452BF8:696B0710 |
| html-safe-nonce | 9aa167ca3a093705b5cc651871cdf9a7b3d977eb9907a2e0e766e0b7d4c497cf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4OEM0OjI2MTc3RjozNjFDOTg6NDUyQkY4OjY5NkIwNzEwIiwidmlzaXRvcl9pZCI6IjUzOTY4NTgzNjMxOTY0MDk2MTYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 67cbe76fde434c93d01a56913f6470f19759934ee1690675cde9000466213a76 |
| hovercard-subject-tag | issue:2691132930 |
| 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/127260/issue_layout |
| twitter:image | https://opengraph.githubassets.com/83f785e9b7c8ee166cbe39ff5d621368aef50f7cfa841a128e04a55b8cde8f83/python/cpython/issues/127260 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/83f785e9b7c8ee166cbe39ff5d621368aef50f7cfa841a128e04a55b8cde8f83/python/cpython/issues/127260 |
| og:image:alt | Bug report Bug description: 1. Incorrect timezone validation in _pydatetime (solved) As far as I understand, the documentation says that Z char should mean that tzinfo is timezone.utc, so there can... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | donbarbos |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| 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 | 82560a55c6b2054555076f46e683151ee28a19bc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width