Skip to content

Commit

Permalink
Merge pull request #548 from azmeuk/issue-505
Browse files Browse the repository at this point in the history
NumberRange can handle NaN values
  • Loading branch information
azmeuk authored Apr 20, 2020
2 parents caeb39a + 1cb52d0 commit c834aa3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ Unreleased
- :class:`~validators.URL` validator now allows query parameters in the URL. (:pr:`523`, :pr:`524`).
- Updated French and Japanese translations. (:pr:`514`, :pr:`506`)
- form.errors is not cached and will update if an error is appended to a field after access. (:pr:`568`)
- :class:`~wtforms.validators.NumberRange` correctly handle *not a number*
values. (:pr:`505`, :pr:`548`)


Version 2.2.1
Expand Down
2 changes: 2 additions & 0 deletions src/wtforms/validators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import unicode_literals

import math
import re
import uuid

Expand Down Expand Up @@ -191,6 +192,7 @@ def __call__(self, form, field):
data = field.data
if (
data is None
or math.isnan(data)
or (self.min is not None and data < self.min)
or (self.max is not None and data > self.max)
):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import decimal
import pytest
import re

Expand Down Expand Up @@ -589,6 +590,14 @@ def test_number_range_raises(min_v, max_v, test_v, dummy_form, dummy_field):
validator(dummy_form, dummy_field)


@pytest.mark.parametrize("nan", [float("NaN"), decimal.Decimal("NaN")])
def test_number_range_nan(nan, dummy_form, dummy_field):
validator = NumberRange(0, 10)
dummy_field.data = nan
with pytest.raises(ValidationError):
validator(dummy_form, dummy_field)


@pytest.mark.parametrize("test_function", [str, text_type])
def test_lazy_proxy_raises(test_function, really_lazy_proxy):
"""
Expand Down

0 comments on commit c834aa3

Please sign in to comment.