Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NumberRange can handle NaN values #548

Merged
merged 2 commits into from Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
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
@@ -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
@@ -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