fix: Fix repr for tuples in approx

This commit is contained in:
Zach OBrien 2022-05-06 01:03:39 -04:00
parent 4ddf48b0a3
commit 520b56d771
No known key found for this signature in database
GPG Key ID: 3FC3457550AF85EB
1 changed files with 10 additions and 6 deletions

View File

@ -133,9 +133,11 @@ class ApproxBase:
# raise if there are any non-numeric elements in the sequence. # raise if there are any non-numeric elements in the sequence.
def _recursive_list_map(f, x): def _recursive_sequence_map(f, x):
if isinstance(x, list): """Recursively map a function over a sequence of arbitary depth"""
return [_recursive_list_map(f, xi) for xi in x] if isinstance(x, Sequence):
seq_type = type(x)
return seq_type(_recursive_sequence_map(f, xi) for xi in x)
else: else:
return f(x) return f(x)
@ -144,7 +146,9 @@ class ApproxNumpy(ApproxBase):
"""Perform approximate comparisons where the expected value is numpy array.""" """Perform approximate comparisons where the expected value is numpy array."""
def __repr__(self) -> str: def __repr__(self) -> str:
list_scalars = _recursive_list_map(self._approx_scalar, self.expected.tolist()) list_scalars = _recursive_sequence_map(
self._approx_scalar, self.expected.tolist()
)
return f"approx({list_scalars!r})" return f"approx({list_scalars!r})"
def _repr_compare(self, other_side: "ndarray") -> List[str]: def _repr_compare(self, other_side: "ndarray") -> List[str]:
@ -164,7 +168,7 @@ class ApproxNumpy(ApproxBase):
return value return value
np_array_shape = self.expected.shape np_array_shape = self.expected.shape
approx_side_as_list = _recursive_list_map( approx_side_as_list = _recursive_sequence_map(
self._approx_scalar, self.expected.tolist() self._approx_scalar, self.expected.tolist()
) )
@ -326,7 +330,7 @@ class ApproxSequenceLike(ApproxBase):
f"Lengths: {len(self.expected)} and {len(other_side)}", f"Lengths: {len(self.expected)} and {len(other_side)}",
] ]
approx_side_as_map = _recursive_list_map(self._approx_scalar, self.expected) approx_side_as_map = _recursive_sequence_map(self._approx_scalar, self.expected)
number_of_elements = len(approx_side_as_map) number_of_elements = len(approx_side_as_map)
max_abs_diff = -math.inf max_abs_diff = -math.inf