Opened 3 weeks ago

Closed 3 weeks ago

#37476 closed Bug (wontfix)

MinValueValidator and MaxValueValidator accept NaN

Reported by: Aathithya Arasu S Owned by:
Component: Core (Other) Version: 4.2
Severity: Low Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Both validators accept float NaN without raising an error:

from django.core.validators import MinValueValidator, MaxValueValidator

MinValueValidator(0)(float("nan"))  # no error
MaxValueValidator(10)(float("nan"))  # no error

A model FloatField with MinValueValidator(0) also passes full_clean() with NaN. This seems inconsistent with forms.FloatField, which rejects it. Is rejecting NaN in these validators the intended behavior too?

I have a patch that checks a != a before the existing comparison, with regression tests. It also changes quiet Decimal NaN from an InvalidOperation exception to a ValidationError. Valid infinity comparisons stay unchanged.

Reproduced on main (6.2.dev, commit ca3f31b) and 5.2.19.

Change History (1)

comment:1 by Sarah Boyce, 3 weeks ago

Resolution: → wontfix
Status: new → closed

Thank you for the report.

Per the ​Python documentation, ordered comparisons with float NaN return False. NaN therefore does not meet either validator’s documented rejection condition: it is neither less than the minimum nor greater than the maximum.

Rejecting NaN can be handled separately by a field or an additional validator, as it is by forms.FloatField. Model and form fields do not necessarily perform the same validation.

Changing this behavior could break existing applications, and I do not see a clear benefit to expanding these validators’ responsibilities. I’m therefore closing this as wontfix.

Note: See TracTickets for help on using tickets.
Back to Top