-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attribute error in pytest.approx for types implicitly convertible to numpy arrays #12114
Comments
I can try working on this! |
I'm not able to reproduce this error in the test cases for testing/python/approx.py, which is what I need to do to verify it in tox. Do you have any pointers? This is one way I wrote the test case:
Here's another way that I wrote it:
But for both cases, they are passing when I know it shouldn't, and I haven't made any edits to ApproxNumpy yet. |
Okay I think tox doesn't work for this as I cannot verify if numpy was actually installed or not with tox. However, it works with the alternate method for testing and I've fixed the bug. I can submit a pull request soon! |
…ed by reporter in issue pytest-dev#12114
…ed by reporter in issue pytest-dev#12114
…itly 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
Fixed in #12232 |
When using pytest.approx with custom types which are implicitly convertible to numpy arrays, the following error is observed:
Note: The line number in the error message is a little off from the current head. That line is here.
Looking at the code,
pytest.approx
sensibly chooses the numpy implementationApproxNumpy
, because the expected value is implicitly convertible to a numpy array. However, the shape check assumes thatother_side
is a numpy array (rather than assuming its implicitly convertible to a numpy array, like the expected value is) and so assumes that it has ashape
attribute (which is not part of how approx determines if something is a numpy array).It seems like either:
ApproxNumpy._repr_compare()
needs to convertother_side
to a numpy array (ie_as_numpy_array(other_side)
)_as_numpy_array
needs to check if the object has ashape
attribute, if the code is going to assume that's the caseoutput of
pip list
from the virtual environment you are using:Relevant packages:
numpy: 1.26.1
pytest and operating system versions
pytest: 7.4.3
Ubuntu: 22.04
The text was updated successfully, but these errors were encountered: