Reject non-finite values (nan, inf) in FloatField#9998
Open
winklemad wants to merge 1 commit into
Open
Conversation
FloatField accepted nan, inf, -inf and overflow-to-inf strings such as
"1e400", returning float('nan')/float('inf') instead of raising a
ValidationError. It was the only numeric field without a finiteness
guard: IntegerField and DecimalField already reject these, as does
Django's own forms.FloatField.
REST framework renders JSON with allow_nan=False to stay spec-compliant,
so a non-finite value accepted here raises "Out of range float values
are not JSON compliant" when the object is later serialized, turning a
validation gap into a 500 error.
Guard to_internal_value with math.isfinite, mirroring DecimalField's
is_nan check and Django's forms.FloatField.
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.
Description
FloatFieldaccepts non-finite floats —nan,inf,-inf, and overflow-to-inf strings such as"1e400"— returningfloat('nan')/float('inf')instead of raising aValidationError. It is the only DRF numeric field without a finiteness guard:IntegerFieldandDecimalFieldalready reject these, as does Django's ownforms.FloatField.The value arrives as a JSON/form string (e.g.
"nan"), so it slips past the JSON parser'sparse_constantguard —float("nan")accepts it.Why it matters
REST framework deliberately renders JSON with
allow_nan=Falseto stay spec-compliant (rest_framework/utils/json.py). A non-finite value accepted byFloatFieldtherefore raisesValueError: Out of range float values are not JSON compliantwhen the object is later serialized, turning a silent validation gap into a 500 error:Fix
Guard
to_internal_valuewithmath.isfinite, mirroringDecimalField(which rejectsis_nan) and Django'sforms.FloatField(which usesmath.isfinite). The existingoverflowpath (huge integer inputs raisingOverflowError) is unchanged; string inputs like"1e400"thatfloat()turns intoinfare now rejected asinvalid.Added
nan,inf,-inf,1e400, and nativefloat('nan')/float('inf')cases toTestFloatField.invalid_inputs. Fulltests/test_fields.pypasses (334 passed, 3 pre-existing skips).