ModuleNotFoundError for Numpy when pytest.approx fails
See original GitHub issueDescription 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.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top Results From Across the Web
A module is found when calling the function but it is not found ...
ModuleNotFoundError with pytest (16 answers) ... Why clases is recognized when called by python but it is not when using pytest.
Read more >How to Fix: No module named NumPy - GeeksforGeeks
Numpy is a module used for array processing. The error “No module named numpy ” will occur when there is no NumPy library...
Read more >pytest: ModuleNotFoundError: No module named 'requests'
If your project is using a module that is only installed in your virtual environment, and you're using a system-wide pytest, it won't...
Read more >Changelog — pytest documentation
After 6.2.0, these types began failing, because they inherited neither from standard Python number hierarchy nor from numpy.ndarray . approx now converts ...
Read more >importlib — The implementation of import — Python 3.11.1 ...
modules before any loading begins, to prevent recursion from the import. If the loader inserted a module and the load fails, it must...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@dzht19 please go ahead!
The offending line is here:
https://github.com/pytest-dev/pytest/blob/9318b2cb7f81252fec215e1cce4c5de021bda180/src/_pytest/python_api.py#L343
np.infshould be replaced bymath.inf, and the numpy import at the beginning of the function should be removed. Also we should fix our test suite:TestApprox.test_error_messagescurrently tests scalars, lists and numpy arrays, but it usesimportorskipat 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.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.