Skip to content

Prevent date-only strings from being deserialized as datetime objects#571

Merged
computron merged 3 commits intomaterialsproject:mainfrom
janosh:fix-date-string-deserialization
Mar 20, 2026
Merged

Prevent date-only strings from being deserialized as datetime objects#571
computron merged 3 commits intomaterialsproject:mainfrom
janosh:fix-date-string-deserialization

Conversation

@janosh
Copy link
Member

@janosh janosh commented Mar 20, 2026

reconstitute_dates() tried datetime.fromisoformat() on every string. Python 3.11+ fromisoformat accepts date-only strings like '2000-02-01', silently converting them to datetime objects. Now only attempts datetime parsing when the string contains T (ISO 8601 separator), matching what DATETIME_HANDLER.isoformat() produces.

Also narrows the except clause from bare Exception to (ValueError, TypeError) and adds pre-existing ruff rules to the ignore list.

Closes #570

`reconstitute_dates()` used `datetime.fromisoformat()` on every string.
Python 3.11+ `fromisoformat` accepts date-only strings like '2000-02-01',
silently converting them to datetime objects. Guard parsing behind a
`"T" in obj_dict` check since `DATETIME_HANDLER.isoformat()` always
produces T-separated strings. Also narrow the except clause from bare
`Exception` to `(ValueError, TypeError)`.

Add pre-existing ruff rules (BLE001, FIX002, N802, TD002, TD003) to the
ignore list so the prek pre-commit hook passes.

Closes materialsproject#570
@janosh janosh force-pushed the fix-date-string-deserialization branch from fb502ee to 4c6a7f9 Compare March 20, 2026 19:50
janosh added 2 commits March 20, 2026 19:53
- Consolidate ruff ignore list: add all pre-existing rule violations
  (S101, ERA001, DTZ, SLF001, N806, etc.) to the global ignore list
  and remove now-redundant per-file-ignores for tests
- Auto-fix 11 unused noqa comments (RUF100) and 1 unsorted import (I001)
- Run ruff format on 1 file needing reformatting
- Fix codespell typo: comptability -> compatibility in flask_site/app.py
- Remove dead try/except in _recursive_load: reconstitute_dates never
  raises, making the except branch with ASCII-encoding unreachable
- Collapse D100-D107 ruff ignores into "D1" prefix
- Test exact datetime values instead of just isinstance checks
- Drop redundant "2000-02" test case (not a regression vector for materialsproject#570)
@computron computron merged commit 5ecc0f8 into materialsproject:main Mar 20, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Strings that look like date-time are deserialized to datetime objects

2 participants