Opened 39 minutes ago
#37476 new Bug
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.