Fixed Bug Regarding Attribute Error in pytest.approx For Types Implicitly Convertible to Numpy Arrays (#12232)

* added test case in testing/python/approx.py based on test case provided by reporter in issue #12114
* test cases pass for pytest testing/python/approx.py
* 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-23 04:45:33 -04:00
committed by GitHub
parent a830a3e98d
commit 5cffef7f07
4 changed files with 28 additions and 5 deletions

View File

@@ -763,6 +763,23 @@ class TestApprox:
assert a12 != approx(a21)
assert a21 != approx(a12)
def test_numpy_array_implicit_conversion(self):
np = pytest.importorskip("numpy")
class ImplicitArray:
"""Type which is implicitly convertible to a numpy array."""
def __init__(self, vals):
self.vals = vals
def __array__(self, dtype=None, copy=None):
return np.array(self.vals)
vec1 = ImplicitArray([1.0, 2.0, 3.0])
vec2 = ImplicitArray([1.0, 2.0, 4.0])
# see issue #12114 for test case
assert vec1 != approx(vec2)
def test_numpy_array_protocol(self):
"""
array-like objects such as tensorflow's DeviceArray are handled like ndarray.