Compare two objects for equivalence (identity or equality), using array_equiv if either object is an ndarray. If both objects are lists, equivalent is sequentially called on all the elements. Returns False for any comparison that doesn't return a boolean, making this function safer
(first: T, second: T)
| 236 | |
| 237 | |
| 238 | def equivalent(first: T, second: T) -> bool: |
| 239 | """Compare two objects for equivalence (identity or equality), using |
| 240 | array_equiv if either object is an ndarray. If both objects are lists, |
| 241 | equivalent is sequentially called on all the elements. |
| 242 | |
| 243 | Returns False for any comparison that doesn't return a boolean, |
| 244 | making this function safer to use with objects that have non-standard |
| 245 | __eq__ implementations. |
| 246 | """ |
| 247 | # TODO: refactor to avoid circular import |
| 248 | from xarray.core import duck_array_ops |
| 249 | |
| 250 | if first is second: |
| 251 | return True |
| 252 | |
| 253 | if isinstance(first, np.ndarray) or isinstance(second, np.ndarray): |
| 254 | return duck_array_ops.array_equiv(first, second) |
| 255 | |
| 256 | if isinstance(first, list) or isinstance(second, list): |
| 257 | return list_equiv(first, second) # type: ignore[arg-type] |
| 258 | |
| 259 | # Check for NaN equivalence early (before equality comparison) |
| 260 | # This handles both Python float NaN and NumPy scalar NaN (issue #10833) |
| 261 | if pd.isnull(first) and pd.isnull(second): # type: ignore[call-overload] |
| 262 | return True |
| 263 | |
| 264 | # For non-array/list types, use == but require boolean result |
| 265 | result = first == second |
| 266 | if not isinstance(result, bool): |
| 267 | # Accept numpy bool scalars as well |
| 268 | if isinstance(result, np.bool_): |
| 269 | return bool(result) |
| 270 | # Reject any other non-boolean type (Dataset, Series, custom objects, etc.) |
| 271 | return False |
| 272 | |
| 273 | return result |
| 274 | |
| 275 | |
| 276 | def list_equiv(first: Sequence[T], second: Sequence[T]) -> bool: |
no test coverage detected
searching dependent graphs…