Skip to content

ModuleNotFoundError for Numpy when pytest.approx fails #9726

@AdrianDAlessandro

Description

@AdrianDAlessandro

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueeasy issue that is friendly to new contributortype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions