From 0700b2bbd4c74a13e90c1f7c41a63c514b53976e Mon Sep 17 00:00:00 2001 From: Drew-Ack Date: Mon, 14 May 2018 13:16:03 -0600 Subject: [PATCH 1/7] Added a check to see if the "expected" argument for the approx function is a string. If "expected" is a string then raise a typeError and a friendly message. --- _pytest/python_api.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/_pytest/python_api.py b/_pytest/python_api.py index 8e09a4a6fd3..3afdb0ef3ad 100644 --- a/_pytest/python_api.py +++ b/_pytest/python_api.py @@ -443,8 +443,9 @@ def approx(expected, rel=None, abs=None, nan_ok=False): # This has the advantage that it made it easy to support mapping types # (i.e. dict). The old code accepted mapping types, but would only compare # their keys, which is probably not what most people would expect. - - if _is_numpy_array(expected): + if isinstance(expected, String): + raise TypeError("Strings can not be approximated") + elif _is_numpy_array(expected): cls = ApproxNumpy elif isinstance(expected, Mapping): cls = ApproxMapping From a6feb0b31aeb1770808b896d5d0ec579aef6d09a Mon Sep 17 00:00:00 2001 From: Drew-Ack Date: Mon, 14 May 2018 13:21:17 -0600 Subject: [PATCH 2/7] Edit to Authors file. --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index eb3c016a570..fe96e0d79b3 100644 --- a/AUTHORS +++ b/AUTHORS @@ -59,6 +59,7 @@ Denis Kirisov Diego Russo Dmitry Dygalo Dmitry Pribysh +Drew Ackerman Duncan Betts Edison Gustavo Muenz Edoardo Batini From 1ffb6bbfb3b96d7c1bcf8f6745c2d4702dc72b94 Mon Sep 17 00:00:00 2001 From: Drew-Ack Date: Mon, 14 May 2018 13:29:54 -0600 Subject: [PATCH 3/7] Created a report for the bugfix that addresses issue #3473 --- changelog/3473.bugfix.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelog/3473.bugfix.rst diff --git a/changelog/3473.bugfix.rst b/changelog/3473.bugfix.rst new file mode 100644 index 00000000000..9e6b73ea959 --- /dev/null +++ b/changelog/3473.bugfix.rst @@ -0,0 +1,2 @@ +pytest.approx does not handle strings, but did not make that easily apparent. +pytest.approx now raises TypeError when trying a string is passed in. \ No newline at end of file From 9b485d6554aab296b6ce0244cf930b80f0e3feec Mon Sep 17 00:00:00 2001 From: Drew-Ack Date: Mon, 14 May 2018 13:33:59 -0600 Subject: [PATCH 4/7] Small change in wording. --- changelog/3473.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/3473.bugfix.rst b/changelog/3473.bugfix.rst index 9e6b73ea959..65f3b448cf6 100644 --- a/changelog/3473.bugfix.rst +++ b/changelog/3473.bugfix.rst @@ -1,2 +1,2 @@ pytest.approx does not handle strings, but did not make that easily apparent. -pytest.approx now raises TypeError when trying a string is passed in. \ No newline at end of file +pytest.approx now raises TypeError when a string is passed in as the expected argument. \ No newline at end of file From d29c9eb17145b25f683f5764cc38a718e5cfeb05 Mon Sep 17 00:00:00 2001 From: Drew-Ack Date: Tue, 15 May 2018 09:02:28 -0600 Subject: [PATCH 5/7] Created test case for trying to approximate a string. The test checks that a TypeError is raised correctly when a string is passed into approx(). --- testing/python/approx.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/testing/python/approx.py b/testing/python/approx.py index 9ca21bdf8e9..acd1aa3ef78 100644 --- a/testing/python/approx.py +++ b/testing/python/approx.py @@ -243,6 +243,10 @@ def test_int(self): assert approx(x, rel=5e-6, abs=0) == a assert approx(x, rel=5e-7, abs=0) != a + def test_string(self): + with pytest.raises(TypeError): + approx('abc') + def test_decimal(self): within_1e6 = [ (Decimal('1.000001'), Decimal('1.0')), From 25045f359c6da49c57528b555e0f58886dcf1c14 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 15 May 2018 18:14:23 -0300 Subject: [PATCH 6/7] Small rewording of the CHANGELOG --- changelog/3473.bugfix.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/changelog/3473.bugfix.rst b/changelog/3473.bugfix.rst index 65f3b448cf6..cfe05431885 100644 --- a/changelog/3473.bugfix.rst +++ b/changelog/3473.bugfix.rst @@ -1,2 +1 @@ -pytest.approx does not handle strings, but did not make that easily apparent. -pytest.approx now raises TypeError when a string is passed in as the expected argument. \ No newline at end of file +``pytest.approx`` does not handle strings, but this was not easily apparent so it now raises a ``TypeError`` when a string is passed in as the expected argument. From 2c0c16161069efb695af7a978a2ac8fe64cd3847 Mon Sep 17 00:00:00 2001 From: Drew-Ack Date: Fri, 18 May 2018 09:22:11 -0600 Subject: [PATCH 7/7] Added a check to make sure all elements of a sequence in pytest.approx are valid. --- _pytest/python_api.py | 3 +++ changelog/3473.bugfix.rst | 1 + 2 files changed, 4 insertions(+) diff --git a/_pytest/python_api.py b/_pytest/python_api.py index 3afdb0ef3ad..bfd5f137274 100644 --- a/_pytest/python_api.py +++ b/_pytest/python_api.py @@ -450,6 +450,9 @@ def approx(expected, rel=None, abs=None, nan_ok=False): elif isinstance(expected, Mapping): cls = ApproxMapping elif isinstance(expected, Sequence) and not isinstance(expected, String): + for value in expected: + if isinstance(value, (Sequence, String, Mapping)): + raise TypeError("Sequence contained a value that was not float or int") cls = ApproxSequence elif isinstance(expected, Decimal): cls = ApproxDecimal diff --git a/changelog/3473.bugfix.rst b/changelog/3473.bugfix.rst index cfe05431885..508e63c08e8 100644 --- a/changelog/3473.bugfix.rst +++ b/changelog/3473.bugfix.rst @@ -1 +1,2 @@ ``pytest.approx`` does not handle strings, but this was not easily apparent so it now raises a ``TypeError`` when a string is passed in as the expected argument. +``pytest.approx`` did not check all elements in a sequence to make sure they were valid type, a ``TypeError`` is now raised when an invalid element is in a sequence. \ No newline at end of file