From ef21e6e4e37ebd830dc8a4fe87f85632dc243e0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89loi=20Rivard?= Date: Fri, 17 Apr 2020 15:22:43 +0200 Subject: [PATCH 1/2] NumberRange can handle NaN. Fixes #505 --- src/wtforms/validators.py | 2 ++ tests/test_validators.py | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/src/wtforms/validators.py b/src/wtforms/validators.py index f274bd53..70cc1a04 100644 --- a/src/wtforms/validators.py +++ b/src/wtforms/validators.py @@ -1,5 +1,6 @@ from __future__ import unicode_literals +import math import re import uuid @@ -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) ): diff --git a/tests/test_validators.py b/tests/test_validators.py index e223c8d7..a54a0c1e 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import decimal import pytest import re @@ -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): """ From 1cb52d0c4248b78a2ff6b95ec91a5a172dcb4669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89loi=20Rivard?= Date: Sat, 18 Apr 2020 00:23:17 +0200 Subject: [PATCH 2/2] Changelog --- CHANGES.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index a8f47a41..3ee928e1 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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