Raise ValidationError (not ValueError) for NaN in TimeDelta - #3004
Open
vidigoat wants to merge 2 commits into
Open
Raise ValidationError (not ValueError) for NaN in TimeDelta#3004vidigoat wants to merge 2 commits into
vidigoat wants to merge 2 commits into
Conversation
TimeDelta._deserialize only caught OverflowError around
dt.timedelta(**kwargs). float("nan") passes the earlier float() guard,
but dt.timedelta(nan) raises ValueError, which escaped uncaught -- so
loading {"duration": "nan"} from untrusted input raised a raw
ValueError instead of a ValidationError, breaking the contract that all
bad field input surfaces as ValidationError. (inf/-inf already worked
because timedelta raises OverflowError for those.)
Catch ValueError alongside OverflowError so NaN yields the field's
standard 'Not a valid period of time.' error. Extends the existing
parametrized invalid-input test with nan/inf/-inf.
lafrech
requested changes
Aug 3, 2026
lafrech
left a comment
Member
There was a problem hiding this comment.
Thanks.
Just a few minor comments. LLM tend to be too verbose.
Author
|
Comment shortened, thanks. On the commit message: I wasn't able to rewrite the original commit here. If you squash-merge, feel free to use just the PR title as the message. |
lafrech
approved these changes
Aug 18, 2026
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.
Problem
A
TimeDeltafield raises a bareValueError(instead ofValidationError) when it receivesNaN, so loading untrusted input can crash with an exception that isn't part of marshmallow's public error contract:"inf"/"-inf"already behave correctly (they raiseValidationError), which makes theNaNcase an inconsistency rather than intended behaviour.Cause
TimeDelta._deserializeonly catchesOverflowErrorarounddt.timedelta(**kwargs):float("nan")passes the earlierfloat(value)guard, butdt.timedelta(nan)raisesValueError— notOverflowError— so it escapes uncaught. (inf/-infwork becausetimedeltaraisesOverflowErrorfor those.)Fix
Catch
ValueErroralongsideOverflowError, soNaNproduces the field's standard"Not a valid period of time."error like every other invalid input:Extended the existing parametrized
test_invalid_timedelta_field_deserializationwith"nan","inf","-inf". Full test suite passes locally.Disclosure: I used an LLM to help find and draft this fix; I've reviewed every line, reproduced the bug, and run the suite myself.