Let approx() work on more generic sequences

approx() was updated in 9f3122fe to work better with numpy arrays,
however at the same time the requirements were tightened from
requiring an Iterable to requiring a Sequence - the former being
tested only on interface, while the latter requires subclassing or
registration with the abc.

Since the ApproxSequence only used __iter__ and __len__ this commit
reduces the requirement to only what's used, and allows unregistered
Sequence-like containers to be used.

Since numpy arrays qualify for the new criteria, reorder the checks so
that generic sequences are checked for after numpy arrays.
This commit is contained in:
Nicholas Devenish
2018-11-07 12:08:23 +00:00
parent 62967b3110
commit 1a8d9bf254
5 changed files with 23 additions and 6 deletions

View File

@@ -496,3 +496,13 @@ class TestApprox(object):
assert actual != approx(expected, rel=5e-8, abs=0)
assert approx(expected, rel=5e-7, abs=0) == actual
assert approx(expected, rel=5e-8, abs=0) != actual
def test_generic_iterable_sized_object(self):
class newIterable(object):
def __iter__(self):
return iter([1, 2, 3, 4])
def __len__(self):
return 4
assert [1, 2, 3, 4] == approx(newIterable())