Skip to content
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

ModuleNotFoundError for Numpy when pytest.approx fails #9726

Closed
AdrianDAlessandro opened this issue Mar 3, 2022 · 5 comments · Fixed by #9798
Closed

ModuleNotFoundError for Numpy when pytest.approx fails #9726

AdrianDAlessandro opened this issue Mar 3, 2022 · 5 comments · Fixed by #9798
Labels
status: easy type: bug

Comments

@AdrianDAlessandro
Copy link

@AdrianDAlessandro AdrianDAlessandro commented Mar 3, 2022

Description of the bug:

When using pytest.approx to compare lists in a test, a ModuleNotFoundError is raised for Numpy whenever the test fails. If the test succeeds, there is no such error.

It appears that pytest.approx does not require Numpy to run, but the error message requires Numpy to display the details. Since pytest.approx does not require Numpy to function correctly, it should not require Numpy to display the error.

Output of pip list from the virtual environment:

Package    Version
---------- -------
attrs      21.4.0
iniconfig  1.1.1
packaging  21.3
pip        22.0.3
pluggy     1.0.0
py         1.11.0
pyparsing  3.0.7
pytest     7.0.1
setuptools 49.2.1
tomli      2.0.1

pytest and operating system versions:

$ python --version
Python 3.9.0
$ python -m pytest --version
pytest 7.0.1

macOS Big Sur

Version 11.6.2

Minimal example:

import pytest
def test_approx():
    assert [1, 2] == pytest.approx([1.001, 2.002])

Actual Result:

$ pytest
============================= test session starts ==============================
platform darwin -- Python 3.9.0, pytest-7.0.1, pluggy-1.0.0
rootdir: ****
collected 1 item                                                               

test_approx.py F                                                         [100%]

=================================== FAILURES ===================================
_________________________________ test_approx __________________________________

    def test_approx():
>       assert [1, 2] == pytest.approx([1.001, 2.002])
E       AssertionError: assert [1, 2] == approx([1.001...02 ± 2.0e-06])
E         (pytest_assertion plugin: representation of details failed: /Users/adalessa/Downloads/diffusion-master 2/venv/lib/python3.9/site-packages/_pytest/python_api.py:323: ModuleNotFoundError: No module named 'numpy'.
E          Probably an object has a faulty __repr__.)

test_approx.py:5: AssertionError
=========================== short test summary info ============================
FAILED test_approx.py::test_approx - AssertionError: assert [1, 2] == approx(...
============================== 1 failed in 0.04s ===============================

Expected result:

No ModuleNotFoundError: No module named 'numpy'. which makes the whole error message confusing and leads you to believe it failed because Numpy is not installed instead of the fact it was an assertion error.

@dzht19
Copy link

@dzht19 dzht19 commented Mar 3, 2022

Hi, I am a beginner and I am looking for the first issue to work on. Could I try to work on this one? Is there anyone else who started contributing?
Thank you for the answer in advance.

@nicoddemus
Copy link
Member

@nicoddemus nicoddemus commented Mar 4, 2022

@dzht19 please go ahead!

The offending line is here:

max_rel_diff = np.inf

np.inf should be replaced by math.inf, and the numpy import at the beginning of the function should be removed. Also we should fix our test suite: TestApprox.test_error_messages currently tests scalars, lists and numpy arrays, but it uses importorskip at the beginning, so we skip the tests if numpy is not installed. We should split the test into two: one which tests everything not-numpy related, and one which tests numpy-data and depends on numpy.

@Zac-HD Zac-HD added status: easy type: bug labels Mar 5, 2022
@AdrianDAlessandro
Copy link
Author

@AdrianDAlessandro AdrianDAlessandro commented Mar 15, 2022

Hi @dzht19,

Any progress on this?

@dzht19
Copy link

@dzht19 dzht19 commented Mar 16, 2022

Hi @dzht19,

Any progress on this?

Hi, Yes. I hope to finish till the end of the week.

@kianelbo
Copy link
Contributor

@kianelbo kianelbo commented Mar 20, 2022

Since there has been no activity from @dzht19 on this issue, I decided to open a quick PR. I will be glad if you take a look at it. Btw, this would be my first contribution ever and I hope you point out if I have been sloppy somewhere :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: easy type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants