MCPcopy Create free account
hub / github.com/pytest-dev/pytest / _repr_compare

Method _repr_compare

src/_pytest/python_api.py:151–209  ·  view source on GitHub ↗
(self, other_side: "ndarray")

Source from the content-addressed store, hash-verified

149 return f"approx({list_scalars!r})"
150
151 def _repr_compare(self, other_side: "ndarray") -> List[str]:
152 import itertools
153 import math
154
155 def get_value_from_nested_list(
156 nested_list: List[Any], nd_index: Tuple[Any, ...]
157 ) -> Any:
158 """
159 Helper function to get the value out of a nested list, given an n-dimensional index.
160 This mimics numpy's indexing, but for raw nested python lists.
161 """
162 value: Any = nested_list
163 for i in nd_index:
164 value = value[i]
165 return value
166
167 np_array_shape = self.expected.shape
168 approx_side_as_list = _recursive_list_map(
169 self._approx_scalar, self.expected.tolist()
170 )
171
172 if np_array_shape != other_side.shape:
173 return [
174 "Impossible to compare arrays with different shapes.",
175 f"Shapes: {np_array_shape} and {other_side.shape}",
176 ]
177
178 number_of_elements = self.expected.size
179 max_abs_diff = -math.inf
180 max_rel_diff = -math.inf
181 different_ids = []
182 for index in itertools.product(*(range(i) for i in np_array_shape)):
183 approx_value = get_value_from_nested_list(approx_side_as_list, index)
184 other_value = get_value_from_nested_list(other_side, index)
185 if approx_value != other_value:
186 abs_diff = abs(approx_value.expected - other_value)
187 max_abs_diff = max(max_abs_diff, abs_diff)
188 if other_value == 0.0:
189 max_rel_diff = math.inf
190 else:
191 max_rel_diff = max(max_rel_diff, abs_diff / abs(other_value))
192 different_ids.append(index)
193
194 message_data = [
195 (
196 str(index),
197 str(get_value_from_nested_list(other_side, index)),
198 str(get_value_from_nested_list(approx_side_as_list, index)),
199 )
200 for index in different_ids
201 ]
202 return _compare_approx(
203 self.expected,
204 message_data,
205 number_of_elements,
206 different_ids,
207 max_abs_diff,
208 max_rel_diff,

Callers

nothing calls this directly

Calls 3

_recursive_list_mapFunction · 0.85
_compare_approxFunction · 0.85
appendMethod · 0.80

Tested by

no test coverage detected