question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ModuleNotFoundError for Numpy when pytest.approx fails

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
nicoddemuscommented, Mar 4, 2022

@dzht19 please go ahead!

The offending line is here:

https://github.com/pytest-dev/pytest/blob/9318b2cb7f81252fec215e1cce4c5de021bda180/src/_pytest/python_api.py#L343

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.

2reactions
dzht19commented, 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found