Skip to content

Commit

Permalink
Update difference behavior to support NaN values inside tuples.
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnbrown committed Jul 16, 2019
1 parent 32d3bb9 commit e8435b1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
16 changes: 11 additions & 5 deletions datatest/differences.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@
)


def _nan_to_token(x):
with suppress(TypeError):
if isnan(x):
return NANTOKEN
return x
def _nan_to_token(value):
"""Return NANTOKEN if *value* is NaN else return value unchanged."""
def func(x):
with suppress(TypeError):
if isnan(x):
return NANTOKEN
return x

if isinstance(value, tuple):
return tuple(func(x) for x in value)
return func(value)


class BaseDifference(abc.ABC):
Expand Down
5 changes: 5 additions & 0 deletions tests/test_difference.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ def test_nan_equal(self):
second = MinimalDifference(float('nan'))
self.assertEqual(first, second)

# NaNs nested in a tuple should also test as equal.
first = MinimalDifference(('abc', float('nan')))
second = MinimalDifference(('abc', float('nan')))
self.assertEqual(first, second)

def test_comparing_different_types(self):
diff = MinimalDifference('X')
self.assertNotEqual(diff, Exception('X'))
Expand Down

0 comments on commit e8435b1

Please sign in to comment.