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

Method test_numpy_array_protocol

testing/python/approx.py:707–735  ·  view source on GitHub ↗

array-like objects such as tensorflow's DeviceArray are handled like ndarray. See issue #8132

(self)

Source from the content-addressed store, hash-verified

705 assert a21 != approx(a12)
706
707 def test_numpy_array_protocol(self):
708 """
709 array-like objects such as tensorflow's DeviceArray are handled like ndarray.
710 See issue #8132
711 """
712 np = pytest.importorskip("numpy")
713
714 class DeviceArray:
715 def __init__(self, value, size):
716 self.value = value
717 self.size = size
718
719 def __array__(self):
720 return self.value * np.ones(self.size)
721
722 class DeviceScalar:
723 def __init__(self, value):
724 self.value = value
725
726 def __array__(self):
727 return np.array(self.value)
728
729 expected = 1
730 actual = 1 + 1e-6
731 assert approx(expected) == DeviceArray(actual, size=1)
732 assert approx(expected) == DeviceArray(actual, size=2)
733 assert approx(expected) == DeviceScalar(actual)
734 assert approx(DeviceScalar(expected)) == actual
735 assert approx(DeviceScalar(expected)) == DeviceScalar(actual)
736
737 def test_doctests(self, mocked_doctest_runner) -> None:
738 import doctest

Callers

nothing calls this directly

Calls 3

approxFunction · 0.90
DeviceArrayClass · 0.85
DeviceScalarClass · 0.85

Tested by

no test coverage detected