Prevent date-only strings from being deserialized as datetime objects#571
Merged
computron merged 3 commits intomaterialsproject:mainfrom Mar 20, 2026
Merged
Conversation
`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
fb502ee to
4c6a7f9
Compare
- 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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
reconstitute_dates()trieddatetime.fromisoformat()on every string. Python 3.11+fromisoformataccepts date-only strings like'2000-02-01', silently converting them todatetimeobjects. Now only attempts datetime parsing when the string containsT(ISO 8601 separator), matching whatDATETIME_HANDLER.isoformat()produces.Also narrows the except clause from bare
Exceptionto(ValueError, TypeError)and adds pre-existing ruff rules to the ignore list.Closes #570