expanded the type annotation to include objects which may cast to a array and renamed other_side to other_side_as_array and asserted that it is not none

This commit is contained in:
poulami-sau 2024-04-22 09:52:35 -04:00
parent 314ef0424a
commit 476d9d07d6
1 changed files with 7 additions and 6 deletions

View File

@ -142,7 +142,7 @@ class ApproxNumpy(ApproxBase):
) )
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: Union["ndarray", List[Any]]) -> List[str]:
import itertools import itertools
import math import math
@ -164,12 +164,13 @@ class ApproxNumpy(ApproxBase):
) )
# convert other_side to numpy array to ensure shape attribute is available # convert other_side to numpy array to ensure shape attribute is available
other_side = _as_numpy_array(other_side) other_side_as_array = _as_numpy_array(other_side)
assert other_side_as_array is not None
if np_array_shape != other_side.shape: if np_array_shape != other_side_as_array.shape:
return [ return [
"Impossible to compare arrays with different shapes.", "Impossible to compare arrays with different shapes.",
f"Shapes: {np_array_shape} and {other_side.shape}", f"Shapes: {np_array_shape} and {other_side_as_array.shape}",
] ]
number_of_elements = self.expected.size number_of_elements = self.expected.size
@ -178,7 +179,7 @@ class ApproxNumpy(ApproxBase):
different_ids = [] different_ids = []
for index in itertools.product(*(range(i) for i in np_array_shape)): for index in itertools.product(*(range(i) for i in np_array_shape)):
approx_value = get_value_from_nested_list(approx_side_as_seq, index) approx_value = get_value_from_nested_list(approx_side_as_seq, index)
other_value = get_value_from_nested_list(other_side, index) other_value = get_value_from_nested_list(other_side_as_array, index)
if approx_value != other_value: if approx_value != other_value:
abs_diff = abs(approx_value.expected - other_value) abs_diff = abs(approx_value.expected - other_value)
max_abs_diff = max(max_abs_diff, abs_diff) max_abs_diff = max(max_abs_diff, abs_diff)
@ -191,7 +192,7 @@ class ApproxNumpy(ApproxBase):
message_data = [ message_data = [
( (
str(index), str(index),
str(get_value_from_nested_list(other_side, index)), str(get_value_from_nested_list(other_side_as_array, index)),
str(get_value_from_nested_list(approx_side_as_seq, index)), str(get_value_from_nested_list(approx_side_as_seq, index)),
) )
for index in different_ids for index in different_ids