Skip to content

Commit 2c0c161

Browse files
committed
Added a check to make sure all elements of a sequence in pytest.approx are valid.
1 parent 25045f3 commit 2c0c161

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

_pytest/python_api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,9 @@ def approx(expected, rel=None, abs=None, nan_ok=False):
450450
elif isinstance(expected, Mapping):
451451
cls = ApproxMapping
452452
elif isinstance(expected, Sequence) and not isinstance(expected, String):
453+
for value in expected:
454+
if isinstance(value, (Sequence, String, Mapping)):
455+
raise TypeError("Sequence contained a value that was not float or int")
453456
cls = ApproxSequence
454457
elif isinstance(expected, Decimal):
455458
cls = ApproxDecimal

changelog/3473.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
``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.
2+
``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.

0 commit comments

Comments
 (0)